diff --git a/.changeset/import-graph-typescript-7.md b/.changeset/import-graph-typescript-7.md new file mode 100644 index 0000000..1aeb19e --- /dev/null +++ b/.changeset/import-graph-typescript-7.md @@ -0,0 +1,5 @@ +--- +"diagnostics-webpack-plugin": patch +--- + +Lint every file again, rather than fail, where a project's TypeScript is the native TypeScript 7, which has no API to read what a file imports through: a check reading across files failed with `Cannot read properties of undefined (reading 'readFile')`. diff --git a/README.md b/README.md index 73b8283..1f26efe 100644 --- a/README.md +++ b/README.md @@ -381,7 +381,8 @@ drops it before webpack sees it — and this holds for every check, whatever too it runs. A change that could reach files some other way — a declaration file nothing imports, a `declare global`, a config file, an import that resolves to nothing (a stylesheet, an image or another asset webpack bundles aside) — lints -every file again, as does a project without TypeScript. There, +every file again, as does a project without TypeScript, or with TypeScript 7, +whose native compiler has no API to read imports through. There, an ESLint check with `cache: true` written down keeps ESLint's own cache, which answers for a file whose import changed from before the change. diff --git a/src/import-graph.js b/src/import-graph.js index 48e70e4..2690a52 100644 --- a/src/import-graph.js +++ b/src/import-graph.js @@ -386,7 +386,19 @@ function getGraph(compiler, context, owner) { ).href ); - return createGraph(loaded.default || loaded); + const ts = loaded.default || loaded; + + // TypeScript 7 is a native compiler whose package exports no such + // API: what a file imports cannot be read through it. + if ( + typeof ts.preProcessFile !== "function" || + typeof ts.resolveModuleName !== "function" || + !ts.sys + ) { + return undefined; + } + + return createGraph(ts); } catch { // Without a TypeScript of the project's own there is nothing to say // which file reads which, and every file is linted again instead. diff --git a/test/import-graph.test.js b/test/import-graph.test.js index 50a5957..50cd74f 100644 --- a/test/import-graph.test.js +++ b/test/import-graph.test.js @@ -369,6 +369,22 @@ describe("the graph a compiler keeps", () => { assert.strictEqual(await getGraph({}, project), undefined); }); + + it("should be nothing for a project whose TypeScript has no API to read imports with", async (t) => { + // As TypeScript 7 is: a native compiler whose package exports a version. + const project = mkdtempSync(join(tmpdir(), "native-typescript-")); + const native = join(project, "node_modules", "typescript"); + + t.after(() => rmSync(project, { recursive: true, force: true })); + mkdirSync(native, { recursive: true }); + writeFileSync(join(native, "package.json"), '{ "main": "index.js" }'); + writeFileSync( + join(native, "index.js"), + 'module.exports = { version: "7.0.0", versionMajorMinor: "7.0" };\n', + ); + + assert.strictEqual(await getGraph({}, project), undefined); + }); }); describe("signatures", () => {