vitest-dev / vitest-dev/vitest
Optimized dependencies are unable to resolve external dependencies provided by their respective workspaces
Nobody has claimed this yet.
- 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.jssite-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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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