vitest icon indicating copy to clipboard operation
vitest copied to clipboard

utils.ensurePackageInstalled is not compatible with yarn PnP

Open pietrovismara opened this issue 2 years ago • 4 comments

Describe the bug

Trying to run vitest in a package of a yarn monorepo will cause the following error:

 MISSING DEP  Can not find dependency 'vite'

✖ Do you want to install vite? … no

I already have vite installed and apparently the issue is caused by this line: https://github.com/vitest-dev/vitest/blob/f557565626e74e3345b417345bd53213c240ecf8/packages/vitest/src/utils/index.ts#L42

The local-pkg is not compatible with yarn PnP, as confirmed by this issue.

As far as I understand it, it should be enough to call isPackageExists with a second options parameters like this:

isPackageExists(dependency, { paths: [ process.cwd() ] })

I already tested this solution locally with a fork of local-pkg and it works.

Reproduction

The issue is already confirmed.

System Info

System:
    OS: Linux 5.10 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: x64 Intel [redacted]
    Memory: 2.72 GB / 7.63 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.13.2 - /usr/bin/node
    Yarn: 3.1.1 - /usr/bin/yarn
    npm: 8.1.2 - /usr/bin/npm

Used Package Manager

yarn

Validations

pietrovismara avatar Mar 06 '22 13:03 pietrovismara

I additionally had the following problem which I think stems from vitest requiring happy-dom through local-pkg:

Error: local-pkg tried to access happy-dom, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: happy-dom (via "happy-dom/package.json")

https://github.com/vitest-dev/vitest/blob/f5189fd3e3b3e361948ea30c6e2d5732fe6ff6a0/packages/vitest/src/integrations/env/happy-dom.ts#L8

After patching vitest with @pietrovismara's fix I had to add the following to yarnrc.yml though it feels very wrong since it feels like vitest should need to be specified and not local-pkg for extension. However fixed the issue.

  "local-pkg@*": # This is required for vitest as it accesses happy-dom through local-pkg on its behalf
    dependencies:
      "happy-dom": "*"

IgnusG avatar Mar 31 '22 16:03 IgnusG

we are looking to migrate from jest to vitest but we have the same issue with a yarn2 monorepo

eudinnou avatar Apr 28 '22 13:04 eudinnou

For anyone trying to use Vitest with pnpm in a monorepo, the following .npmrc config seems to work as a workaround:

hoist-pattern[]="vite"

fwouts avatar May 04 '22 04:05 fwouts

The issue should still be fixed, but as a temporary solution you can add this to .yarnrc.yml:

pnpMode: "loose"

packageExtensions:
    "vite@*": 
        dependencies:
            "supports-color": "*"
    "debug@*": 
        dependencies:
            "supports-color": "*"

ekeijl avatar Jul 18 '22 11:07 ekeijl

Also happens with pnpm and hoist=false in .npmrc.

Error looks like:

Error: Cannot find package 'jsdom' imported from [REDACTED]/node_modules/.pnpm/[email protected]/node_modules/local-pkg/index.mjs

jfirebaugh avatar Nov 29 '23 03:11 jfirebaugh