Skip to content

fix: support import.meta.env reassignment#10123

Open
DaveT1991 wants to merge 3 commits intovitest-dev:mainfrom
DaveT1991:fix/import-meta-env-assignment
Open

fix: support import.meta.env reassignment#10123
DaveT1991 wants to merge 3 commits intovitest-dev:mainfrom
DaveT1991:fix/import-meta-env-assignment

Conversation

@DaveT1991
Copy link
Copy Markdown
Contributor

Fixes #10091

import.meta.env = ... currently gets rewritten into a non-assignable expression, which causes a parse error before the test file can run.

This change handles direct import.meta.env = ... assignments as a dedicated AST rewrite while keeping the existing proxy-based rewrite for other import.meta.env access. That lets whole-object reassignment parse correctly and still keeps property reads/writes aligned with Vitest's worker env proxy.

Changes:

  • rewrite direct import.meta.env = ... assignments to merge object values into the worker env proxy and return the proxy object
  • keep the existing import.meta.env access rewrite for non-assignment cases
  • add regression tests for direct import.meta.env reassignment in:
    • test/core/test/env.test.ts
    • test/core/test/env-jsdom.test.ts
    • test/browser/test/env.test.ts

Verification:

  • pnpm exec eslint packages/vitest/src/node/plugins/metaEnvReplacer.ts test/core/test/env.test.ts test/core/test/env-jsdom.test.ts test/browser/test/env.test.ts
  • pnpm exec vitest run test/env.test.ts test/env-jsdom.test.ts

I added the browser regression test, but I wasn't able to run the browser-side verification locally because this workspace picks up a parent PostCSS config that requires autoprefixer outside the Vitest repo checkout.

@netlify
Copy link
Copy Markdown

netlify bot commented Apr 10, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 0bd2550
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/69d925870a7d130008c73bfb
😎 Deploy Preview https://deploy-preview-10123--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

…r in tests

acorn-walk's ancestor() visitor signature is (node, state, ancestors) —
the previous code used (node, ancestors) so ancestors was actually
receiving the state arg (undefined at runtime), causing a crash when
accessing ancestors.length and TypeScript errors about unknown types.

The direct `import.meta.env = ...` assignments in tests are read-only
per TypeScript's ImportMeta, but vitest's MetaEnvReplacerPlugin rewrites
them at runtime. Added // @ts-expect-error to suppress the compile-time
error in all three test files.
@hi-ogawa hi-ogawa added the maybe automated User is likely an AI agent, or the content was generated by an AI assistant without user control label Apr 11, 2026
@github-actions
Copy link
Copy Markdown

Hello @DaveT1991. Your PR has been labeled maybe automated because it appears to have been fully generated by AI with no human involvement. It will be closed automatically in 3 days unless a real person responds.

If you're a real person behind this contribution, please:

  • Confirm you've personally reviewed and stand behind its content
  • Make sure it follows our contribution guidelines and uses the correct GitHub template
  • Disclose any AI tools you used (e.g. Claude, Copilot, Codex)

If you believe this was flagged by mistake, leave a comment.

These measures help us reduce maintenance burden and keep the team's work efficient. See our AI contributions policy for more context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maybe automated User is likely an AI agent, or the content was generated by an AI assistant without user control

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parse error in import.meta.env assignment

2 participants