Skip to content

fix(watch): delete stale output files when a source file is removed in tsc --watch - #65626

Open
DeviousCardi wants to merge 1 commit into
microsoft:mainfrom
DeviousCardi:fix/16057-watch-clean-deleted-source-outputs
Open

DeviousCardi wants to merge 1 commit into
microsoft:mainfrom
DeviousCardi:fix/16057-watch-clean-deleted-source-outputs

Conversation

@DeviousCardi

Copy link
Copy Markdown

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 in Watcher.emittedFiles, but pruneEmittedFiles() 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)

  • emittedFiles now stores an emittedFile record: the original output path (original casing, since a PathKey must not be used as a RootedPath), 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 new deleteEmittedOutput() helper when its source is gone from the program.
  • Safety guards so nothing that should be kept is deleted and nothing crashes:
    • 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 (vfs.ErrNotExist);
    • any other removal failure is reported as a diagnostic (Failed_to_delete_file_0) instead of crashing the watch cycle.
  • Outputs whose source still exists (e.g. noEmit toggled on, or outDir/declarationDir changed) are only untracked, never deleted.
  • 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 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.
  • New baselined TestWatch scenario "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/..., gofmt clean
  • go test ./internal/execute/... ./cmd/... ./internal/api/... ./internal/project/... -count=1 — all pass
  • go test -race -run 'TestWatcher' ./internal/execute/tsctests/ — pass

Diff: 12 files changed, 820 insertions(+), 11 deletions(-).

Written with assistance from Mistral Vibe (AI), reviewed and tested by me.

…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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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))
@DeviousCardi

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Typescript Watch - Cleaning Target Files on Source Deletion

3 participants