vitest-dev / vitest-dev/vitest

Optimized dependencies are unable to resolve external dependencies provided by their respective workspaces

Open
#4,898 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Describe the bug

I found a regression caused by the changes introduced by #4036 - I have a Yarn PnP monorepo setup that provides dependencies used both in my component/test code and optimized dependency code. I'm using React and Ariakit in the linked reproduction, but this issue is a bit more critical in my private setup where I have both a dependency that is sensitive to duplicate instances and another dependent that requires transpilation by Vite's dependency optimizer.

I'm not sure if the changes in #4036 were completely necessary, as prior to Vitest 0.34.4, I was already seeing Vitest isolate the optimized dependency cache across workspaces. For example, on Vitest 0.34.3, I have two projects that would each optimize the dependency under two separate caches in the respective node_modules folders:

  • site-a/node_modules/.vitest/deps/@kherock_example.js
  • site-b/node_modules/.vitest/deps/@kherock_example.js

Regardless, amending the code to store project caches relative to the workspace root rather than the project root would alleviate my issue. The regression boils down to the fact that resolveOptimizerConfig previously resolved a relative path node_modules/.vitest as the cacheDir, but after the changes to this function, it now resolves to an absolute path via VitestCache.resolveCacheDir. I think the most obvious fix would be to provide a default value for the root argument that resolves to the folder containing the workspace vitest.config.js file instead of process.cwd().

Reproduction

https://github.com/kherock/vitest-workspace-deps-repro

System Info
System:
    OS: macOS 13.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 78.67 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.20.2 - /opt/homebrew/bin/node
    Yarn: 3.7.0 - /opt/homebrew/bin/yarn
    npm: 8.19.4 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 120.0.6099.199
    Edge: 120.0.2210.121
    Firefox: 115.4.0
    Safari: 17.2.1
Used Package Manager

yarn

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with resolveOptimizerConfig and VitestCache.resolveCacheDir, then compare the workspace reproduction with the project cache paths described in the issue. Verify how the cacheDir is resolved across workspaces and use the linked reproduction to confirm that optimized dependencies resolve their workspace-provided external dependencies. Done means the regression is reproduced and workspace caches resolve correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, vite
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.