Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

globalSetup script is executed with wrong CWD #6706

Open
6 tasks done
maxpatiiuk opened this issue Oct 14, 2024 · 4 comments
Open
6 tasks done

globalSetup script is executed with wrong CWD #6706

maxpatiiuk opened this issue Oct 14, 2024 · 4 comments
Labels
feat: workspace Issues and PRs related to the workspace feature p2-to-be-discussed Enhancement under consideration (priority) pending triage

Comments

@maxpatiiuk
Copy link

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

@AriPerkkio
Copy link
Member

Is this duplicate of #5277?

@AriPerkkio AriPerkkio added the feat: workspace Issues and PRs related to the workspace feature label Oct 15, 2024
@AriPerkkio
Copy link
Member

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

@maxpatiiuk
Copy link
Author

Is this duplicate of #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
}

@hi-ogawa
Copy link
Contributor

We might be able to chdir for globalSetup like AriPerkkio suggested #6706 (comment) since we already does something similar during config loading, but there's still a caveat that we cannot do that for test runner itself #5277 (comment)

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 hi-ogawa added the p2-to-be-discussed Enhancement under consideration (priority) label Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: workspace Issues and PRs related to the workspace feature p2-to-be-discussed Enhancement under consideration (priority) pending triage
Projects
Status: Rejected
Development

No branches or pull requests

3 participants