Repository navigation
Description
Astro Info
Astro v7.3.6
Node v26.10.0 (also affects Node 22)
System Linux (x64)
Package Manager pnpm
Output static
Adapter none
Integrations keystatic (not required to reproduce, see below)
Describe the Bug
In dev, the generated astro:env/server virtual module fails to parse with:
[ERROR] Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.
Stack trace:
at astro:env/server:17:863
Root cause (packages/astro/src/env/vite-plugin-env.ts, getTemplates()):
server = server.replace('// @@GET_ENV@@', `return (${JSON.stringify(loadedEnv)})[key];`);The replacement is passed as a string, so any $ patterns ($\`` , $&, $', $1, …) inside JSON.stringify(loadedEnv) are interpreted as replacement patterns instead of literal text. When any environment variable contains e.g. `` $ ``, the template prefix gets injected into the middle of the inlined env JSON, producing an unterminated string literal on line 17 (the // @@GET_ENV@@ line).
This is trivially triggerable in the real world: the Kitty terminal exports KITTY_PUBLIC_KEY=1:… by default, whose value commonly contains $ followed by a backtick. So every Kitty user importing astro:env/server in astro dev hits this. Any secret in .env containing $&/$\`` breaks it too. astro build` is unaffected (env is not inlined there).
I verified this by replicating the generation and parsing with esbuild: string replacement → ERROR: Unterminated string literal at exactly 17:863 (matching the dev server error); function replacement → parses fine.
Reproduction (minimal, no Keystatic needed)
- Minimal Astro project with any SSR module importing
astro:env/server, e.g.src/pages/api/test.ts:import { getSecret } from 'astro:env/server'; export const GET = () => new Response('ok');
- Run dev with a hostile env var:
FOO='a$\b' astro dev(or just use the Kitty terminal, which setsKITTY_PUBLIC_KEY`). - Request the route → the
[ERROR] Failed to parse source… at astro:env/server:17:…appears and the page/client fails to load the module.
What's the expected result?
The generated module must be valid JS regardless of env var contents.
Proposed fix
One line — use a replacer function (immune to $ patterns):
- server = server.replace('// @@GET_ENV@@', `return (${JSON.stringify(loadedEnv)})[key];`);
+ server = server.replace('// @@GET_ENV@@', () => `return (${JSON.stringify(loadedEnv)})[key];`);(Same latent pattern exists one line above for // @@ON_SET_GET_ENV@@, but its input is schema keys rather than arbitrary process env.)
Participation
- I am willing to submit a pull request for this issue.
Activity
astro-factory commentedon Oct 10, 2026
Triage complete
triage: fix pendingpackages/astro/test/units/env/vite-plugin-env.test.tsastro dev, and only when some env var contains$sequences (for example Kitty'sKITTY_PUBLIC_KEY).astro buildis not affected.Your analysis is correct. In
getTemplates()inpackages/astro/src/env/vite-plugin-env.ts, the dev env is inlined withString.prototype.replaceusing a string as the replacement, so$`,$&,$'and$$insideJSON.stringify(loadedEnv)are treated as replacement patterns. With$`, the template text that comes before the placeholder gets pasted into a JSON string literal, which breaks line 17 of the generated module. The other patterns change values without any error (for example,$$becomes$). Build is not affected becauseloadedEnvisnullthere.I found a potential fix for this issue. It's the one-line change you proposed: use function replacers for the
// @@GET_ENV@@replacement, and also for the// @@ON_SET_GET_ENV@@one, because$is a valid identifier character in schema keys. It needs review by a maintainer.Try this fix
You can test this fix right now without waiting for a release:
# astro npm i https://pkg.pr.new/astro@f9d681cIf this fixes your issue, please leave a comment letting us know (for example
"confirmed, this fixes it"). A pull request will then be opened to get it merged.
Full Triage Report
Reproduction
Environment: Astro 7.3.8 (built from the monorepo
packages/astro), Node.js (sandbox),minimalexample with no adapter.examples/minimalinto the triage directory.src/pages/api/test.ts:FOO='a$`b' astro dev, thencurl /api/test.Result: The page returned an error, and the server logged:
Baseline: Without
FOO, the same request returnedok:functionwith status[200].Diagnosis
File:
packages/astro/src/env/vite-plugin-env.ts,getTemplates(), around lines 202–206:loadedEnvholds the whole loaded env, includingprocess.env(loadedEnv: isBuild ? null : loadedEnv).$patterns in it get expanded.$`inserts the beginning oftemplates/env.mjs, which contains newlines, so the string literal is never closed. The// @@GET_ENV@@placeholder sits on line 17 of that template, which matches the error position.{FOO:"a$`b"}producesreturn ({"FOO":"a<template prefix>b"})[key];, while a function replacer keeps the value as-is.Verification
$handling here.GET_ENVline was added in a9373c0 ("fix: do not freeze process.env in dev (fix: do not freeze process.env in dev #12585)", 2024-12). TheON_SET_GET_ENVline has been there sinceastro:envwas introduced (41121fb).Fix
FOO='a$`b$&c' astro dev,/api/testreturns[200]and there is no parse error.packages/astro/test/units/env/vite-plugin-env.test.tsbuilds theastroEnvplugin inservemode with a stub env loader. It loads\0astro:env/serverand checks that the inlined env JSON appears unchanged for the valuesa$`bandc$&d$'e$$f. It fails without the fix and passes with it. All 34 tests intest/units/env/*pass..changeset/ten-poets-pull.md(astro: patch).$as$$would also work, but the function replacer is simpler and is the usual way to do this.node -escript that usedString.replacewith a string replacement corrupted the source file in exactly the way described in this issue.This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.