globalSetup script is executed with wrong CWD
Describe the bug
When using vitest.workspace.ts file in a monorepo, the globalSetup script is executed with the CWD set to the monorepo root rather than the package root.
The vite.config.ts is correctly executed with the package root as CWD.
Reproduction
https://stackblitz.com/~/github.com/maxpatiiuk/vitest-wrong-dir
-
Clone this repository & install dependencies (or use Stackblitz):
git clone https://github.com/maxpatiiuk/vitest-wrong-cwd cd vite-wrong-cwd npm install -
Run
npx vitest runfrom monorepo root. See that thevite.config.tsis correctly executed with CWD set to package root. At the same time,globalSetupscript is unexpectedly executed with CWD set to the monorepo root.
System Info
System:
OS: macOS 14.7
CPU: (10) arm64 Apple M1 Pro
Memory: 501.66 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.16.0 - ~/.nvm/versions/node/v20.16.0/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v20.16.0/bin/yarn
npm: 10.8.1 - ~/.nvm/versions/node/v20.16.0/bin/npm
pnpm: 8.9.0 - ~/.nvm/versions/node/v20.16.0/bin/pnpm
Browsers:
Chrome: 129.0.6668.100
Safari: 18.0.1
npmPackages:
vite: 5.4.8 => 5.4.8
vitest: 2.1.2 => 2.1.2
Used Package Manager
npm
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
Is this duplicate of https://github.com/vitest-dev/vitest/issues/5277?
Though global setup is executed in the main thread. Maybe we could do something like
const cwd = process.cwd()
process.chdir(dirname(<project-config-location>))
await import(<project-global-setup>)
process.chdir(cwd)
But would this just cause confusion as other fields of project's configuration are not relative to configuration file's location? 🤔
Is this duplicate of https://github.com/vitest-dev/vitest/issues/5277?
It indeed appears so - sorry I failed to find that issue when searching for duplicates.
The following seems to be a sufficient workaround for my case. It was just a surprising behavior.
export async function setup(context: GlobalSetupContext): Promise<() => Promise<void>> {
// Workaround for https://github.com/vitest-dev/vscode/issues/503
if (process.cwd() !== context.config.root) {
process.chdir(context.config.root);
}
// ...rest of global setup
}
We might be able to chdir for globalSetup like AriPerkkio suggested https://github.com/vitest-dev/vitest/issues/6706#issuecomment-2412938067 since we already does something similar during config loading, but there's still a caveat that we cannot do that for test runner itself https://github.com/vitest-dev/vitest/issues/5277#issuecomment-2051188642
In 1.5.0, the
process.cwdin the config file is now equal to the directory of that config file. Butprocess.cwdin test files still uses workspaceprocess.cwd.