rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.57k stars 568 forks source link

Typescript plugin fails to compile the entry point when there is at least one invalid TypeScript file somewhere is the working directory #1652

Open ericmorand opened 6 months ago

ericmorand commented 6 months ago

Expected Behavior

In the reproduction repository, we find 3 TS files:

When executing rollup on src/index.ts, the build should be successful since the src/invalid.ts file is not imported.

Actual Behavior

The build is unsuccessful, and the error suggests that src/invalid.ts is syntax-checked by the plugin even though it is not part of the dependency graph of the entry point:

$ npx rollup -i src/index.ts -p @rollup/plugin-typescript
npm WARN cli npm v10.2.4 does not support Node.js v16.20.2. This version of npm supports the following node versions: `^18.17.0 || >=20.5.0`. You can find the latest version at https://nodejs.org/.

src/index.ts → stdout...
var foo = 5;

console.log(foo);
(!) Plugin typescript: @rollup/plugin-typescript TS2304: Cannot find name 'foo'.
src/invalid.ts: (1:1)

1 foo
  ~~~

Additional Information

Note that the behaviour of tsc is correct in that regard:

$ tsc src/index.ts

No error is emitted and src/index.ts and src/valid.ts are successfully compiled.

So the error is coming from the plugin, not from the TypeScript compiler.

ericmorand commented 6 months ago

While investigating the issue, I stumbled upon a very strange part of the code that seems to handle non-yet discovered files but always ends up throwing an error:

https://github.com/rollup/plugins/blob/33174f956304ab4aad4bbaba656f627c31679dc5/packages/typescript/src/index.ts#L157

if (!parsedOptions.fileNames.includes(fileName)) {
    // Discovered new file that was not known when originally parsing the TypeScript config
    parsedOptions.fileNames.push(fileName);
}

There, the newly discovered file is added to parsedOptions.fileNames but since it was never compiled by the TypeScript program, it is missing from emittedFiles or ts cache, and ends being considered by Rollup as non handled which triggers an error.

What is the rational that drives this part of the code? Can someone guide me to the test that covers this part of the code?