Skip to content

fix: type check virtual modules, and builds after the TypeScript process is replaced - #1741

Merged
johnnyreilly merged 12 commits into
nextfrom
ccr-e5123452-iefcal
Nov 29, 2026
Merged

johnnyreilly merged 12 commits into
nextfrom
ccr-e5123452-iefcal

Conversation

@johnnyreilly

@johnnyreilly johnnyreilly commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Fixes the five cases #1734 left open (closes #1735, closes #1736, closes #1737, closes #1738, closes #1739), and two problems with files TypeScript knows under a made-up name, such as a .vue file's script (section 6).

Each was reproduced before being fixed, except where a section says otherwise. #1736's main effect only showed once #1735 was out of the way, as its issue predicted.

Split at 89b1b78. Until then this PR also gave a .vue file's template a TypeScript name of its own, to stop the script and template overwriting each other. That part is now #1743, stacked on this branch. Three of the first four review rounds here found problems in it, two of them in the fix of the round before. What the reviews found in the remaining code is in section 7.

Not fixed here, and on next too: #1742 (TypeScript can find a deleted file again in a later rebuild).

What changes

1. #1735: false "Cannot find module" for a virtual module

A file TypeScript can only get from ts-loader isn't there when a file that imports it is compiled first. That importer's failed import isn't in its import list, so the end-of-build recheck never came back to it.

ts-loader now remembers which files were last checked with a "Cannot find module" error (TS2307, TS2792, and TS2882 for a side-effect import). When a file that isn't on disk is layered for the first time, the recheck drops those files' import lists, so they are found as dependants of the new file and checked again.

Only an import that was an error counts. An import answered by an ambient declaration (declare module '*.vue') also has no resolved file, and a first version of this fix re-checked those too. That broke sourceMapsShouldConsiderInputSourceMap on CI.

Testing with the real webpack-virtual-modules plugin showed a second failure in watch mode. webpack's watcher looks at the disk, so on the first rebuild it reports the virtual file in compiler.removedFiles, and ts-loader told TypeScript the file was deleted. A file is now marked virtual when it is compiled (webpack's file system has it, the disk doesn't). A "removed" report for such a file is dropped while webpack's file system still has it, and passed on once it doesn't.

2. #1736: a restarted process loses a file webpack's file system can't stat

3. #1737: webpack 4, deleted x.ts unnoticed beside a file x

Each layer now records webpack's own name for its file (resourcePath), so the missing-file check asks about exactly that file. The check that stripped .ts/.tsx from every name is gone. Removed and changed files are matched to layers by the same record, so a layer with the suffix of appendTsxSuffixTo is found too.

4. #1738: webpack 4, a rebuild that deletes a file but compiles nothing

The deleted-file check also runs at the start of the end-of-build recheck. When the process has just been replaced there is no snapshot, so the recheck now opens the project itself, anchored on a file from the config, or on a file compiled outside the tsconfig if the config lists none that exists.

This also covers webpack 5: when the process dies during the recheck, the next build reports the errors that build missed even if it compiles no TypeScript file. Before, they waited for a build that did.

5. #1739: a replaced process forgets files compiled outside the tsconfig

The replacement keeps orphanRootFileNames, minus files that have gone. The synthetic program is created again from them by the next such file compiled, or by the end-of-build recheck if none is. A root that is deleted later is dropped too, and the recheck reconfigures the program to match.

6. Names only TypeScript knows (not in the issues; both are on next)

A file without a TypeScript extension, or one matched by appendTsSuffixTo, is known to TypeScript under a made-up name: App.vue.ts for App.vue.

That name was handed to webpack to watch, as a dependency of the file's importers and as an unbundled project file. It is no file, so webpack's watcher reported it removed after the first build. That started a rebuild nobody asked for, and ts-loader passed the removal on to TypeScript, which dropped the file from the project: an import of it then failed with TS2307, or fell back to a *.vue shim's type, until the file was next compiled. webpack is now given the file the layer was compiled from.

With appendTsSuffixTo and vue-loader, a .vue script change wasn't compiled again. Each module's own dependency was registered under the suffixed name too, replacing its dependency on the real file. On a change to App.vue, webpack rebuilt the vue-loader module but not the script's module. The module now depends on resourcePath, as ts-loader 9 did.

7. Review findings in this code

Finding Reproduced? Fix
The owed recheck is dropped when the tsconfig lists no existing file Yes The recheck picks the file it opens the project with before it takes what changed, and falls back to a file compiled outside the tsconfig.
A virtual module outside the program counts as created on every rebuild Not run; the code path is gone A dropped "removed" report is neither removed nor changed, and only files ts-loader compiled can be marked virtual.
A case-only rename is no longer a removal Not run (needs macOS or Windows) A file on disk is never marked virtual, so its removal is passed on as on next.
A virtual module that really is removed is never reported Not on the reviewed commit. Afterwards: its layer is dropped and an importer gets TS2307. The report is passed on once webpack's file system no longer has the file.
Asking webpack's file system about a removed file poisons its cache Yes, by me: deleting a file and its import, then putting both back, gave webpack's own Module not found It is only asked about files known to be virtual, and a "no such file" answer is purged. No purge otherwise: webpack-virtual-modules replaces purge with one that empties the whole cache (confirmed in its source). The webpack 4 missing-file check purges such an answer too (not run).
After a restart, a deleted file stays a root of the synthetic program The stale root, yes. The TS6053 it was expected to cause, no. A root the process has no layer for is dropped once it is gone from disk.
A deleted file outside the tsconfig is dropped as a root but stays in the synthetic program; put back, it is compiled against a program that lacks it (fifth review, the one to settle before merge) Yes: the restored file was compiled, but left out of the program, so it was not found as a dependant when a type it imports changed and its new error went unreported The recheck reconfigures the synthetic program whenever it is stale, not only with config overrides. A file compiled again after its root was dropped is added as a root again.
The recheck can open the project with a file the new process has in no program (predates this PR) Not run A file of the synthetic program isn't chosen while the process has no such program.
One more disk read per compiled file Measured, below A file whose text TypeScript already holds, without this process ever having given it a layer, isn't read again. A rebuild still reads each recompiled file: about 4 ms per 1,200 files, so left as it is.
A side-effect import of a virtual module keeps its error Yes: TS2882 stayed TS2882 counts as a missing import.
A stat error with no code no longer means "missing" Not run It means "missing" again.
Arrow-function constants in the test plugin - Function declarations.

Proof

Script runs in watch mode against a build of next and this branch. webpack 4 is 4.47 in a separate install; webpack 5 is the repo's own. The numbered rows and the review rows were re-run at 5d4f461.

Scenario next This branch
#1735 webpack 5, webpack-virtual-modules: app.ts imports a virtual generated.ts TS2307 on first build no error
#1735 same, first rebuild (watcher reports the virtual file removed) TS2307 no error
#1735 same, with import './generated' for side effects TS2882 no error (last run at 89b1b78)
#1735 same on webpack 4 TS2307 on first build no error
#1736 webpack 5: process killed, only app.ts recompiled, app.ts misuses the virtual file's type TS2307 TS2322 (the type is known)
#1737 webpack 4: schema.graphql.ts beside schema.graphql, delete the .ts no TS2307 TS2307
#1738 webpack 4: delete util.ts, rebuild compiles only index.js no error TS2307 for other.ts
#1738 then recreate util.ts with another type no error TS2322 for other.ts
#1739 webpack 5: process killed, app.ts changes a type the orphan extra.ts uses through mid.ts no error TS2322 for extra.ts
#1739 webpack 4: same, with a compiled file deleted in place of the kill no error TS2322 for extra.ts
Review tsconfig lists only a missing file; process dies in the recheck; next build compiles only other.js error not reported TS2322 for top.ts
Review extra.ts, outside the tsconfig, is deleted and put back; then shared.ts, which it imports a type from, changes that type (89b1b78) no error TS2322 for extra.ts
6 real vue-loader 17 and Vue 3 with appendTsSuffixTo: a type error is put in a .vue file's script the script isn't recompiled; nothing reported TS2322 (last run before the split, at 879ec16)
6 same: rebuilds after the first build with nothing changed one none (last run before the split, at 879ec16)

#1734's own webpack 4 scenario (delete b.ts, then recreate it) still gives TS2307, then no error.

Not reproduced: #1736's second effect, the process being replaced on every webpack 4 rebuild for a file that can't be stat'd. With webpack-virtual-modules on webpack 4 the process was kept on next as well, because that plugin answers statSync. The code path that would cause it is gone, but I have no failing case for it.

Speed

npm run benchmark at 1,200 files, 4 iterations (before the multiplier), at db7501b, before the split. Not re-run since. A is this branch.

Scenario transpileOnly vs next vs 736bf8f (before the disk-read fix)
Cold build false -0.4% -0.6%
Incremental rebuild (hub touch) false +4.5% +1.1%
Cold build true +0.4% +1.5%
Cold build (all orphan files) false -1.5% -0.2%
Cold build (declaration) false +1.2% -0.8%

Nothing I can tell apart from noise: transpileOnly, which runs none of the changed code, moves as much as the rest. The fifth review asked whether the +4.5% on hub-touch rebuilds is the disk read each recompiled file still gets. Reading the fixture's 1,200 files that way takes about 4 ms once the OS has them cached, against about 1,400 ms for that rebuild, so no. CI's benchmark comments cover all three platforms.

Known limits

Testing

At 5d4f461:

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

…ess is replaced

Fixes the cases left open by #1734.

- #1735: a file TypeScript can only get from ts-loader (a virtual module)
  isn't there for an importer compiled before it. The importer's failed
  import is now remembered, and the end-of-build recheck checks it again
  once such a file has been layered. A file webpack's watcher reports as
  removed while its input file system still has it is no longer treated
  as removed.
- #1736: a layer is only dropped as "missing" when webpack's file system
  had the file and now says it is absent; a failed stat that isn't
  ENOENT/ENOTDIR no longer counts. Whether a restarted process must be
  sent a file again is decided by what is on disk, which is what the
  process reads, not by webpack's input file system.
- #1737: each layer records webpack's own name for its file, so a
  deleted `x.ts` is noticed with a file `x` beside it.
- #1738: the webpack 4 deleted-file check also runs before the
  end-of-build recheck, which opens the project itself when the process
  has just been replaced.
- #1739: a replaced process keeps the files compiled outside the
  tsconfig as roots, and their program is made again by the next such
  file compiled or by the end-of-build recheck.

Closes #1735, closes #1736, closes #1737, closes #1738, closes #1739

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv
A re-check of every importer whose import had no resolved file also took
in imports answered by an ambient declaration (declare module '*.vue'),
and swapped the declared type for the layered file's.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Benchmark (Ubuntu)

Scenario transpileOnly PR branch median (ms) base branch median (ms) Δ vs base branch
Cold build false 876.2 881.3 -0.6%
Incremental rebuild (leaf touch) false 52.6 52.0 +1.0%
Incremental rebuild (hub touch) false 314.3 310.5 +1.2%
Cold build true 372.1 372.1 +0.0%
Incremental rebuild (leaf touch) true 32.0 32.1 -0.4%
Incremental rebuild (hub touch) true 33.2 33.5 -0.9%
Cold build (all orphan files) false 950.1 950.4 -0.0%
Incremental rebuild (hub touch, all orphan files) false 358.8 350.6 +2.3%
Cold build (declaration) false 988.4 974.4 +1.4%
Incremental rebuild (leaf touch, declaration) false 93.9 93.1 +0.9%

PR branch = /home/runner/work/ts-loader/ts-loader, base branch = /home/runner/work/ts-loader/ts-loader-main. 2 warmup + 10 measured iterations per scenario, median reported. Report-only - no threshold fails this check.

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Benchmark (macOS)

Scenario transpileOnly PR branch median (ms) base branch median (ms) Δ vs base branch
Cold build false 511.9 509.0 +0.6%
Incremental rebuild (leaf touch) false 49.9 49.5 +0.9%
Incremental rebuild (hub touch) false 170.0 151.0 +12.6%
Cold build true 212.1 222.3 -4.6%
Incremental rebuild (leaf touch) true 50.2 50.4 -0.4%
Incremental rebuild (hub touch) true 53.8 54.1 -0.4%
Cold build (all orphan files) false 855.6 857.9 -0.3%
Incremental rebuild (hub touch, all orphan files) false 303.1 285.9 +6.0%
Cold build (declaration) false 713.9 714.8 -0.1%
Incremental rebuild (leaf touch, declaration) false 136.7 130.7 +4.5%

PR branch = /Users/runner/work/ts-loader/ts-loader, base branch = /Users/runner/work/ts-loader/ts-loader-main. 2 warmup + 10 measured iterations per scenario, median reported. Report-only - no threshold fails this check.

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Benchmark (Windows)

Scenario transpileOnly PR branch median (ms) base branch median (ms) Δ vs base branch
Cold build false 694.1 694.5 -0.1%
Incremental rebuild (leaf touch) false 42.8 43.6 -1.8%
Incremental rebuild (hub touch) false 260.8 245.1 +6.4%
Cold build true 274.1 272.5 +0.6%
Incremental rebuild (leaf touch) true 25.9 26.2 -1.0%
Incremental rebuild (hub touch) true 28.4 27.3 +3.9%
Cold build (all orphan files) false 702.9 699.8 +0.4%
Incremental rebuild (hub touch, all orphan files) false 279.6 265.1 +5.5%
Cold build (declaration) false 807.3 797.9 +1.2%
Incremental rebuild (leaf touch, declaration) false 159.5 155.4 +2.6%

PR branch = C:\source\ts-loader, base branch = C:\source\ts-loader-main. 2 warmup + 10 measured iterations per scenario, median reported. Report-only - no threshold fails this check.

…s only

- vue-loader sends the script and the compiled template of a .vue file
  through ts-loader as the same file. Both were given to TypeScript under
  one name, so whichever was compiled last was what an import of the file
  resolved to, and the template has no default export (TS1192). The
  script keeps the file's name; any other block gets its own.
- A name only TypeScript knows (a suffix added by appendTsSuffixTo or for
  a file without a TypeScript extension) was handed to webpack to watch.
  It names no file, so webpack reported it removed after the first build:
  an extra rebuild, and TypeScript told to forget the file. webpack is now
  given the file the layer was compiled from.
- A module depends on its own file, not on the suffixed name, so it is
  rebuilt when the file changes.
- Removed files are matched to layers by the name webpack compiled them
  under, since one file can now have several layers.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv
@johnnyreilly johnnyreilly changed the title fix: type check virtual modules, and builds after the TypeScript process is replaced fix: type check virtual modules and Vue files reliably, and builds after the TypeScript process is replaced Nov 29, 2026

@johnnyreilly johnnyreilly left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Review of 736bf8f: five findings worth fixing or checking before merge, plus two nits, as inline comments. Please address all of them.

All come from reading the diff; none was reproduced by running the code. The declaration: true one is the least certain.

  1. A deleted .vue file's template block stays in the program (src/typeScriptApi.ts).
  2. The owed recheck can be dropped after a process restart in an all-orphan project (src/typeScriptApi.ts).
  3. A virtual module outside the TS program is treated as newly created on every rebuild (src/index.ts).
  4. A case-only rename on macOS/Windows is no longer seen as a removal (src/index.ts, same comment as 3).
  5. declaration: true probably emits a stray App.vue.template.d.ts (src/typeScriptApi.ts).
  6. Nit: one extra synchronous disk read per compiled file (src/typeScriptApi.ts).
  7. Nit: arrow-function constants for named helpers in virtual-files-plugin.js.

Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
*/
function toApiFacingFileName(fileName: string) {
function toApiFacingFileName(fileName: string, blockName?: string) {
if (blockName !== undefined) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A deleted .vue file's template block can stay in the program.

The template is now layered as App.vue.template.ts, but the watcher notifications in updateSnapshot (mapApiFacingFileNames) still map App.vue only to App.vue.ts, and forgetRemovedFileLayers drops just ts-loader's own record.

Scenario: webpack 5 watch with vue-loader, App.vue has a TS template block with a type error and is then deleted. The API gets deleted: [App.vue.ts] only, so App.vue.template.ts stays a root and its pendingDiagnostics entry is re-reported every build. toWatchableFileName no longer finds its layer, so the made-up name is added to compilation.fileDependencies, which brings back the spurious "removed" rebuilds this PR removes.

Suggested fix: when a webpack name is removed or changed, also notify the API names of every layer whose resourcePath matches, and drop them from orphanRootFileNames.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
// to the synthetic program, which it doesn't have yet.
const anchorFileName =
changedFileNames.values().next().value ??
(hadSnapshot ? changedFileNames.values().next().value : undefined) ??

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The owed recheck can be dropped after a compiler-process restart when the tsconfig lists no existing file.

recheckAllTransitiveDependants now runs without a snapshot. It empties changedFilesThisBuild and sets watcherChangedFiles to an empty set, then (with no snapshot) ignores the changed files as an anchor and returns when findExistingProjectFile finds nothing.

Scenario: an all-orphan project (tsconfig files/include match nothing on disk). The process dies during type checking and index.ts restores changedFilesThisBuild. The next build compiles no TS: the sets are consumed, the anchor is undefined, and the function returns. Later builds see both sets empty and skip, so dependants keep stale errors. Before this change the sets survived until a build that compiled a file.

Suggested fix: pick the anchor before consuming the sets, or restore them on that return, or fall back to a changed file.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/index.ts Outdated
const modifiedFileNames = [...(compiler.modifiedFiles ?? [])];
const removedFileNames: string[] = [];
for (const fileName of compiler.removedFiles ?? []) {
(typeScriptInstance.inputFiles.isFile(fileName) === true

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Two problems with reclassifying every "removed" name that webpack's input file system still has as a file:

1. A virtual module outside the TypeScript program is treated as newly created on every rebuild. The name goes into pendingChangedFiles, and findCreatedFiles counts every .js/.ts name outside the last known file list as created.

Scenario: a plugin provides a virtual routes.js that ts-loader never compiles, and webpack's watcher reports it removed on each rebuild. Every rebuild now clears the whole directImportsCache and sends created: [routes.js] for a file not on disk, so the recheck re-resolves imports for every project file. Even a virtual file that is in the program is added to watcherChangedFiles, so its dependants are rechecked on every rebuild.

Suggested fix: keep such files out of findCreatedFiles (for example, only reclassify names that have a layer), or skip names that aren't on disk there.

2. A case-only rename on a case-insensitive file system is no longer seen as a removal.

Scenario: on macOS or Windows, rename Foo.ts to foo.ts in watch mode. The watcher reports Foo.ts removed and foo.ts changed. isFile('Foo.ts') is true, so no deleted notification is sent and the old-cased layer isn't forgotten; casing errors (TS1149/TS1261) or the old name may linger.

Suggested fix: check what the API receives for this rename on macOS/Windows, and consider reclassifying only names with a layer whose wasFile is false. That narrowing would cover both problems.

Both found by reading the diff; not reproduced.


Generated by Claude Code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Both fixed in db7501b, but not with the suggested narrowing, which would have undone the fix for webpack-virtual-modules.

Why not "only names with a layer whose wasFile is false": that plugin answers statSync, so its files have wasFile: true. They are exactly the files the watcher reports as removed, and they would have gone back to being treated as deleted.

What it does now:

  • A file is marked virtual when it is compiled: webpack's file system has it, and the disk doesn't. The compile hook drops a "removed" report for those names only.
  • A dropped report is neither removed nor changed. So it isn't in pendingChangedFiles (no created, no cleared directImportsCache) or in watcherChangedFiles (no recheck of its dependants). webpack rebuilds a virtual module whose contents changed, which layers it again.
  • A virtual routes.js that ts-loader never compiles isn't marked, so it is handled as on next.
  • A case-only rename: Foo.ts is on disk under either spelling, so it is never marked virtual and its removal is passed on as before this PR. I have not run this on macOS or Windows.

A bug this turned up: the version you reviewed asked webpack's file system about every removed name. For a file that really was deleted, that left "no such file" in webpack's stat cache. Deleting a file, dropping its import, then putting both back gave Module not found: Can't resolve './component.vue' from webpack itself. I reproduced that on 736bf8f; it is gone now that nothing is asked at removal time.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
function toApiFacingFileName(fileName: string, blockName?: string) {
if (blockName !== undefined) {
const extension = /\.tsx?$/.exec(fileName)?.[0] ?? '.ts';
return `${fileName.replace(/\.tsx?$/, '')}.${blockName}${extension}`;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

With declaration on, a .vue file's compiled template probably gets its own declaration asset.

The template is now a separate program file (App.vue.template.ts), and the end-of-build recheck emits declarations for every project file that matches the TS extension pattern (staleFileNames covers all of projectFileNames).

Scenario: vue-loader with <script lang="ts">, a TS-compiled template and declaration: true. The build output would gain App.vue.template.d.ts beside App.vue.d.ts.

This one is inferred and the least certain of the set. Suggested fix: check the output with declaration: true and vue-loader, and leave block layers out of recordDeclarationFiles if confirmed.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
// Only a file that was there can be seen to have gone.
wasFile: typeScriptInstance.inputFiles.isFile(resourcePath) === true,
};
const diskContents = readDiskFile(fileName);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nit: every compiled file now costs one extra synchronous disk read.

layerFileContents calls readDiskFile (fs.readFileSync) where it used webpack's cached input file system, which had just read the same file. A cold build of a 4,800-file project does 4,800 extra uncached reads on the loader thread, and each rebuild does one per recompiled file.

Suggested fix: only read the disk when webpack's file system isn't the real one or the file isn't a real file there, or accept the cost after a benchmark run at a larger file count than the default 300.


Generated by Claude Code

])
);
const disk = compiler.inputFileSystem;
const stats = contents => ({

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nit: AGENTS.md prefers function declarations for named helpers ("Use function name(...) {} instead of const name = (...) => {} when the function needs a name"). This applies to stats here and to answer at line 31: function stats(contents) {} and function answer(method, result) {}.


Generated by Claude Code

… config file, and virtual files

- A removed or changed file is passed on to TypeScript under the name of
  every layer it has, so a deleted .vue file's template goes with its
  script. A removed layer is no longer a root of the synthetic program,
  and a name ts-loader made up is hidden from TypeScript, which otherwise
  finds the layer again when it next lists the directory.
- A block of a file gets no declaration file of its own.
- The end-of-build recheck picks the file it opens the project with
  before it takes what changed, and falls back to a file compiled outside
  the tsconfig, so a project whose tsconfig lists no existing file doesn't
  lose a recheck it is owed.
- A file is known to be virtual from when it was compiled. webpack's file
  system is no longer asked about files reported removed: that left "no
  such file" in its cache, and a file put back could not be resolved.
  Such a file is neither removed nor changed, so it no longer counts as
  created or has its dependants rechecked on every rebuild, and a rename
  that only changes case is a removal again.
- A file whose text TypeScript already read from disk isn't read from
  disk a second time.
- Test plugin: function declarations for named helpers.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

@johnnyreilly johnnyreilly left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Re-review at db7501b: four new findings worth fixing and one nit, as inline comments. Please address all of them. None of the seven findings from the review of 736bf8f was raised again.

All come from reading the diff; none was reproduced by running the code.

  1. A deleted real file is hidden from TypeScript and can stay hidden after it is put back (src/typeScriptApi.ts).
  2. A virtual module that really is removed is never reported to TypeScript on webpack 5 (src/index.ts).
  3. After a process restart, a deleted file can stay a root of the synthetic program (src/typeScriptApi.ts).
  4. A file whose loader output differs from disk can lose its layer on a later restart (src/typeScriptApi.ts).
  5. Nit: a removed .vue file's layers stay visible for the whole build that reports the removal (src/typeScriptApi.ts).

Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
// be hidden: the file can only come back by being compiled, which
// layers it anew. A real file's name can't, or the API would miss it
// being put back.
if (!fs.existsSync(layeredFile.fileName)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A deleted real file is hidden from TypeScript, and can stay hidden after it is put back.

!fs.existsSync(layeredFile.fileName) is true for any just-deleted ordinary .ts file, not only for made-up names like foo.vue.ts. So the name goes into unsentRemovedLayerPaths, which is what the comment just above (and the PR description's last known limit) says must not happen for a real file's name.

Scenario: a.ts imports b.ts. Delete b.ts: its name is sent as a removed path at the end of that build. Restore b.ts: webpack rebuilds a.ts first, while b.ts is still hidden, so a.ts gets TS2307. b.ts is then compiled and layered again, but it is on disk so layerOnlyFileAdded stays false, a.ts's cached import list lacks b.ts, and a.ts is not rechecked. The false TS2307 stays until a.ts is next edited.

Suggested fix: hide only made-up names, e.g. when layeredFile.fileName differs from layeredFile.resourcePath, and add a delete-then-restore watch test.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/index.ts
// isn't, and it hasn't changed either: webpack rebuilds it if it has.
// Known from when it was compiled: asking webpack's file system about
// a file that has gone leaves "no such file" in its cache.
const removedFileNames = [...(compiler.removedFiles ?? [])].filter(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A virtual or in-memory module that really is removed is never reported to TypeScript under webpack 5.

The compile hook drops every removed file found in virtualResourcePaths, and an entry only leaves that set when the same file is compiled again.

Scenario: a virtual-modules plugin stops providing generated.ts, or the whole project lives in memfs and a file is deleted. The watcher reports it removed, the filter discards that, and forgetRemovedFileLayers never runs for it. Its layer stays in layeredFiles/fileLayers and is resent after every process restart, so importers keep type-checking against the deleted module with no TS2307.

Suggested fix: let a removal through when inputFiles.isFile(fileName) === false, or prune the set when the input file system no longer has the file. (The description notes that asking webpack's file system at removal time poisoned its cache for really-deleted files, so the check needs to avoid that path.)

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts
// The synthetic program is made again from these, by the next orphan
// file compiled or by the end-of-build recheck. A root that is gone
// would be an error in it.
orphanRootFileNames: new Map(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

After the TypeScript process is replaced, a deleted file can stay a root of the synthetic program.

orphanRootFileNames is now carried across the restart, but forgetRemovedFileLayers only prunes roots it finds in layeredFiles, which after a restart holds only layers that differ from disk. forgetMissingFileLayers never prunes roots at all.

Scenario: extra.ts is compiled by webpack but not in the tsconfig, with contents equal to disk. The process dies and is replaced: extra.ts stays a root but is not in the new layeredFiles. Delete extra.ts and drop its import. The loop over layeredFiles never sees it, so createOrphanProgram or the next reconfigure lists a missing root and the build reports a file-not-found error (TS6053) for a file nobody uses. Before this PR the root map was reset on restart.

Suggested fix: prune orphanRootFileNames by removed resource path independently of layeredFiles, and also in forgetMissingFileLayers.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts
);
// Text the API holds without having been given it, it read from disk:
// reading every compiled file again here would only repeat that.
const diskContents =

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A file whose loader output differs from disk can lose its layer on a later process restart.

layerFileContents assumes that text the snapshot holds for a file absent from layeredFiles was read from disk. But forgetRemovedFileLayers deletes the layeredFiles entry while the API keeps the layer when the name still exists on disk.

Scenario: foo.ts is transformed by an earlier loader, so its layer differs from disk. The watcher reports foo.ts removed although it exists (deleted and recreated between builds). The layer is dropped from layeredFiles and fileLayers, but the API still has the transformed text. The rebuild compiles foo.ts with the same contents: snapshotIsCurrent is true, diskContents is set to contents, and fileLayers.delete runs. After the next restart the new process reads the untransformed disk text and reports wrong type errors for foo.ts's importers.

Suggested fix: take the shortcut only for a file never layered in this process, or read the disk whenever the file was ever layered.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
removedFileNames,
fileName,
);
const hideRemovedLayers =

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nit, worth checking before changing anything: a removed .vue file's layers stay visible to TypeScript for the whole build that reports the removal.

hideRemovedLayers is false for the update carrying the removal, so the hidden paths only go out with a later update, which may be the end-of-build recheck, after importers were compiled.

Scenario: component.vue is deleted while app.ts still imports it. app.ts is recompiled in the first snapshot update of the rebuild, where the component.vue.ts layer is still visible, so it gets no TS2307. The recheck then hides the layer, but the watcher's name component.vue doesn't match the import component.vue.ts, so app.ts may not be rechecked and its diagnostics stay clean until its next compile.

Suggested check: confirm whether an importer compiled in the removal build is rechecked once the layer is hidden; if not, send a follow-up update before compiling it.


Generated by Claude Code

…ual files and roots

- Only a layer whose name ts-loader made up is hidden from TypeScript
  when its file is removed. The check was "not on disk", which is true of
  any file that was just deleted.
- A virtual file the watcher reports removed is passed on once webpack's
  file system no longer has it. That file system is asked afresh, and a
  "no such file" answer is not left in its cache.
- A root of the synthetic program that the process has no layer for is
  dropped once it is gone from disk, so a file deleted after a restart
  doesn't stay a root.
- The disk read is only skipped for a file this process was never given
  a layer for: TypeScript keeps the layer of a file ts-loader has dropped
  its own record of.
- Removed layers are hidden before the end-of-build recheck, which checks
  their importers again: one compiled earlier in that build saw the layer.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

@johnnyreilly johnnyreilly left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Third review, at c796671: two new findings worth fixing, two that predate this PR, and two nits. None of the earlier rounds' findings was raised again.

All come from reading the code; none was reproduced by running it.

Please address 1, 2, 3, 5 and 6. Number 4 is the known limit the description already lists and need not hold this PR.

  1. A Vue block that stops being compiled leaves its layer and errors behind (inline, line 291).
  2. In the build that deletes a .vue file, an importer can still be made to watch the made-up .vue.ts name (inline, line 2750).
  3. Predates this PR: with transpileOnly and isolatedDeclarations, a template block's declaration is emitted to the script's asset path (file comment, line 679).
  4. Predates this PR: a removed file layered under its real name can come back as a ghost project file (inline, line 1452).
  5. Nit: a template block's errors change file name between builds (file comment, line 2447).
  6. Nit: hiddenLayerPaths is cleared before an early return on config errors (inline, line 2257).

Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
// `apiFileName` for API calls; `fileName` for anything shown to webpack/the user.
const apiFileName = toApiFacingFileName(fileName);
const blockName = getVueBlockName(loaderContext.resourceQuery);
const apiFileName = toApiFacingFileName(fileName, blockName);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A Vue block that stops being compiled leaves its layer, and its type errors, behind for the rest of the watch session.

Each non-script block now gets its own name (component.vue.template.ts) and its own pendingDiagnostics entry, but a block layer is only dropped when the whole .vue file is removed (forgetRemovedFileLayers/isMissing match on resourcePath).

Scenario: watch mode with vue-loader, component.vue has <script lang="ts"> and a template with a type error. The user deletes the <template> (or drops lang="ts", or removes a custom block so indexes shift). webpack no longer sends the type=template request, but component.vue.template.ts stays in layeredFiles, in the API and in the program, so the end-of-build prune keeps its entry and the stale error is reported on every later build until the process restarts.

Suggested fix: when a .vue resource is recompiled, drop and hide block layers of that resourcePath that were not compiled again in the build, along with their pendingDiagnostics and orphan-root entries.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts
* that only TypeScript knows (see toApiFacingFileName): watched, it is
* reported as removed on the next rebuild, and TypeScript told to forget it.
*/
function toWatchableFileName(instance: TSInstance, fileName: string): string {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In the build that deletes a .vue file, an importer can still be made to watch the made-up .vue.ts name.

toWatchableFileName looks the name up in layeredFiles, but forgetRemovedFileLayers has already deleted that entry in the first updateSnapshot of the build, while the layer is deliberately not hidden until a later update. So the import still resolves and the lookup misses.

Scenario: webpack 5 watch with appendTsSuffixTo: [/\.vue$/]. component.vue is deleted while app.ts still imports it. app.ts is the first file compiled; the layer is forgotten but still visible, so registerResolvedImportDependencies reports component.vue.ts and addDependency('.../component.vue.ts') registers a file that does not exist. webpack then reports it as removed and starts a rebuild of its own, which is the spurious-rebuild bug section 6 removes.

Suggested fix: also map names in unsentRemovedLayerPaths (or skip them as dependencies), or keep the resource path until the layer is hidden.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
// be hidden: the file can only come back by being compiled, which
// layers it anew. A real file's name can't, or the API would miss it
// being put back.
if (layerKey !== resolvedFilePathCache(layeredFile.resourcePath)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Predates this PR, and not a blocker: a removed file that was layered under its real name is never hidden from the API, so its layered text can come back as a ghost project file.

This is the other side of the previous round's fix (only made-up names are hidden now, so a restored real file is seen). The description already lists it as a known limit; recording a concrete scenario here.

Scenario: webpack 5 watch, gen.ts is transformed by an earlier loader, so its contents are layered, and the tsconfig has include: ["*.ts"]. gen.ts is deleted. A later full rescan (a new included file, a config change) lists the directory, finds the kept layer and puts gen.ts back in the program with its old text, so importers type-check against a file that no longer exists.

Possible follow-up: check whether the API offers a way to drop a real-named layer, or restart the process for this case as is done for webpack 4.

Found by reading the diff; not reproduced.


Generated by Claude Code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not changed in this PR, as the review allows. It is tracked as #1742, which has a reproduction of the same root cause on next: an importer's TS2307 for a deleted file goes away the next time the importer is checked.

The scenario here (a loader-changed gen.ts coming back after a full rescan) is not in that issue and I have not run it. The issue's notes list the same candidate fixes: un-hide on the watcher's report, replace the process as on webpack 4, or an API way to drop a layer.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
// A file compiled before its import's layer was hidden was checked with
// the layer still there.
const hiddenLayerPaths = [...typeScriptInstance.hiddenLayerPaths];
typeScriptInstance.hiddenLayerPaths.clear();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nit: importers of a deleted .vue file can miss their recheck when the overridden config has errors.

recheckAllTransitiveDependants copies and clears hiddenLayerPaths here and then returns early on overriddenConfig.errors.length > 0, so the list is lost.

Scenario: configFile overrides (for example compilerOptions in the loader options) currently fail to parse. component.vue is deleted and its layers are hidden in the recheck's second update; the function returns on the config errors. After the config is fixed, the next recheck has an empty hiddenLayerPaths, so app.ts is not found as a dependant and keeps its pre-deletion diagnostics until it is next compiled.

Suggested fix: clear hiddenLayerPaths only after the early returns, next to where the copy is used.


Generated by Claude Code

Comment thread src/typeScriptApi.ts

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Two findings on lines of this file that the diff doesn't touch, so they are attached to the file.

Line 679 (recordTranspileOnlyDeclarationFile), predates this PR: with transpileOnly and isolatedDeclarations, a Vue template block's declaration is emitted to the same asset path as the script's.

getTranspileOnlyEmit passes the block's own name to transpileDeclaration, but recordTranspileOnlyDeclarationFile computes the output path from the shared fileName. This PR excludes blocks from declarations only in the type-checking path (isBlock in the recheck).

Scenario: transpileOnly: true, declaration: true, isolatedDeclarations: true, vue-loader with a TypeScript script and template. Both requests call loaderContext.emitFile('component.vue.d.ts', ...) with different text, so webpack 5 reports "Conflict: Multiple assets emit different content to the same filename", or the template's declarations replace the script's.

Suggested fix: skip recordTranspileOnlyDeclarationFile when blockName !== undefined.

Line 2447 (instance.pendingDiagnostics.set(...)), nit: a template block's errors change the file they are reported under between builds.

The loader-time entry is built with fileName (component.vue.ts), but the end-of-build recheck rebuilds the same entry from the diagnostic's own name, the made-up component.vue.template.ts.

Scenario: watch mode, a template block with a type error imports types.ts. Build 1 reports the error at component.vue.ts. types.ts changes, the block is rechecked as a dependant without being recompiled, and the same error is now reported at component.vue.template.ts, a file the user cannot open.

Suggested fix: in the recheck, report a block under the name its compile used (for example keep the previous entry's fileName, or derive it from layeredFiles.get(...).resourcePath).

Both found by reading the code; not reproduced.


Generated by Claude Code

…hed while one is removed

- A block that no module of the build asks for is forgotten like a
  removed file's: a .vue file that loses its template no longer keeps
  the template's layer and errors for the rest of the watch.
- A block's errors are reported under the name its compile gave them in
  the end-of-build recheck too, not under the block's made-up name.
- With transpileOnly and isolatedDeclarations, a block's declarations
  are not emitted: they went to the same asset path as the script's.
- The name of a layer whose file was removed is not handed to webpack to
  watch in the build before the layer is hidden.
- The importers of hidden layers stay owed a recheck when the recheck
  returns early on config errors.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

@johnnyreilly johnnyreilly left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fourth review, at 7eb8bd1: five new findings worth fixing and two nits, as inline comments. Please address all of them. None of the earlier rounds' findings was raised again.

All come from reading the diff; none was reproduced by running the code. Two rest on unchecked assumptions about other packages and should be confirmed first: number 2 (webpack's module concatenation) and number 6 (webpack-virtual-modules' purge).

  1. A template block's type errors can vanish for the rest of a watch session (src/typeScriptApi.ts:2594).
  2. Template errors may be dropped in builds with module concatenation (src/typeScriptApi.ts:2566).
  3. The end-of-build recheck can anchor on a block file that was just hidden (src/typeScriptApi.ts:2222).
  4. A side-effect import of a virtual module keeps a false TS2882 (src/typeScriptApi.ts:2618).
  5. On Windows, a removed layer's made-up name may still reach webpack's watcher (src/typeScriptApi.ts:2844).
  6. Nit: isFileNow may flush webpack's whole file-system cache on every rebuild (src/index.ts:386).
  7. Nit: a stat error without a code is treated as "can't tell" (src/index.ts:380).

Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
typeScriptInstance.unsentRemovedLayerPaths.add(
path.resolve(layeredFile.fileName),
);
instance.pendingDiagnostics.delete(layerKey);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A .vue template block's type errors can vanish for the rest of a watch session.

forgetBlocksWithoutModule drops a block's layer and its pendingDiagnostics entry whenever no module in compilation.modules is a request for it, on the assumption that the block will be compiled again if it comes back. webpack's memory cache restores an unchanged module without running loaders, so that recompile never happens.

Scenario: watch mode, component.vue has a template with a type error. Edit app.ts to stop importing component.vue, so the block module leaves the compilation and its layer and errors are deleted. Re-add the import: webpack restores the unchanged template module from cache, ts-loader is not called, and the template error is never reported again until component.vue itself is edited.

Suggested fix: only forget a block when its .vue file was rebuilt this build without requesting it, or keep the layer and diagnostics and only hide them while unrequested.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
return;
}
const requestedBlocks = new Set<string>();
compilation.modules.forEach(module => {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Template-block errors may be dropped in builds that use module concatenation. Not confirmed against webpack; please check before fixing.

forgetBlocksWithoutModule runs at processAssets/afterCompile, after ModuleConcatenationPlugin has replaced each concatenation root in compilation.modules with a ConcatenatedModule that has no resource. A block module that is such a root would not be seen as requested, so its layer and diagnostics would be deleted in the same build that compiled it.

Scenario: mode: 'production' with vue-loader. The App.vue?vue&type=template&ts=true module ends up as the root of a concatenated module. It is no longer in compilation.modules with a resource, so pendingDiagnostics.delete(layerKey) removes its type errors before reportPendingTypeScriptDiagnostics runs, and the build passes with a broken template.

Suggested fix: also walk ConcatenatedModule.modules/rootModule, or collect the requested blocks before optimisation (e.g. in finishModules).


Generated by Claude Code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Checked, and not changed: this one doesn't happen.

I ran a mode: 'production' build (webpack 5.101) with the stand-in block loaders and module: 'esnext', so that the script, the template and their importer are concatenated. compilation.modules at processAssets held one ConcatenatedModule of four modules and the NormalModules it was made from: concatenation takes the inner modules out of the chunks, not out of compilation.modules. The template block's module was found, and its type error was reported, on 7eb8bd1 as well as on the current head.

I had written the suggested walk over ConcatenatedModule.modules first and took it out again once the run showed it wasn't needed; forgetBlocksWithoutModule has a one-line comment saying why. Not checked on webpack 4, which has no resourceQuery-based vue-loader 17 setup to run it with here.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
const anchorFileName =
changedFileNames.values().next().value ??
findExistingProjectFile(typeScriptInstance, instance.resolvedFilePathCache);
(hadSnapshot ? changedFileNames.values().next().value : undefined) ??

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The end-of-build recheck can pick as its anchor a block file whose layer forgetBlocksWithoutModule has just hidden.

The anchor is the first entry of changedFilesThisBuild, which is carried over when an earlier recheck died, so it can name a block that this build no longer requests. openPrimaryProject then opens a file while the same update hides it via removedPaths.

Scenario: build N compiles component.vue's template and script; the compiler process dies during the recheck, so changedFilesThisBuild (component.vue.template.ts first) is restored. Before build N+1 the template is removed from component.vue. The script compile creates a snapshot, forgetBlocksWithoutModule hides component.vue.template.ts, and the recheck passes that hidden, non-existent file as openFiles. The API call fails or the recheck throws out of the processAssets hook.

Suggested fix: choose the anchor after filtering out names in unsentRemovedLayerPaths or missing from layeredFiles, or drop forgotten blocks from changedFilesThisBuild.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
}

/** TS2307 and TS2792: "Cannot find module". */
const missingImportDiagnosticCodes = [2307, 2792];

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A side-effect import of a virtual module keeps a false "cannot find module" error.

Only 2307 and 2792 are listed, so recordMissingImports never marks an importer whose error is TS2882 (side-effect import not found, reported under noUncheckedSideEffectImports). forgetImportsNewFilesMaySatisfy then leaves its stale import list in place and it is not rechecked.

Scenario: app.ts contains import './generated'; where generated.ts exists only in webpack's input file system and is compiled after app.ts. app.ts gets TS2882, is not added to filesWithMissingImports, is not found as a dependant of the newly layered file, and the error stays until app.ts is next edited.

Suggested fix: add 2882 to the list, and check whether other unresolved-import codes such as 2834/2835 apply.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
if (layeredFile !== undefined) {
return layeredFile.resourcePath;
}
return unsentRemovedLayerPaths.has(path.resolve(fileName))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

On Windows, a removed layer's made-up name may still be handed to webpack's watcher.

unsentRemovedLayerPaths.has(path.resolve(fileName)) is a plain string match, where the set holds webpack's spelling and fileName comes from the TypeScript API. AGENTS.md warns that those spellings can differ in drive-letter or path casing.

Scenario: Windows, C:\proj\component.vue is deleted and its layer C:\proj\component.vue.ts is added to unsentRemovedLayerPaths. An importer compiled before the layer is hidden still resolves it, and the API reports it as c:/proj/component.vue.ts. path.resolve gives c:\proj\component.vue.ts, the lookup misses, and the non-existent name is added as a file dependency. webpack then reports it removed and starts a spurious rebuild.

Suggested fix: key unsentRemovedLayerPaths by resolvedFilePathCache(...) or compare via toComparablePath.

Found by reading the diff; not reproduced (needs Windows).


Generated by Claude Code

Comment thread src/index.ts Outdated
return undefined;
isFile,
isFileNow(fileName) {
fileSystem.purge?.(fileName);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nit, needs confirming: with webpack-virtual-modules, every watch rebuild may now flush webpack's whole input-file-system cache.

isFileNow calls fileSystem.purge(fileName) for each virtual file the watcher reports as removed. As I recall, that plugin replaces purge with a wrapper that ignores its argument, purges everything and rewrites all virtual files. I have not checked this against the plugin's source.

Scenario if so: a project with 20 virtual .ts modules in watch mode. On each rebuild the compile hook calls isFileNow 20 times, each a full purge of the cached stat/readFile/readdir data plus a rewrite of all virtual files, and webpack then re-stats the whole project.

Suggested fix: confirm against webpack-virtual-modules; if it holds, stat without purging first and purge only when the answer is "not a file".


Generated by Claude Code

Comment thread src/index.ts Outdated
return fileSystem.statSync(fileName).isFile();
} catch (error) {
const code = (error as { code?: unknown } | null)?.code;
return code === 'ENOENT' || code === 'ENOTDIR' ? false : undefined;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nit: with a custom input file system, deleted files may no longer be noticed.

isFile returns undefined for any stat error whose code is not ENOENT or ENOTDIR, and both isMissing and the virtual-file filter treat undefined as "still there". The old code treated every stat failure as missing.

Scenario: webpack 4 watch with an in-memory or plugin-wrapped inputFileSystem whose statSync throws new Error('not found') without a code. A layered file is deleted, isFile returns undefined, isMissing is false, the TypeScript process is not replaced, and imports of the deleted file keep resolving to its stale layer.

Suggested fix: treat an error with no code as missing, and reserve undefined for known transient codes such as EPERM, EBUSY and EACCES.


Generated by Claude Code

…build, and smaller fixes

- A block that no module asks for is only forgotten in a build that
  compiled another part of its file. One whose module merely left the
  build is kept: webpack brings the module back from its cache without
  running the loaders, and its errors were never reported again.
- A forgotten block is also taken out of the files a failed recheck left
  owed, one of which the next recheck opens the project with.
- TS2882, the "cannot find module" of a side-effect import, counts as a
  missing import that a new layer-only file may satisfy.
- Removed layer names are kept by canonical path, so a name the API
  spells in another case is still recognised on Windows.
- webpack's file system is only purged for a virtual file it no longer
  has: webpack-virtual-modules replaces purge with one that empties the
  whole cache.
- A stat error with no code means the file is missing, as it did before
  this pull request; only an error with another code is "can't tell".

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv
…ther failing import

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv
…request

Giving a .vue file's template a TypeScript name of its own, and all that
follows from it (forgetting a block, hiding removed layers, declarations
and error names for blocks), moves to a pull request of its own on top
of this one. Four review rounds found problems in each other's fixes
there, and none in the rest.

Stays: the fixes for #1735 to #1739, virtual modules, watching the real
file in place of a name only TypeScript knows, and a module depending on
its own file. vueScriptWatch covers those last two with a script block
alone; the four tests about blocks go with the code.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv
@johnnyreilly johnnyreilly changed the title fix: type check virtual modules and Vue files reliably, and builds after the TypeScript process is replaced fix: type check virtual modules, and builds after the TypeScript process is replaced Nov 29, 2026

@johnnyreilly johnnyreilly left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fifth review, at 89b1b78 (after the split to #1743). CI is green: all 45 checks passed. None of the earlier rounds' findings came back.

One thing to fix or disprove before merge; the rest can be follow-ups. All come from reading the code; none was reproduced by running it.

Before merge

  1. A deleted file outside the tsconfig is dropped from orphanRootFileNames but not from the synthetic program (lines 1433 and 2172, same root cause). Deleting and restoring an orphan entry can compile it against a program that lacks it.

Follow-ups

  1. Predates this PR: the recheck can anchor on a file the new process has in no program (line 2135).
  2. Nit: the webpack 4 missing-file check can leave a cached "no such file" answer (line 1482).
  3. Nit: every recompiled file is still read from disk a second time in watch mode (line 1335).

Generated by Claude Code

Comment thread src/typeScriptApi.ts
layeredFiles.delete(layerKey);
// Or it stays a root of the synthetic program, which is then
// reconfigured the next time a file is compiled in it.
orphanRootFileNames.delete(layerKey);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Worth fixing or disproving before merge: a deleted file outside the tsconfig is dropped from orphanRootFileNames but not from the synthetic program.

forgetRemovedFileLayers (and forgetMissingFileLayers) now delete the root from the map without reconfiguring the synthetic program, so the program still lists the file as a root while the map does not.

Scenario: webpack 5 watch, no compilerOptions/context overrides. Entry extra.ts is outside the tsconfig and imported by nothing. Delete it in a build that compiles no other orphan file: its root is removed from the map but the program keeps it. Restore it: ensureOrphanProgram sees getSourceFile(extra.ts) truthy in the stale program and calls reconfigureOrphanProgramIfStale, which reconfigures without extra.ts and returns that program. getTypeScriptEmit then falls back to getDefaultProjectForFile or a program lacking the file, giving wrong options, wrong diagnostics or empty output until the file is next compiled.

Suggested fix: reconfigure the synthetic program when a root is dropped, or make ensureOrphanProgram require orphanRootFileNames.has(...) before taking the reconfigureOrphanProgramIfStale shortcut. A delete-then-restore watch test for an orphan entry would settle whether the scenario is real.

Traced from the diff and base code; not run.


Generated by Claude Code

Comment thread src/typeScriptApi.ts
if (overriddenConfig !== undefined && overriddenConfig.errors.length > 0) {
return;
}
if (

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same root cause as the comment on line 1433, seen from the recheck: after an orphan file outside the tsconfig is deleted, the synthetic program keeps it as a root until another orphan file happens to be compiled.

The recheck only creates a missing synthetic program, and only reconfigures an existing one when config overrides are set, so the root removed from orphanRootFileNames is never applied to the program.

Scenario: webpack 5 watch, no config overrides. Delete an orphan entry file, with no other orphan file rebuilt. The map loses the root, but orphanProgram.configuration and the API's program still list the missing file, so the recheck and recordProgramDiagnostics read a program with a root that is gone (the comment in replaceTypeScriptApi says such a root "would be an error in it").

Suggested fix: call reconfigureOrphanProgramIfStale in the recheck whenever orphanProgram exists, not only with overriddenConfig. That would likely cover the line 1433 scenario too.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
const anchorFileName =
changedFileNames.values().next().value ??
findExistingProjectFile(typeScriptInstance, instance.resolvedFilePathCache);
(hadSnapshot ? changedFileNames.values().next().value : undefined) ??

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Predates this PR; fine as a follow-up: after a compiler process dies mid-build, the recheck can still anchor on a file the new process has in no program.

The hadSnapshot guard only skips changedFilesThisBuild as the anchor when the new process has no snapshot at all, but a file compiled after the restart gives it one.

Scenario: orphan file A is compiled first, then the process dies while compiling project file X. The retry of X gives the new process a snapshot, so hadSnapshot is true. changedFilesThisBuild (carried over) yields A as the anchor, and openPrimaryProject opens A, which is in no project of the new process. This is the case the comment says the fallback exists to avoid. The effect of opening such a file was not tested.

Possible fix: also skip a changed file as anchor when orphanProgram is undefined and the file is in orphanRootFileNames, or create the synthetic program before opening the anchor.


Generated by Claude Code

Comment thread src/typeScriptApi.ts Outdated
* file (a virtual resource), or can't tell about now, is taken to be there.
*/
function isMissing({ isFile }: InputFiles, layeredFile: FileLayer) {
return layeredFile.wasFile && isFile(layeredFile.resourcePath) === false;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nit; fine as a follow-up: the webpack 4 "is this layered file gone" check can leave a cached "no such file" answer behind.

isMissing asks webpack's file system with isFile for every layered file in restartTypeScriptApiIfLayeredFileIsGone, now also from the end-of-build recheck. Only the compile hook filter in index.ts uses the purging isFileNow.

Scenario: webpack 4 watch with a virtual-modules plugin. A virtual module is briefly absent from the input file system when a rebuild runs isMissing, so the negative stat is cached. The plugin puts the module back, but webpack's disk watcher never reports it, so the cached answer stays (the problem the isFileNow doc comment describes) and webpack fails to resolve the module.

Possible fix: use isFileNow in isMissing, or confirm webpack always purges these paths before they can matter.

Found by reading the diff; not reproduced.


Generated by Claude Code

Comment thread src/typeScriptApi.ts
const diskContents =
snapshotIsCurrent && !typeScriptInstance.everLayeredFiles.has(layerKey)
? contents
: readDiskFile(fileName);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nit; fine as a follow-up: in watch mode every recompiled file is still read from disk synchronously a second time.

layerFileContents only skips readDiskFile for a file not yet in everLayeredFiles, and every compiled file is added to that set, so the shortcut only helps on the first build. The code on next read through webpack's cached input file system.

Scenario: a branch switch in watch mode rebuilds 2,000 TypeScript files. Each compile does an extra uncached fs.readFileSync of the whole file on the loader thread, which the comment above says it means to avoid. The description's +4.5% on hub-touch rebuilds against next may be this.

Possible fix: only add to everLayeredFiles when the layer differs from disk, or cache the disk text by mtime.


Generated by Claude Code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not changed. I measured it, and the suggested fix would reopen the hole the previous round closed.

Cost: reading the 1,200 files of the benchmark fixture with fs.readFileSync(f, 'utf8'), as readDiskFile does, takes about 4 ms once the OS has them cached (85 ms the first time, which webpack's own read has already paid). A hub-touch rebuild of that fixture takes about 1,400 ms, so this isn't the +4.5%; I think that number is noise, as the transpileOnly rows that run none of this code move by as much.

Why not "only add to everLayeredFiles when the layer differs from disk": the shortcut concludes "the API has this text and was never given it, so it read it from disk". A file layered with text equal to the disk's has been given it. If the disk changes afterwards and a loader in front still produces the old text, the shortcut would take the layer to be on disk and drop it from what a restarted process is re-sent. It is a narrow case, but it is the one the set exists for, and 4 ms doesn't buy it back.

Caching the disk text by mtime would be sound, but adds a stat per file to save a read of a file the OS already has in memory.


Generated by Claude Code

…ed and put back

- When such a file's root is dropped because the file is gone, the
  synthetic program is reconfigured by the end-of-build recheck, with or
  without config overrides. It kept listing the missing root.
- A file compiled again after that is added as a root again. The stale
  program still held it, so it was taken to be in the program, and then
  reconfigured out of it: it was compiled, but not found as a dependant
  when a type it imports changed.
- The recheck doesn't open the project with a file of the synthetic
  program while a new process has no such program yet.
- The webpack 4 check for a layered file that has gone leaves no "no such
  file" answer in webpack's file system cache.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv
@johnnyreilly
johnnyreilly merged commit c78033c into next Nov 29, 2026
45 checks passed
@johnnyreilly
johnnyreilly deleted the ccr-e5123452-iefcal branch November 29, 2026 17:35
johnnyreilly pushed a commit that referenced this pull request Nov 29, 2026
next is #1741 squashed, which this branch already holds commit by commit:
the two trees are the same, so nothing changes here.
johnnyreilly added a commit that referenced this pull request Nov 29, 2026
* fix: type check virtual modules, and builds after the TypeScript process is replaced

Fixes the cases left open by #1734.

- #1735: a file TypeScript can only get from ts-loader (a virtual module)
  isn't there for an importer compiled before it. The importer's failed
  import is now remembered, and the end-of-build recheck checks it again
  once such a file has been layered. A file webpack's watcher reports as
  removed while its input file system still has it is no longer treated
  as removed.
- #1736: a layer is only dropped as "missing" when webpack's file system
  had the file and now says it is absent; a failed stat that isn't
  ENOENT/ENOTDIR no longer counts. Whether a restarted process must be
  sent a file again is decided by what is on disk, which is what the
  process reads, not by webpack's input file system.
- #1737: each layer records webpack's own name for its file, so a
  deleted `x.ts` is noticed with a file `x` beside it.
- #1738: the webpack 4 deleted-file check also runs before the
  end-of-build recheck, which opens the project itself when the process
  has just been replaced.
- #1739: a replaced process keeps the files compiled outside the
  tsconfig as roots, and their program is made again by the next such
  file compiled or by the end-of-build recheck.

Closes #1735, closes #1736, closes #1737, closes #1738, closes #1739

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* fix: only check an importer again when its import was an error

A re-check of every importer whose import had no resolved file also took
in imports answered by an ambient declaration (declare module '*.vue'),
and swapped the declared type for the layered file's.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* docs: changelog link for #1741

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* fix: keep a Vue file's script and template apart, and watch real files only

- vue-loader sends the script and the compiled template of a .vue file
  through ts-loader as the same file. Both were given to TypeScript under
  one name, so whichever was compiled last was what an import of the file
  resolved to, and the template has no default export (TS1192). The
  script keeps the file's name; any other block gets its own.
- A name only TypeScript knows (a suffix added by appendTsSuffixTo or for
  a file without a TypeScript extension) was handed to webpack to watch.
  It names no file, so webpack reported it removed after the first build:
  an extra rebuild, and TypeScript told to forget the file. webpack is now
  given the file the layer was compiled from.
- A module depends on its own file, not on the suffixed name, so it is
  rebuilt when the file changes.
- Removed files are matched to layers by the name webpack compiled them
  under, since one file can now have several layers.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* fix: address review - Vue blocks of a deleted file, a recheck with no config file, and virtual files

- A removed or changed file is passed on to TypeScript under the name of
  every layer it has, so a deleted .vue file's template goes with its
  script. A removed layer is no longer a root of the synthetic program,
  and a name ts-loader made up is hidden from TypeScript, which otherwise
  finds the layer again when it next lists the directory.
- A block of a file gets no declaration file of its own.
- The end-of-build recheck picks the file it opens the project with
  before it takes what changed, and falls back to a file compiled outside
  the tsconfig, so a project whose tsconfig lists no existing file doesn't
  lose a recheck it is owed.
- A file is known to be virtual from when it was compiled. webpack's file
  system is no longer asked about files reported removed: that left "no
  such file" in its cache, and a file put back could not be resolved.
  Such a file is neither removed nor changed, so it no longer counts as
  created or has its dependants rechecked on every rebuild, and a rename
  that only changes case is a removal again.
- A file whose text TypeScript already read from disk isn't read from
  disk a second time.
- Test plugin: function declarations for named helpers.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* fix: address re-review - hide only made-up names, forget removed virtual files and roots

- Only a layer whose name ts-loader made up is hidden from TypeScript
  when its file is removed. The check was "not on disk", which is true of
  any file that was just deleted.
- A virtual file the watcher reports removed is passed on once webpack's
  file system no longer has it. That file system is asked afresh, and a
  "no such file" answer is not left in its cache.
- A root of the synthetic program that the process has no layer for is
  dropped once it is gone from disk, so a file deleted after a restart
  doesn't stay a root.
- The disk read is only skipped for a file this process was never given
  a layer for: TypeScript keeps the layer of a file ts-loader has dropped
  its own record of.
- Removed layers are hidden before the end-of-build recheck, which checks
  their importers again: one compiled earlier in that build saw the layer.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* fix: address third review - Vue blocks that go away, and what is watched while one is removed

- A block that no module of the build asks for is forgotten like a
  removed file's: a .vue file that loses its template no longer keeps
  the template's layer and errors for the rest of the watch.
- A block's errors are reported under the name its compile gave them in
  the end-of-build recheck too, not under the block's made-up name.
- With transpileOnly and isolatedDeclarations, a block's declarations
  are not emitted: they went to the same asset path as the script's.
- The name of a layer whose file was removed is not handed to webpack to
  watch in the build before the layer is hidden.
- The importers of hidden layers stay owed a recheck when the recheck
  returns early on config errors.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* fix: address fourth review - keep a block whose module only left the build, and smaller fixes

- A block that no module asks for is only forgotten in a build that
  compiled another part of its file. One whose module merely left the
  build is kept: webpack brings the module back from its cache without
  running the loaders, and its errors were never reported again.
- A forgotten block is also taken out of the files a failed recheck left
  owed, one of which the next recheck opens the project with.
- TS2882, the "cannot find module" of a side-effect import, counts as a
  missing import that a new layer-only file may satisfy.
- Removed layer names are kept by canonical path, so a name the API
  spells in another case is still recognised on Windows.
- webpack's file system is only purged for a virtual file it no longer
  has: webpack-virtual-modules replaces purge with one that empties the
  whole cache.
- A stat error with no code means the file is missing, as it did before
  this pull request; only an error with another code is "can't tell".

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* test: a side-effect import of a virtual module, from a file with no other failing import

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* refactor: split the handling of separate Vue blocks out of this pull request

Giving a .vue file's template a TypeScript name of its own, and all that
follows from it (forgetting a block, hiding removed layers, declarations
and error names for blocks), moves to a pull request of its own on top
of this one. Four review rounds found problems in each other's fixes
there, and none in the rest.

Stays: the fixes for #1735 to #1739, virtual modules, watching the real
file in place of a name only TypeScript knows, and a module depending on
its own file. vueScriptWatch covers those last two with a script block
alone; the four tests about blocks go with the code.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* fix: keep a Vue file's script and template apart

vue-loader sends the script and the compiled template of a .vue file
through ts-loader as the same file. Both were given to TypeScript under
one name, so whichever was compiled last was what an import of the file
resolved to, and the template has no default export (TS1192). The script
keeps the file's name; any other block gets its own.

With that come:
- a removed or changed file is passed on under the name of every layer
  it has, and a removed layer whose name ts-loader made up is hidden
  from TypeScript, which otherwise finds it again;
- a block that leaves its file is forgotten, in a build that compiled
  another part of the file;
- no declarations are emitted for a block, and its errors are reported
  under the script's name.

This is the part split out of #1741, as it stood there after four
review rounds.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* docs: changelog link for #1743

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* fix: address fifth review - a file outside the tsconfig that is deleted and put back

- When such a file's root is dropped because the file is gone, the
  synthetic program is reconfigured by the end-of-build recheck, with or
  without config overrides. It kept listing the missing root.
- A file compiled again after that is added as a root again. The stale
  program still held it, so it was taken to be in the program, and then
  reconfigured out of it: it was compiled, but not found as a dependant
  when a type it imports changed.
- The recheck doesn't open the project with a file of the synthetic
  program while a new process has no such program yet.
- The webpack 4 check for a layered file that has gone leaves no "no such
  file" answer in webpack's file system cache.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

* fix: address review - keep a block's layer, and only its errors wait for a request

- A block that no module of the build asks for is no longer forgotten.
  Its layer and errors are kept, and the errors are left out of the
  report while nothing asks for the block. Forgetting it relied on
  knowing which files a build compiled, which a failed recheck carries
  over from the build before, and on the block being compiled again,
  which webpack's cache can skip.
- A block's errors go on the compilation only. Without appendTsSuffixTo
  they share a file name with the script's, and took their place on the
  module.
- A removed layer is only hidden if its made-up name isn't also a real
  file's, and it is taken out of the files a failed recheck left owed.
- A replaced process keeps the list of hidden layers whose importers are
  still to be checked again.
- The recheck reads the primary project from the snapshot that is
  current after hiding removed layers.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv

---------

Co-authored-by: Claude <noreply@anthropic.com>
johnnyreilly added a commit that referenced this pull request Nov 30, 2026
* perf: stop reading every recompiled file from disk on a rebuild

Since #1741, layerFileContents reads each compiled file from disk to tell
whether its contents are what the disk holds, which decides what a
restarted TypeScript process has to be sent again and which files are
virtual. It only skipped the read for a file the API had read itself and
had never been given, which covers a cold build. In a rebuild every
compiled file has been given to the API before, so each one was read:
after a change to a widely imported file, that is every file that imports
it. On the 300-file benchmark fixture it was 158 reads per rebuild, where
#1734 made none.

A file's copy on disk is now only looked at when it can have changed: the
first time the file is compiled, when the text webpack hands over differs
from the last time, or when webpack's watcher has reported the file since.
Otherwise the answers of the last compile are kept. Without the watcher's
list (the first build, webpack 4, a second run of the same compiler)
nothing is kept, as before. A file compiled under a name ts-loader made up
is always looked at: webpack doesn't watch a file of that name.

The new comparison test compiles a file as it is on disk, then again after
an edit that another loader undoes, so the same text arrives while the
disk differs, and then restarts the TypeScript process. It fails if the
watcher's report is ignored.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7KnFavDVeKUKPTjSKZ

* docs: benchmark figures for #1756

The benchmark workflow's first run on the pull request had the rebuild
after the hub file changes at -2.7% (Ubuntu), -18.2% (macOS) and -8.5%
(Windows), and with all orphan files at -2.2%, -7.5% and -6.1%. The
changelog entry and the comment in layerFileContents now say what the
reads cost.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7KnFavDVeKUKPTjSKZ

* fix: address review - only a file found on disk is remembered

From an independent review of the change for cases where it computes
something `next` wouldn't:

- A look-up that fails is not remembered. A file that couldn't be read,
  or that webpack's file system didn't have as a file, was kept as such
  until the watcher reported it; `next` asks again at the next compile. The
  set now only holds files that were found on disk, so it is
  `filesSeenOnDisk`, and a file that isn't on disk (a virtual module) is
  looked at on every compile, as on `next`.
- A layer under a name ts-loader made up takes the name out of the set. A
  real file of that name, compiled as a module, could have put it there
  (`schema.graphql.ts` beside `schema.graphql`), and the made-up layer
  would then have passed for it.
- The comments say which files are skipped: those that were on disk under
  the name webpack watches them by.

The review's one case with a visible effect is the limit the pull request
already describes: a file the watcher isn't watching, changed on disk in a
way another loader undoes, and then a restart of the TypeScript process.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7KnFavDVeKUKPTjSKZ

* docs: benchmark figures over three runs

Three runs of the benchmark workflow on the pull request had the rebuild
after the hub file changes at -1.0% to -2.7% on Ubuntu, -9.0% to -18.2% on
macOS and -5.9% to -8.5% on Windows, and with all orphan files at -1.4% to
-2.2%, -6.7% to -7.5% and -4.7% to -6.1%. The changelog said 6% to 18% and
2% to 3%, from the first run alone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2Jv7KnFavDVeKUKPTjSKZ

---------

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants