vitest-dev / vitest-dev/vitest

Parse error in import.meta.env assignment

Open
#10,091 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

p3-minor-bug
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Describe the bug

A parse error occurred in the code where import.meta.env is being written to.
Code causing the error:

import.meta.env = import.meta.env || {}
import.meta.env.__config__ = 'true'

Below are the details of the error:

 FAIL  test/basic.test.ts [ test/basic.test.ts ]
Error: Transform failed with 1 error:

[PARSE_ERROR] Error: Cannot assign to this expression
   ╭─[ home/projects/vitest-dev-vitest-wdp1vbyr/src/env.ts:2:3 ]
   │
 2 │   Object.assign(/* istanbul ignore next */ globalThis.__vitest_worker__?.metaEnv ?? import.meta.env) = Object.assign(/* istanbul ignore next */ globalThis.__vitest_worker__?.metaEnv ?? import.meta.env) || {}
   │   ─────────────────────────────────────────────────┬────────────────────────────────────────────────  
   │                                                    ╰────────────────────────────────────────────────── 
───╯

  Plugin: vite:oxc
  File: /home/projects/vitest-dev-vitest-wdp1vbyr/src/env.ts
 ❯ transformWithOxc node_modules/.pnpm/vite@8.0.3_@emnapi+core@1.9.2_@emnapi+runtime@1.9.2/node_modules/vite/dist/node/chunks/node.js:3839:19
 ❯ TransformPluginContext.transform node_modules/.pnpm/vite@8.0.3_@emnapi+core@1.9.2_@emnapi+runtime@1.9.2/node_modules/vite/dist/node/chunks/node.js:3907:26
 ❯ EnvironmentPluginContainer.transform node_modules/.pnpm/vite@8.0.3_@emnapi+core@1.9.2_@emnapi+runtime@1.9.2/node_modules/vite/dist/node/chunks/node.js:30167:51
 ❯ loadAndTransform node_modules/.pnpm/vite@8.0.3_@emnapi+core@1.9.2_@emnapi+runtime@1.9.2/node_modules/vite/dist/node/chunks/node.js:24296:26

Also, thank you for the help in the Q&A

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-wdp1vbyr?file=test%2Fbasic.test.ts,src%2Fenv.ts

System Info
System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 22.22.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/coverage-v8: latest => 4.1.2 
    @vitest/ui: latest => 4.1.2 
    vite: latest => 8.0.3 
    vitest: latest => 4.1.2
Used Package Manager

pnpm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/env.ts and reproduce the failure using the linked StackBlitz example or test/basic.test.ts. Check how the import.meta.env assignment is transformed by Vite's OXC plugin. Done means the example no longer produces a parse error and the environment assignment still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, vite
Domain
devtools, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.