Repository navigation
fix: type check virtual modules, and builds after the TypeScript process is replaced - #1741
Conversation
…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
…cr-e5123452-iefcal
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv
Benchmark (Ubuntu)
PR branch = |
Benchmark (macOS)
PR branch = |
Benchmark (Windows)
PR branch = |
…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
left a comment
There was a problem hiding this comment.
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.
- A deleted
.vuefile's template block stays in the program (src/typeScriptApi.ts). - The owed recheck can be dropped after a process restart in an all-orphan project (
src/typeScriptApi.ts). - A virtual module outside the TS program is treated as newly created on every rebuild (
src/index.ts). - A case-only rename on macOS/Windows is no longer seen as a removal (
src/index.ts, same comment as 3). declaration: trueprobably emits a strayApp.vue.template.d.ts(src/typeScriptApi.ts).- Nit: one extra synchronous disk read per compiled file (
src/typeScriptApi.ts). - Nit: arrow-function constants for named helpers in
virtual-files-plugin.js.
Generated by Claude Code
| */ | ||
| function toApiFacingFileName(fileName: string) { | ||
| function toApiFacingFileName(fileName: string, blockName?: string) { | ||
| if (blockName !== undefined) { |
There was a problem hiding this comment.
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
| // to the synthetic program, which it doesn't have yet. | ||
| const anchorFileName = | ||
| changedFileNames.values().next().value ?? | ||
| (hadSnapshot ? changedFileNames.values().next().value : undefined) ?? |
There was a problem hiding this comment.
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
| const modifiedFileNames = [...(compiler.modifiedFiles ?? [])]; | ||
| const removedFileNames: string[] = []; | ||
| for (const fileName of compiler.removedFiles ?? []) { | ||
| (typeScriptInstance.inputFiles.isFile(fileName) === true |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
compilehook drops a "removed" report for those names only. - A dropped report is neither removed nor changed. So it isn't in
pendingChangedFiles(nocreated, no cleareddirectImportsCache) or inwatcherChangedFiles(no recheck of its dependants). webpack rebuilds a virtual module whose contents changed, which layers it again. - A virtual
routes.jsthat ts-loader never compiles isn't marked, so it is handled as onnext. - A case-only rename:
Foo.tsis 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
| function toApiFacingFileName(fileName: string, blockName?: string) { | ||
| if (blockName !== undefined) { | ||
| const extension = /\.tsx?$/.exec(fileName)?.[0] ?? '.ts'; | ||
| return `${fileName.replace(/\.tsx?$/, '')}.${blockName}${extension}`; |
There was a problem hiding this comment.
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
| // Only a file that was there can be seen to have gone. | ||
| wasFile: typeScriptInstance.inputFiles.isFile(resourcePath) === true, | ||
| }; | ||
| const diskContents = readDiskFile(fileName); |
There was a problem hiding this comment.
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 => ({ |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
- A deleted real file is hidden from TypeScript and can stay hidden after it is put back (
src/typeScriptApi.ts). - A virtual module that really is removed is never reported to TypeScript on webpack 5 (
src/index.ts). - After a process restart, a deleted file can stay a root of the synthetic program (
src/typeScriptApi.ts). - A file whose loader output differs from disk can lose its layer on a later restart (
src/typeScriptApi.ts). - Nit: a removed
.vuefile's layers stay visible for the whole build that reports the removal (src/typeScriptApi.ts).
Generated by Claude Code
| // 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)) { |
There was a problem hiding this comment.
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
| // 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( |
There was a problem hiding this comment.
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
| // 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( |
There was a problem hiding this comment.
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
| ); | ||
| // 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 = |
There was a problem hiding this comment.
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
| removedFileNames, | ||
| fileName, | ||
| ); | ||
| const hideRemovedLayers = |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
- A Vue block that stops being compiled leaves its layer and errors behind (inline, line 291).
- In the build that deletes a
.vuefile, an importer can still be made to watch the made-up.vue.tsname (inline, line 2750). - Predates this PR: with
transpileOnlyandisolatedDeclarations, a template block's declaration is emitted to the script's asset path (file comment, line 679). - Predates this PR: a removed file layered under its real name can come back as a ghost project file (inline, line 1452).
- Nit: a template block's errors change file name between builds (file comment, line 2447).
- Nit:
hiddenLayerPathsis cleared before an early return on config errors (inline, line 2257).
Generated by Claude Code
| // `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); |
There was a problem hiding this comment.
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
| * 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 { |
There was a problem hiding this comment.
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
| // 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)) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
| // A file compiled before its import's layer was hidden was checked with | ||
| // the layer still there. | ||
| const hiddenLayerPaths = [...typeScriptInstance.hiddenLayerPaths]; | ||
| typeScriptInstance.hiddenLayerPaths.clear(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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).
- A template block's type errors can vanish for the rest of a watch session (
src/typeScriptApi.ts:2594). - Template errors may be dropped in builds with module concatenation (
src/typeScriptApi.ts:2566). - The end-of-build recheck can anchor on a block file that was just hidden (
src/typeScriptApi.ts:2222). - A side-effect import of a virtual module keeps a false TS2882 (
src/typeScriptApi.ts:2618). - On Windows, a removed layer's made-up name may still reach webpack's watcher (
src/typeScriptApi.ts:2844). - Nit:
isFileNowmay flush webpack's whole file-system cache on every rebuild (src/index.ts:386). - Nit: a stat error without a
codeis treated as "can't tell" (src/index.ts:380).
Generated by Claude Code
| typeScriptInstance.unsentRemovedLayerPaths.add( | ||
| path.resolve(layeredFile.fileName), | ||
| ); | ||
| instance.pendingDiagnostics.delete(layerKey); |
There was a problem hiding this comment.
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
| return; | ||
| } | ||
| const requestedBlocks = new Set<string>(); | ||
| compilation.modules.forEach(module => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
| const anchorFileName = | ||
| changedFileNames.values().next().value ?? | ||
| findExistingProjectFile(typeScriptInstance, instance.resolvedFilePathCache); | ||
| (hadSnapshot ? changedFileNames.values().next().value : undefined) ?? |
There was a problem hiding this comment.
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
| } | ||
|
|
||
| /** TS2307 and TS2792: "Cannot find module". */ | ||
| const missingImportDiagnosticCodes = [2307, 2792]; |
There was a problem hiding this comment.
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
| if (layeredFile !== undefined) { | ||
| return layeredFile.resourcePath; | ||
| } | ||
| return unsentRemovedLayerPaths.has(path.resolve(fileName)) |
There was a problem hiding this comment.
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
| return undefined; | ||
| isFile, | ||
| isFileNow(fileName) { | ||
| fileSystem.purge?.(fileName); |
There was a problem hiding this comment.
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
| return fileSystem.statSync(fileName).isFile(); | ||
| } catch (error) { | ||
| const code = (error as { code?: unknown } | null)?.code; | ||
| return code === 'ENOENT' || code === 'ENOTDIR' ? false : undefined; |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
- A deleted file outside the tsconfig is dropped from
orphanRootFileNamesbut 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
- Predates this PR: the recheck can anchor on a file the new process has in no program (line 2135).
- Nit: the webpack 4 missing-file check can leave a cached "no such file" answer (line 1482).
- Nit: every recompiled file is still read from disk a second time in watch mode (line 1335).
Generated by Claude Code
| 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); |
There was a problem hiding this comment.
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
| if (overriddenConfig !== undefined && overriddenConfig.errors.length > 0) { | ||
| return; | ||
| } | ||
| if ( |
There was a problem hiding this comment.
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
| const anchorFileName = | ||
| changedFileNames.values().next().value ?? | ||
| findExistingProjectFile(typeScriptInstance, instance.resolvedFilePathCache); | ||
| (hadSnapshot ? changedFileNames.values().next().value : undefined) ?? |
There was a problem hiding this comment.
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
| * 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; |
There was a problem hiding this comment.
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
| const diskContents = | ||
| snapshotIsCurrent && !typeScriptInstance.everLayeredFiles.has(layerKey) | ||
| ? contents | ||
| : readDiskFile(fileName); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
next is #1741 squashed, which this branch already holds commit by commit: the two trees are the same, so nothing changes here.
* 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>
* 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>
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
.vuefile'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
.vuefile'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
nexttoo: #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 brokesourceMapsShouldConsiderInputSourceMapon CI.Testing with the real
webpack-virtual-modulesplugin showed a second failure in watch mode. webpack's watcher looks at the disk, so on the first rebuild it reports the virtual file incompiler.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
statmeans "absent" forENOENT,ENOTDIR, and an error with no code at all (a file system that isn't Node's). An error with any other code means "can't tell", and the layer is kept.3. #1737: webpack 4, deleted
x.tsunnoticed beside a filexEach 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/.tsxfrom every name is gone. Removed and changed files are matched to layers by the same record, so a layer with the suffix ofappendTsxSuffixTois 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.tsforApp.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
*.vueshim's type, until the file was next compiled. webpack is now given the file the layer was compiled from.With
appendTsSuffixToand vue-loader, a.vuescript 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 toApp.vue, webpack rebuilt the vue-loader module but not the script's module. The module now depends onresourcePath, as ts-loader 9 did.7. Review findings in this code
next.Module not foundwebpack-virtual-modulesreplacespurgewith one that empties the whole cache (confirmed in its source). The webpack 4 missing-file check purges such an answer too (not run).Proof
Script runs in watch mode against a build of
nextand 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.nextwebpack-virtual-modules:app.tsimports a virtualgenerated.tsimport './generated'for side effectsapp.tsrecompiled,app.tsmisuses the virtual file's typeschema.graphql.tsbesideschema.graphql, delete the.tsutil.ts, rebuild compiles onlyindex.jsother.tsutil.tswith another typeother.tsapp.tschanges a type the orphanextra.tsuses throughmid.tsextra.tsextra.tsother.jstop.tsextra.ts, outside the tsconfig, is deleted and put back; thenshared.ts, which it imports a type from, changes that typeextra.tsappendTsSuffixTo: a type error is put in a.vuefile's script#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-moduleson webpack 4 the process was kept onnextas well, because that plugin answersstatSync. The code path that would cause it is gone, but I have no failing case for it.Speed
npm run benchmarkat 1,200 files, 4 iterations (before the multiplier), at db7501b, before the split. Not re-run since. A is this branch.nextNothing 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
.vuefile's script and compiled template still share a name, as onnext: an importer can getTS1192: Module '"App.vue"' has no default export. That is fix: keep a Vue file's script and template apart #1743..vuefile's script layer can be found again by TypeScript, like any deleted file's: v10, webpack 5: TypeScript finds a deleted file again in a later rebuild, and the "Cannot find module" error goes away #1742. fix: keep a Vue file's script and template apart #1743 hides layers with a made-up name; this PR doesn't.other.ts(in the tsconfig, not bundled) imports a type fromb.ts; deleteb.tsandother.tsgets TS2307; editother.tsthree builds later and the error is gone, thoughb.tsis still deleted. Same onnextand on this branch. A removed virtual module behaves the same way..vuefile gets the type of its script or of adeclare module '*.vue'shim depends on whether TypeScript knew the script when the import was first resolved. This PR stops the script being forgotten between builds, which made it worse; it doesn't make the two cases agree.Testing
At 5d4f461:
npm run comparison-tests: 82 suites pass.npm run execution-testspasses. (Linux, Node 22, webpack 5.)npm run lint,npm run format:check,npm run knip,npm run check:comparison-testspass.virtualModuleWatch: virtual modules on the first build (v10: false "Cannot find module" for an import of a file TypeScript only learns about later in the build (virtual modules) #1735, with a named import and a side-effect import), when the watcher reports them removed, and after a restart (v10: after a compiler restart, a file webpack's file system can't stat loses its contents in the new process #1736). Fails onnext; each of the three stages was checked to fail when its own fix is disabled.compilerProcessRestartOrphanWatch: v10: replacing the TypeScript process forgets files compiled outside the tsconfig, so their type errors can go stale #1739 on webpack 5. Fails onnext.compilerProcessRestartNothingCompiledWatch: the recheck without a snapshot (v10, webpack 4: a rebuild that deletes a file but compiles nothing through ts-loader still type checks against it #1738's mechanism) on webpack 5. Fails onnext.compilerProcessRestartNoConfigFilesWatch: the owed recheck when the tsconfig lists no existing file.vueScriptWatch: two loaders standing in for vue-loader send a file's script through ts-loader withappendTsSuffixTo; an importer is added in a rebuild, then the script changes. Fails onnext, and when either fix of section 6 is disabled.orphanDeletedRestoredWatch: a file outside the tsconfig is deleted and put back, then a type it imports changes. Fails on 89b1b78. NotranspileOnlyrun (added to the harness's list): its last patch changes a file only a type import reaches, which starts no rebuild there.🤖 Generated with Claude Code
https://claude.ai/code/session_01Q2Jv7HXiQsLaoYG7G8eXfv