Skip to content

Fails to run with setupFile when inside git worktree #9955

@marvinhagemeister

Description

@marvinhagemeister

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

  1. 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
  1. Add a vitest.config.mjs file
import { defineConfig } from 'vitest/config';
export default defineConfig({ test: { setupFiles: ['./setup.js'] } });
  1. Add setup.js
globalThis.__SETUP_LOCATION = 'MAIN REPO';
  1. Add a dummy test file
import { test, expect } from 'vitest';
test('should load setup from worktree', () => {
  expect(globalThis.__SETUP_LOCATION).toBe('WORKTREE');
});
  1. Run npm install && git add -A && git commit -m "initial"
  2. Create a worktree inside the repo:
git branch worktree-branch
git worktree add .worktree worktree-branch
  1. In the worktree change setup.js
globalThis.__SETUP_LOCATION = 'WORKTREE';
  1. Run npm install inside the worktree
  2. Run npx vitest run inside 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.0

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions