Watch mode: clean up output files when source files are deleted#2710
Open
DukeDeSouth wants to merge 1 commit intomicrosoft:mainfrom
Open
Watch mode: clean up output files when source files are deleted#2710DukeDeSouth wants to merge 1 commit intomicrosoft:mainfrom
DukeDeSouth wants to merge 1 commit intomicrosoft:mainfrom
Conversation
When using `tsc --watch`, deleting a source .ts file did not remove the corresponding output files (.js, .js.map, .d.ts, .d.ts.map) from the output directory. This left stale outputs that could be picked up by test runners, bundlers, and other downstream tools. The watcher already detected file deletions and triggered rebuilds, but never cleaned up the orphaned outputs. This change adds a `cleanupDeletedOutputs` method that: 1. Captures deleted source file names in `hasBeenModified()` by comparing the previous file snapshot against the current state. 2. After emit, computes output paths for each deleted source file using the existing `outputpaths` package. 3. Removes the stale output files via `vfs.FS.Remove()`. The cleanup handles all output types: .js, .js.map (sourceMap), .d.ts (declaration), and .d.ts.map (declarationMap). It respects `outDir`, `declarationDir`, `noEmit`, and other compiler options. Fixes microsoft/TypeScript#16057 Co-authored-by: Cursor <cursoragent@cursor.com>
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human View
Summary
Fixes microsoft/TypeScript#16057 — a 9-year-old bug (154 reactions) where
tsc --watchdoes not delete corresponding output files when a source.tsfile is deleted.Problem
When using
tsc --watch, deleting a source.tsfile triggers a rebuild, but the corresponding output files (.js,.js.map,.d.ts,.d.ts.map) remain on disk. This causes:Solution
The watcher already detects file deletions (in
hasBeenModified()), but never cleans up the orphaned outputs. This PR adds acleanupDeletedOutputs()method that:hasBeenModified()by comparing the previous file snapshot against the current stateoutputpathspackagevfs.FS.Remove()The cleanup handles all output types:
.jsfiles.js.map(whensourceMapis enabled).d.ts(whendeclarationis enabled).d.ts.map(whendeclarationMapis enabled)It respects
outDir,declarationDir,noEmit, and other compiler options.Changes
internal/execute/watcher.godeletedFilesfield, capture deleted files inhasBeenModified(), addcleanupDeletedOutputs()method, call it fromDoCycle()internal/execute/tsctests/tscwatch_test.gotestdata/baselines/reference/tscWatch/commandLineWatch/watch-cleans-up-deleted-file-outputs.jsTest
The new test case:
a.tsandb.tswithoutDir: "dist"dist/a.jsanddist/b.jsb.tsdist/b.jsis removedThe baseline confirms the deletion:
[/home/src/workspaces/project/dist/b.js] *deleted*All existing watch mode tests pass without modification.
AI View (DCCE Protocol v1.0)
Metadata
AI Contribution Summary
Verification Steps Performed
Human Review Guidance
.js.map,.d.ts,.d.ts.mapMade with M7 Cursor