@@ -319,24 +319,53 @@ describe.skipIf(process.platform === "win32")("dist artifact ownership", () => {
319319 } , signal ) ;
320320 } ) ;
321321
322- it ( "keeps declarations alive until their writer joins and keeps ownership across dist cleanup" , async ( {
323- signal,
324- } ) => {
325- await withProcesses ( async ( { checkpoint, waitEvent, start } ) => {
326- const root = createCheckout ( ) ;
327- installCompiler ( root , checkpoint ( "declarations-ready" ) ) ;
328- write ( root , "pnpm.cjs" , checkpoint ( "build-started" ) ) ;
329- const writer = start ( root , path . join ( sourceRoot , "scripts/run-tsgo.mts" ) , tsgoArgs ) ;
330- const writerGate = await writer . event ( "declarations-ready" ) ;
331- const declaration = path . join ( root , declarationPath ) ;
332- expect ( fs . readFileSync ( declaration , "utf8" ) ) . toContain ( "interface Channel" ) ;
322+ it . for ( [
323+ { directory : "." , nested : false } ,
324+ { directory : "src" , nested : false } ,
325+ { directory : "src" , nested : true } ,
326+ ] ) (
327+ "keeps declarations alive from $directory (nested=$nested) until their writer joins and keeps ownership across dist cleanup" ,
328+ { timeout : 30_000 } ,
329+ async ( { directory, nested } , { signal } ) => {
330+ await withProcesses ( async ( { checkpoint, waitEvent, start } ) => {
331+ const root = createCheckout ( ) ;
332+ const cwd = path . join ( root , directory ) ;
333+ installCompiler ( root , checkpoint ( "declarations-ready" ) ) ;
334+ if ( directory !== "." ) {
335+ fs . symlinkSync ( path . join ( root , "node_modules" ) , path . join ( cwd , "node_modules" ) ) ;
336+ }
337+ write ( root , "pnpm.cjs" , checkpoint ( "build-started" ) ) ;
338+ const writerArgs = [
339+ "-p" ,
340+ path . join ( root , "tsconfig.plugin-sdk.dts.json" ) ,
341+ "--declaration" ,
342+ "true" ,
343+ ] ;
344+ const compilerScript = path . join ( sourceRoot , "scripts/run-tsgo.mts" ) ;
345+ const writerScript = nested
346+ ? write (
347+ root ,
348+ "nested-writer.mts" ,
349+ `
350+ import { withDistArtifactOwnership, distArtifactEntryArgs } from ${ JSON . stringify ( path . join ( sourceRoot , "scripts/lib/dist-artifact-ownership.mts" ) ) } ;
351+ import { runManagedCommand } from ${ JSON . stringify ( path . join ( sourceRoot , "scripts/lib/managed-child-process.mts" ) ) } ;
352+ await withDistArtifactOwnership(${ JSON . stringify ( root ) } , () => runManagedCommand({
353+ bin: process.execPath, args: distArtifactEntryArgs(${ JSON . stringify ( compilerScript ) } , ${ JSON . stringify ( writerArgs ) } ), requireProcessTreeExit: true,
354+ }));
355+ ` ,
356+ )
357+ : compilerScript ;
358+ const writer = start ( cwd , writerScript , nested ? [ ] : writerArgs ) ;
359+ const writerGate = await writer . event ( "declarations-ready" ) ;
360+ const declaration = path . join ( root , declarationPath ) ;
361+ expect ( fs . readFileSync ( declaration , "utf8" ) ) . toContain ( "interface Channel" ) ;
333362
334- // Advance the contender's wall clock past the observed sixteen-minute build
335- // without spending that time in Vitest; restore it before executing tsdown.
336- const contender = write (
337- root ,
338- "contender.mts" ,
339- `
363+ // Advance the contender's wall clock past the observed sixteen-minute build
364+ // without spending that time in Vitest; restore it before executing tsdown.
365+ const contender = write (
366+ root ,
367+ "contender.mts" ,
368+ `
340369 import { withDistArtifactOwnership } from ${ JSON . stringify ( path . join ( sourceRoot , "scripts/lib/dist-artifact-ownership.mts" ) ) } ;
341370 import { runTsdownBuild } from ${ JSON . stringify ( path . join ( sourceRoot , "scripts/tsdown-build.mts" ) ) } ;
342371 const now = Date.now;
@@ -347,44 +376,49 @@ describe.skipIf(process.platform === "win32")("dist artifact ownership", () => {
347376 return await runTsdownBuild(${ JSON . stringify ( buildArgs ) } );
348377 });
349378 ` ,
350- ) ;
351- const build = start ( root , contender ) ;
352- await Promise . race ( [ build . waiting , waitEvent ( "build-started" ) , build . done ] ) ;
353- // Before the repair the real tsdown cleanup deletes the emitted file here.
354- expect (
355- fs . existsSync ( declaration ) ,
356- "cleanup must wait for the active declaration writer" ,
357- ) . toBe ( true ) ;
358- writerGate . write ( "continue" ) ;
359- expect ( await writer . done ) . toMatchObject ( { code : 0 } ) ;
360- const buildGate = await build . event ( "build-started" ) ;
361- expect ( fs . existsSync ( declaration ) ) . toBe ( false ) ;
379+ ) ;
380+ const build = start ( root , contender ) ;
381+ await Promise . race ( [ build . waiting , waitEvent ( "build-started" ) , build . done ] ) ;
382+ // Before the repair the real tsdown cleanup deletes the emitted file here.
383+ expect (
384+ fs . existsSync ( declaration ) ,
385+ "cleanup must wait for the active declaration writer" ,
386+ ) . toBe ( true ) ;
387+ writerGate . write ( "continue" ) ;
388+ expect ( await writer . done ) . toMatchObject ( { code : 0 } ) ;
389+ const buildGate = await build . event ( "build-started" ) ;
390+ expect ( fs . existsSync ( declaration ) ) . toBe ( false ) ;
362391
363- installCompiler ( root , checkpoint ( "next-declarations-ready" ) ) ;
364- const nextWriter = start ( root , path . join ( sourceRoot , "scripts/run-tsgo.mts" ) , tsgoArgs ) ;
365- await Promise . race ( [
366- nextWriter . waiting ,
367- waitEvent ( "next-declarations-ready" ) ,
368- nextWriter . done ,
369- ] ) ;
370- expect ( fs . existsSync ( declaration ) , "deleting dist must not delete build ownership" ) . toBe (
371- false ,
372- ) ;
392+ installCompiler ( root , checkpoint ( "next-declarations-ready" ) ) ;
393+ const nextWriter = start ( root , path . join ( sourceRoot , "scripts/run-tsgo.mts" ) , tsgoArgs ) ;
394+ await Promise . race ( [
395+ nextWriter . waiting ,
396+ waitEvent ( "next-declarations-ready" ) ,
397+ nextWriter . done ,
398+ ] ) ;
399+ expect ( fs . existsSync ( declaration ) , "deleting dist must not delete build ownership" ) . toBe (
400+ false ,
401+ ) ;
373402
374- const otherRoot = createCheckout ( ) ;
375- installCompiler ( otherRoot , checkpoint ( "other-checkout-ready" ) ) ;
376- const independent = start ( otherRoot , path . join ( sourceRoot , "scripts/run-tsgo.mts" ) , tsgoArgs ) ;
377- ( await independent . event ( "other-checkout-ready" ) ) . write ( "continue" ) ;
378- expect ( await independent . done ) . toMatchObject ( { code : 0 } ) ;
379- expect ( fs . existsSync ( declaration ) ) . toBe ( false ) ;
403+ const otherRoot = createCheckout ( ) ;
404+ installCompiler ( otherRoot , checkpoint ( "other-checkout-ready" ) ) ;
405+ const independent = start (
406+ otherRoot ,
407+ path . join ( sourceRoot , "scripts/run-tsgo.mts" ) ,
408+ tsgoArgs ,
409+ ) ;
410+ ( await independent . event ( "other-checkout-ready" ) ) . write ( "continue" ) ;
411+ expect ( await independent . done ) . toMatchObject ( { code : 0 } ) ;
412+ expect ( fs . existsSync ( declaration ) ) . toBe ( false ) ;
380413
381- buildGate . write ( "continue" ) ;
382- expect ( await build . done ) . toMatchObject ( { code : 0 } ) ;
383- ( await nextWriter . event ( "next-declarations-ready" ) ) . write ( "continue" ) ;
384- expect ( await nextWriter . done ) . toMatchObject ( { code : 0 } ) ;
385- expect ( fs . readFileSync ( declaration , "utf8" ) ) . toContain ( "interface Channel" ) ;
386- } , signal ) ;
387- } , 30_000 ) ;
414+ buildGate . write ( "continue" ) ;
415+ expect ( await build . done ) . toMatchObject ( { code : 0 } ) ;
416+ ( await nextWriter . event ( "next-declarations-ready" ) ) . write ( "continue" ) ;
417+ expect ( await nextWriter . done ) . toMatchObject ( { code : 0 } ) ;
418+ expect ( fs . readFileSync ( declaration , "utf8" ) ) . toContain ( "interface Channel" ) ;
419+ } , signal ) ;
420+ } ,
421+ ) ;
388422
389423 it ( "retains ownership when a supervisor exits before its compiler joins" , async ( { signal } ) => {
390424 await withProcesses ( async ( { checkpoint, waitEvent, start } ) => {
0 commit comments