diff --git a/scripts/lib/package-script-graph.ts b/scripts/lib/package-script-graph.ts index bab18ab3..eff06d86 100644 --- a/scripts/lib/package-script-graph.ts +++ b/scripts/lib/package-script-graph.ts @@ -245,7 +245,7 @@ export function validateNpmScopeEnvironment( environment: Readonly>, ): string[] { return Object.keys(environment) - .filter((name) => npmDispatchScopeEnvironmentNames.has(name.toLowerCase())) + .filter(isNpmScopeEnvironmentName) .map((name) => `npm scope environment is not allowed: ${name}`); } @@ -744,7 +744,7 @@ function parseEnvironmentAssignmentName(token: string): string | null { return equals > 0 ? token.slice(0, equals) : null; } -function isNpmScopeEnvironmentName(name: string): boolean { +export function isNpmScopeEnvironmentName(name: string): boolean { return npmDispatchScopeEnvironmentNames.has(name.toLowerCase()); } diff --git a/tests/unit/ci-workflow-generation.test.ts b/tests/unit/ci-workflow-generation.test.ts index efa9d001..8ba6e386 100644 --- a/tests/unit/ci-workflow-generation.test.ts +++ b/tests/unit/ci-workflow-generation.test.ts @@ -24,6 +24,7 @@ import { type CiWorkflowFileSystem, } from "../../scripts/generate-ci-workflow.ts"; import { + isNpmScopeEnvironmentName, validateInstallScriptPolicy, validatePackageScriptGraph, } from "../../scripts/lib/package-script-graph.ts"; @@ -743,6 +744,14 @@ describe("CI gate contract", () => { }, ); + // 1101/15-05. `npx vitest` and npm-launched IDE runners hand the test + // process npm_config_prefix/userconfig/globalconfig. The preflight above + // rightly refuses them, so 79 tests here failed for the caller's + // environment, not for the code. vitest.config.ts drops them. + it("starts without the npm scope variables an npm-launched runner sets", () => { + expect(Object.keys(process.env).filter(isNpmScopeEnvironmentName)).toEqual([]); + }); + it.each([ ["npm run nested --ignore-scripts=false", "nested"], ["npm test -- --ignore-scripts=true", "test"], diff --git a/vitest.config.ts b/vitest.config.ts index 9235ef63..d89eec90 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,10 +1,19 @@ import { configDefaults, defineConfig } from "vitest/config"; +import { isNpmScopeEnvironmentName } from "./scripts/lib/package-script-graph.ts"; + // Tests must not inherit a caller's production React/runtime selection. // Package scripts also launch Vitest through scripts/run-vitest.ts, which owns // NODE_ENV before the Vitest process starts. Keeping the config deterministic // protects direct `vitest` invocations as well. process.env.NODE_ENV = "test"; +// 1101/15-05. `npx vitest` and npm-launched IDE runners set npm_config_prefix, +// _userconfig and _globalconfig. The CI gate preflight refuses those in a real +// runner (scripts/contracts/ci-gates.ts), so tests that run it, in process or +// as a child, failed for the caller's environment. Tests start without them. +for (const name of Object.keys(process.env)) { + if (isNpmScopeEnvironmentName(name)) delete process.env[name]; +} // tests/system needs CI-runner host facilities (see docs/testing/taxonomy.md). // It runs only when a caller names it, as `test:system` does; a bare `vitest`