-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Labels
p3-minor-bugAn edge case that only affects very specific usage (priority)An edge case that only affects very specific usage (priority)
Description
Describe the bug
Using vitest inside a git worktree fails because it resolves the setupFile from the wrong folder.
Vitest's resolvePath(path, root) calls resolveModule(path, { paths: [root] }). Internally, root is converted to a file URL without a trailing slash (file:///.../.worktree). Per URL spec, resolving ./setup.js against that URL treats .worktre as a filename and resolves to the parent directory — so it finds and returns the main repo's setup.js instead of the worktree's.
Reproduction
- Create a repo with vitest + a setup file
mkdir /tmp/vitest-worktree-bug && cd /tmp/vitest-worktree-bug
git init
echo "node_modules" > .gitignore
echo '{"type":"module","dependencies":{"vitest":"3.2.1"}}' > package.json- Add a
vitest.config.mjsfile
import { defineConfig } from 'vitest/config';
export default defineConfig({ test: { setupFiles: ['./setup.js'] } });- Add
setup.js
globalThis.__SETUP_LOCATION = 'MAIN REPO';- Add a dummy test file
import { test, expect } from 'vitest';
test('should load setup from worktree', () => {
expect(globalThis.__SETUP_LOCATION).toBe('WORKTREE');
});- Run
npm install && git add -A && git commit -m "initial" - Create a worktree inside the repo:
git branch worktree-branch
git worktree add .worktree worktree-branch- In the worktree change
setup.js
globalThis.__SETUP_LOCATION = 'WORKTREE';- Run
npm installinside the worktree - Run
npx vitest runinside the worktree
# FAILS: expected 'MAIN REPO' to be 'WORKTREE'System Info
System:
OS: macOS 26.3.1
CPU: (8) arm64 Apple M1
Memory: 103.16 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 24.9.0 - /Users/marvinhagemeister/.nvm/versions/node/v24.9.0/bin/node
Yarn: 1.22.22 - /opt/homebrew/bin/yarn
npm: 11.6.0 - /Users/marvinhagemeister/.nvm/versions/node/v24.9.0/bin/npm
pnpm: 10.26.0 - /Users/marvinhagemeister/Library/pnpm/pnpm
bun: 1.3.3 - /Users/marvinhagemeister/.bun/bin/bun
Deno: 2.6.10 - /Users/marvinhagemeister/.deno/bin/deno
Watchman: 2025.03.10.00 - /opt/homebrew/bin/watchman
Browsers:
Chrome: 146.0.7680.80
Chrome Canary: 148.0.7749.0
Firefox: 148.0.2
Safari: 26.3.1
npmPackages:
vitest: 4.1.0 => 4.1.0Used Package Manager
npm
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
p3-minor-bugAn edge case that only affects very specific usage (priority)An edge case that only affects very specific usage (priority)