@@ -75,6 +75,8 @@ import { fileURLToPath } from 'node:url';
7575import ts from 'typescript' ;
7676import { afterAll , beforeAll , describe , expect , it } from 'vitest' ;
7777
78+ import { childEnv } from './helpers/serve-process.js' ;
79+
7880const HERE = resolve ( fileURLToPath ( import . meta. url ) , '..' ) ;
7981/** `packages/cli` — this package's own root, never another package's. */
8082const PACKAGE_ROOT = resolve ( HERE , '..' ) ;
@@ -200,9 +202,13 @@ function pnpmPack(destination: string): { filename: string; files: string[] } {
200202 const execpath = process . env . npm_execpath ;
201203 const viaExecpath = typeof execpath === 'string' && / p n p m / . test ( basename ( execpath ) ) ;
202204 const [ command , prefix ] : [ string , string [ ] ] = viaExecpath ? [ process . execPath , [ execpath as string ] ] : [ 'pnpm' , [ ] ] ;
205+ // `childEnv()` — every child spawned from this directory declares its
206+ // environment (check:cli-test-child-env): the vitest worker's `TEST`/`VITEST*`
207+ // family and `NODE_PATH` are stripped, everything pnpm needs (PATH, HOME) stays.
203208 const res = spawnSync ( command , [ ...prefix , 'pack' , '--pack-destination' , destination , '--json' ] , {
204209 cwd : PACKAGE_ROOT ,
205210 encoding : 'utf8' ,
211+ env : childEnv ( ) ,
206212 } ) ;
207213 if ( res . error ) throw new Error ( `pnpm pack could not start (${ command } ): ${ res . error . message } ` ) ;
208214 if ( res . status !== 0 ) {
@@ -264,7 +270,7 @@ beforeAll(() => {
264270 // Unpack into a consumer's node_modules. The tarball root is `package/`.
265271 const extractDir = join ( scratch , 'extract' ) ;
266272 mkdirSync ( extractDir ) ;
267- const tar = spawnSync ( 'tar' , [ '-xzf' , packed . filename , '-C' , extractDir ] , { encoding : 'utf8' } ) ;
273+ const tar = spawnSync ( 'tar' , [ '-xzf' , packed . filename , '-C' , extractDir ] , { encoding : 'utf8' , env : childEnv ( ) } ) ;
268274 if ( tar . status !== 0 ) throw new Error ( `tar -xzf failed (${ tar . status } ): ${ tar . stderr } ` ) ;
269275 const consumer = join ( scratch , 'consumer' ) ;
270276 const scope = join ( consumer , 'node_modules' , ...PACKAGE_NAME . split ( '/' ) . slice ( 0 , - 1 ) ) ;
@@ -279,10 +285,12 @@ beforeAll(() => {
279285
280286 const probePath = join ( consumer , 'probe.mjs' ) ;
281287 writeFileSync ( probePath , PROBE_SOURCE ) ;
288+ // `childEnv()` already strips `NODE_PATH`, so nothing of this workspace's
289+ // resolution base reaches the probe: what resolves, resolves from `consumer`.
282290 const run = spawnSync ( process . execPath , [ probePath , HOOK_BODY_SPECIFIER , MANIFEST_SPECIFIER , DEEP_PATH_SPECIFIER ] , {
283291 cwd : consumer ,
284292 encoding : 'utf8' ,
285- env : { ... process . env , NODE_PATH : '' } ,
293+ env : childEnv ( ) ,
286294 } ) ;
287295 if ( run . status !== 0 ) throw new Error ( `probe exited ${ run . status } \n--- stderr ---\n${ run . stderr } \n--- stdout ---\n${ run . stdout } ` ) ;
288296 probe = JSON . parse ( run . stdout ) as ProbeResult ;
0 commit comments