{"id":"b607461259dc22c2","repo":"vitest-dev/vitest","slug":"cannot-find-the-environment-this-is-a-bug-in-vite","errorCode":null,"errorMessage":"Cannot find the environment. This is a bug in Vitest.","messagePattern":"Cannot find the environment\\. This is a bug in Vitest\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/node/pool.ts","lineNumber":119,"sourceCode":"\n      const taskGroup: PoolTask[] = []\n      const browserSpecs: TestSpecification[] = []\n      taskGroups.push({\n        tasks: taskGroup,\n        maxWorkers: group.maxWorkers,\n        browserSpecs,\n      })\n\n      for (const specs of group.specs) {\n        const { project, pool } = specs[0]\n        if (pool === 'browser') {\n          browserSpecs.push(...specs)\n          continue\n        }\n\n        const environment = environments.get(specs[0])!\n        if (!environment) {\n          throw new Error(`Cannot find the environment. This is a bug in Vitest.`)\n        }\n\n        let env = projectEnvs.get(project)\n        if (!env) {\n          env = {\n            ...process.env,\n            ...options.env,\n            ...ctx.config.env,\n            ...project.config.env,\n          }\n\n          // V8 serializes compile-cached scripts without the source positions\n          // that precise coverage relies on, so the compile cache must stay off\n          // for the v8 provider (and custom providers, whose mechanism we can't\n          // assume) in workers and any process they spawn. istanbul instruments\n          // the source at transform time, so the cache is harmless there and the\n          // boot speedup is kept.\n          if (ctx.config.coverage.enabled && ctx.config.coverage.provider !== 'istanbul') {","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/node/pool.ts#L101-L137","documentation":"Internal assertion thrown when a resolved TestSpecification has no matching ContextTestEnvironment in the environments WeakMap built by getSpecificationsOptions. The environments map is populated during spec collection, so a missing entry means the spec/environment resolution pipeline produced an entry it cannot service — this is not a user-facing config error but a framework invariant violation.","triggerScenarios":"An internal divergence where getSpecificationsOptions returns an environments WeakMap that does not contain a key for specs[0] during task grouping in createPool (packages/vitest/src/node/pool.ts:117-119). Typically only reproducible with custom environment plugins that mishandle spec resolution or across a Vitest version regression.","commonSituations":"Upgrading Vitest to a version with a regression in environment resolution; a custom environment provider that throws during initialization leaving the WeakMap partially populated; race conditions during watch-mode re-collection.","solutions":["Report as a Vitest bug with a minimal reproduction (the message itself says so).","Downgrade to the last working Vitest version to confirm it is a regression.","Disable any custom environment plugin to isolate whether a third-party environment is involved.","Clear node_modules/.vite and reinstall to rule out a stale build artifact."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// This is an internal assertion; surface it with context for a bug report\ntry {\n  await vitest.start()\n} catch (e) {\n  if (e.message === 'Cannot find the environment. This is a bug in Vitest.') {\n    console.error('Vitest internal bug — please report with reproduction.')\n    console.error('Vitest version:', require('vitest/package.json').version)\n  }\n  throw e\n}","preventionTips":["Pin Vitest to a known-good version in CI to avoid regressions in environment resolution.","Minimize custom environment plugins when upgrading.","Keep a minimal repro project to bisect regressions quickly."],"tags":["internal-bug","environment","assertion"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}