Repository navigation
fix(watch): delete stale output files when a source file is removed in tsc --watch - #65626
Open
DeviousCardi wants to merge 1 commit into
Open
DeviousCardi wants to merge 1 commit into
DeviousCardi wants to merge 1 commit into
Conversation
…n tsc --watch Fixes microsoft#16057. Root cause: the watch mode tracked every file it emitted in Watcher.emittedFiles (output -> source), and pruneEmittedFiles() only removed entries from that in-memory map when the source disappeared from the program. The actual files on disk were never removed, so deleting (or moving/excluding) a source file while running `tsc --watch` left orphaned .js/.d.ts/.map outputs behind forever. Fix (tsc/internal/execute/watcher.go): - emittedFiles now stores an emittedFile record (original output path in its original casing, source path key, and whether the output was already a project input when emitted). - pruneEmittedFiles() now deletes the output from disk when its source is gone from the program, via the new deleteEmittedOutput() helper. - Safety guards: files that are/were project inputs (e.g. an emitted .js that is also an allowJs input listed in "files", or an externally supplied .d.ts) and config files are never deleted; already-missing files are ignored; other removal failures are reported with TS6055-style diagnostics (Failed_to_delete_file_0) instead of crashing the watch cycle. - Outputs whose source still exists (noEmit toggled on, output location changed) are only untracked, never deleted, so no outputs that should be kept are removed. - A stale .tsbuildinfo recorded at a previous location is removed. Tests: - New tsc/internal/execute/tsctests/watch_output_clean_test.go with 7 regression tests: outDir + declaration + maps + incremental deletion, no-outDir deletion, input-collision protection (TS5055), "files"-listed source deletion, already-deleted output (no crash), delete-then-recreate, and move/rename cleanup. - New baselined TestWatch scenario "watch deletes outputs of deleted source file in outDir" plus updated baselines for the 8 existing watch scenarios that delete/move sources (outputs now show as *deleted*). Validation: go build ./internal/execute/..., go vet, gofmt clean; go test ./internal/execute/... ./cmd/... ./internal/api/... ./internal/project/... -count=1 all pass; go test -race on the watcher race tests passes. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Output pruning can delete previously emitted files that are later explicitly selected as inputs, risking loss of project inputs.
4 open issues
What changed in this PR
This PR addresses #16057 by adding stale-output cleanup to tsc --watch when sources are deleted, moved, or excluded from the program.
Changes:
- Tracks original output paths and input ownership, then removes stale emitted files.
- Adds deletion safeguards and reports removal failures as diagnostics.
- Adds seven regression tests, a watch scenario, and updated deletion/recreation baselines.
| File | Description |
|---|---|
tsc/internal/execute/watcher.go |
Implements output tracking, cleanup, and deletion safeguards. |
tsc/internal/execute/tsctests/watch_output_clean_test.go |
Adds seven cleanup regression tests. |
tsc/internal/execute/tsctests/tscwatch_test.go |
Adds a baselined deletion/recreation scenario. |
tsc/testdata/baselines/reference/tscWatch/commandLineWatch/watch-with-tsconfig-files-list-entry-deleted.js |
Records cleanup for a deleted explicit root file. |
tsc/testdata/baselines/reference/tscWatch/commandLineWatch/watch-rebuilds-when-source-file-is-deleted.js |
Records output deletion during rebuild. |
tsc/testdata/baselines/reference/tscWatch/commandLineWatch/watch-detects-nested-subdirectory-removed-and-recreated.js |
Records nested output deletion and recreation. |
tsc/testdata/baselines/reference/tscWatch/commandLineWatch/watch-detects-module-going-missing-then-coming-back.js |
Records module output deletion and recreation. |
tsc/testdata/baselines/reference/tscWatch/commandLineWatch/watch-detects-imported-directory-removed.js |
Records cleanup after imported-directory removal. |
tsc/testdata/baselines/reference/tscWatch/commandLineWatch/watch-detects-import-path-restructured.js |
Records cleanup at the old source location. |
tsc/testdata/baselines/reference/tscWatch/commandLineWatch/watch-detects-file-renamed-and-renamed-back.js |
Records output cleanup across renames. |
tsc/testdata/baselines/reference/tscWatch/commandLineWatch/watch-detects-file-deleted-and-new-file-added-simultaneously.js |
Records cleanup alongside new-file emission. |
tsc/testdata/baselines/reference/tscWatch/commandLineWatch/watch-deletes-outputs-of-deleted-source-file-in-outDir.js |
Adds baseline coverage for JavaScript, declarations, maps, and incremental rebuilds. |
Review effort: Balanced
Comment on lines
+799
to
+800
| if emitted.alsoInput { | ||
| return |
Comment on lines
+567
to
+568
| key := w.caseSensitivity().PathKey(output.AsPath()) | ||
| w.emittedFiles.Store(key, emittedFile{output: output, source: sourceFile.PathKey(), alsoInput: inputs.Has(key)}) |
| } | ||
| } | ||
| output := emitted.output | ||
| if !fs.FileExists(output) { |
| return | ||
| } | ||
| if err := fs.Remove(output.AsPath()); err != nil && !errors.Is(err, vfs.ErrNotExist) { | ||
| w.reportDiagnostic(ast.NewCompilerDiagnostic(diagnostics.Failed_to_delete_file_0, output)) |
Author
|
@microsoft-github-policy-service agree |
This branch has not been deployed
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.


Fixes #16057
Problem
When a source file is deleted (or moved/excluded) while running
tsc --watch, the outputs previously emitted for it (.js,.d.ts,.js.map,.d.ts.map, ...) were left on disk forever. The watcher tracked emitted files inWatcher.emittedFiles, butpruneEmittedFiles()only removed entries from that in-memory map when the source disappeared from the program — the files themselves were never deleted.Fix (
tsc/internal/execute/watcher.go)emittedFilesnow stores anemittedFilerecord: the original output path (original casing, since aPathKeymust not be used as aRootedPath), the source path key, and whether the output was already a project input when emitted (alsoInput).pruneEmittedFiles()now removes the output from disk via the newdeleteEmittedOutput()helper when its source is gone from the program..jsthat is also anallowJsinput listed in "files", or an externally supplied.d.ts) and config files are never deleted;vfs.ErrNotExist);Failed_to_delete_file_0) instead of crashing the watch cycle.noEmittoggled on, oroutDir/declarationDirchanged) are only untracked, never deleted..tsbuildinforecorded at a previous location is removed.Tests
tsc/internal/execute/tsctests/watch_output_clean_test.gowith 7 regression tests (outDir+declaration+maps+incremental deletion, no-outDir deletion, input-collision protection via TS5055, "files"-listed source deletion, already-deleted output (no crash), delete-then-recreate, move/rename cleanup). All failed before the fix (stale outputs kept) and pass after.TestWatchscenario "watch deletes outputs of deleted source file in outDir"; updated 8 existing watch baselines whose scenarios delete/move sources (outputs now correctly show*deleted*).Validation
go build ./internal/execute/...,go vet ./internal/execute/...,gofmtcleango test ./internal/execute/... ./cmd/... ./internal/api/... ./internal/project/... -count=1— all passgo test -race -run 'TestWatcher' ./internal/execute/tsctests/— passDiff: 12 files changed, 820 insertions(+), 11 deletions(-).
Written with assistance from Mistral Vibe (AI), reviewed and tested by me.