vitest: process.chdir() is not supported in workers (--no-threads doesn't work)

Describe the bug

When I try to call process.chdir in a test, I get “TypeError: process.chdir() is not supported in workers”. From #566 I understand the solution is passing --no-threads but it doesn’t seem to work.

Reproduction

https://stackblitz.com/edit/node-ve8ywd?file=package.json

System Info

System:
  OS: Linux 5.4 Linux Mint 20.3 (Una)
  CPU: (8) x64 Intel(R) Core(TM) i7-3630QM CPU @ 2.40GHz
  Memory: 3.70 GB / 15.52 GB
  Container: Yes
  Shell: 5.0.17 - /bin/bash
Binaries:
  Node: 18.2.0 - /usr/local/bin/node
  Yarn: 1.22.11 - ~/.npm-global/bin/yarn
  npm: 8.5.1 - ~/.npm-global/bin/npm
Browsers:
  Chrome: 102.0.5005.61
  Firefox: 101.0
npmPackages:
  vitest: ^0.13.1 => 0.13.1

Used Package Manager

pnpm

Validations

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

So what’s the path forward here? Simply not using process.chdir doesn’t seem like a reasonable forward path.

The use case we currently support in many packages is creating a tmp dir or npm package using something like fixturify-project, and use that directory as a fixture for tests. At the end of the test, we dispose of the test directory, which requires us to switch directories before to support OS tests such as windows-latest, which will error if you try to rm a directory when it’s the current dir.

This seems like a reasonably common usage, and we were previously using --no-threads with vitest when we needed to support this scenario. Now that --no-threads no longer works for this use case, there doesn’t seem to be a workaround.

I understand that this is a larger issue with threads and changing directories, but it seems like we could collectively figure out a solution.