@@ -98,6 +98,12 @@ export function cleanTsdownOutputRoots(params = {}) {
9898 const fsImpl = params . fs ?? fs ;
9999 const env = params . env ?? process . env ;
100100 const roots = params . roots ?? listTsdownOutputRoots ( ) ;
101+ const rootPaths = roots . map ( ( root ) => path . join ( cwd , root ) ) ;
102+ // Validate the complete mutation set before traversing protected children or
103+ // cleaning any earlier root; otherwise a later symlink can leave a partial build.
104+ for ( const rootPath of rootPaths ) {
105+ assertRealOutputRoot ( rootPath , { fs : fsImpl } ) ;
106+ }
101107 const protectedDeclarationPaths =
102108 env [ RUN_NODE_SKIP_DTS_BUILD_ENV ] === "1"
103109 ? listExistingDeclarationOutputPaths ( {
@@ -110,11 +116,7 @@ export function cleanTsdownOutputRoots(params = {}) {
110116 ...protectedDeclarationPaths ,
111117 ...listExistingPreservedOutputPaths ( { cwd, env, fs : fsImpl } ) ,
112118 ] ) ;
113- for ( const root of roots ) {
114- const rootPath = path . join ( cwd , root ) ;
115- // The guard throws outside the best-effort catch below: a symlinked root
116- // must abort the build, not be swallowed as a skippable cleanup error.
117- assertRealOutputRoot ( rootPath , { fs : fsImpl } ) ;
119+ for ( const rootPath of rootPaths ) {
118120 try {
119121 if ( hasProtectedChild ( { rootPath, protectedPaths } ) ) {
120122 cleanOutputRootExcept ( rootPath , protectedPaths , fsImpl ) ;
@@ -214,6 +216,8 @@ export function pruneStaleRootChunkFiles(params = {}) {
214216 const roots = listTsdownOutputRoots ( { cwd, fs : fsImpl } ) . map ( ( root ) => path . join ( cwd , root ) ) ;
215217 for ( const root of roots ) {
216218 assertRealOutputRoot ( root , { fs : fsImpl } ) ;
219+ }
220+ for ( const root of roots ) {
217221 let entries ;
218222 try {
219223 entries = fsImpl . readdirSync ( root , { withFileTypes : true } ) ;
0 commit comments