vitest icon indicating copy to clipboard operation
vitest copied to clipboard

globalSetup script is executed with wrong CWD

Open maxpatiiuk opened this issue 1 year ago • 4 comments

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

  1. Clone this repository & install dependencies (or use Stackblitz):

    git clone https://github.com/maxpatiiuk/vitest-wrong-cwd
    cd vite-wrong-cwd
    npm install
    
  2. Run npx vitest run from monorepo root. See that the vite.config.ts is correctly executed with CWD set to package root. At the same time, globalSetup script 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

maxpatiiuk avatar Oct 14 '24 17:10 maxpatiiuk

Is this duplicate of https://github.com/vitest-dev/vitest/issues/5277?

AriPerkkio avatar Oct 15 '24 05:10 AriPerkkio

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? 🤔

AriPerkkio avatar Oct 15 '24 05:10 AriPerkkio

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
}

maxpatiiuk avatar Oct 16 '24 03:10 maxpatiiuk

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.cwd in the config file is now equal to the directory of that config file. But process.cwd in test files still uses workspace process.cwd.

hi-ogawa avatar Oct 16 '24 03:10 hi-ogawa