Skip to content

TypeScript error when using Yarn PnP #4391

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

Closed
6 tasks done
wojtekmaj opened this issue Oct 29, 2023 · 4 comments · Fixed by #4392
Closed
6 tasks done

TypeScript error when using Yarn PnP #4391

wojtekmaj opened this issue Oct 29, 2023 · 4 comments · Fixed by #4392
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) types

Comments

@wojtekmaj
Copy link
Contributor

Describe the bug

Error

The following error may appear while type checking, if using Yarn PnP:

yarn tsc             
.yarn/unplugged/vitest-virtual-635245b105/node_modules/vitest/dist/reporters-5f784f42.d.ts:15:25 - error TS2307: Cannot find module 'rollup' or its corresponding type declarations.

15 import * as rollup from 'rollup';
                           ~~~~~~~~

Found 1 error in .yarn/unplugged/vitest-virtual-635245b105/node_modules/vitest/dist/reporters-5f784f42.d.ts:15

Why this happens:

vitest imports rollup, while not declaring it as its dependency. This is illegal in Yarn PnP, and works "by miracle" in non-PnP setups.

Here's why.

Imagine vitest relies on [email protected] which in turn relies on bar. foo's maintainer finds a way to get rid of bar dependency without changing its public API. Releases this improvement as a non-breaking version, righfully so.

Now, if you relied on foo to provide vitest with bar, everything falls apart.

In fact, with Vite plans to replace rollup with rolldown the above is EXACTLY what might happen.

What we can do about it

Declare rollup as vitest's dependency.

Reproduction

n/a

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
    Memory: 5.44 GB / 15.85 GB
  Binaries:
    Node: 20.9.0 - ~\AppData\Local\Temp\xfs-515b5efc\node.CMD
    Yarn: 4.0.0 - ~\AppData\Local\Temp\xfs-515b5efc\yarn.CMD
    npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (118.0.2088.76)

Used Package Manager

yarn

Validations

@wojtekmaj
Copy link
Contributor Author

Can confirm that adding:

packageExtensions:
  "vitest@*":
    dependencies:
      rollup: ^3.25.2

to .yarnrc.yml fixes the issue, which proves the suggested fix would have worked.

@sheremet-va
Copy link
Member

sheremet-va commented Oct 29, 2023

Yeah, I wonder why it worked before. I noticed a rogue rollup import in types a few days ago - I wonder if it was added during the last update.

Vitest doesn't depend on rollup directly, Vite does, so I don't think we can specify it. However, this should change with #4368 when we upgrade to rollup 4 and have to use rollup/parseAst. I guess, we need to specify it as a peerDependency now, but I am afraid of type mismatches with Vite :(

@sheremet-va sheremet-va added bug types p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Oct 29, 2023
@sheremet-va
Copy link
Member

Can confirm, this is just what rollup bundles are because it's externalized when building Vitest: WorkspaceProject.prototype.getSourceMapModuleById returns (without an annotation) rollup.SourceMap. If vite decides to move away from rollup, this will be changed when Vitest is rebundled.

For now, we can just make the same interface manually and return it there, I guess.

@wojtekmaj
Copy link
Contributor Author

I wonder if it was added during the last update.

0.33.0 didn't have it as far as I can see. 0.34.0 added the import in types-3c7dbfa5.d.ts.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants