Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Dec 16, 2023 · 1 comment · May be fixed by #1653

Comments

@ericmorand
Copy link

Expected Behavior

In the reproduction repository, we find 3 TS files:

  • src/index.ts, the entry point
  • src/valid.ts, imported by the entry point
  • src/invalid.ts, containing non valid TypeScript syntax and imported by neither src/index.ts or src/valid.ts

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
Copy link
Author

ericmorand commented Dec 16, 2023

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:

// Discovered new file that was not known when originally parsing the TypeScript config

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?

ericmorand added a commit to ericmorand/plugins that referenced this issue Dec 16, 2023
ericmorand added a commit to ericmorand/plugins that referenced this issue Dec 16, 2023
ericmorand added a commit to ericmorand/plugins that referenced this issue Dec 16, 2023
ericmorand added a commit to ericmorand/plugins that referenced this issue Dec 17, 2023
ericmorand added a commit to ericmorand/plugins that referenced this issue Dec 17, 2023
ericmorand added a commit to ericmorand/plugins that referenced this issue Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant