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

"cwd" is workspace root when running tests in sub directory projects #5277

Open
6 tasks done
hi-ogawa opened this issue Feb 23, 2024 · 11 comments
Open
6 tasks done

"cwd" is workspace root when running tests in sub directory projects #5277

hi-ogawa opened this issue Feb 23, 2024 · 11 comments
Labels
feat: workspace Issues and PRs related to the workspace feature pending triage

Comments

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Feb 23, 2024

UPDATE: current workaround with pool: "forks" and setupFiles looks like this https://stackblitz.com/edit/vitest-dev-vitest-xedeud?file=packages%2Flib1%2Fvite.config.ts


Describe the bug

I've seen a similar behavior while I was investigating #5003, but I suppose this is not an intended behavior according to vitest-dev/vscode#254 (comment)

In the reproduction, I also checked cwd when loading config. Following logs are from running console.log({ url: import.meta.url, cwd: process.cwd() }) in config files and test files:

❯ npx vitest run
{
  url: 'file:///home/projects/vitest-dev-vitest-4fbeln/packages/lib1/vite.config.ts',
  cwd: '/home/projects/vitest-dev-vitest-4fbeln'
}
{
  url: 'file:///home/projects/vitest-dev-vitest-4fbeln/packages/lib2/vite.config.ts',
  cwd: '/home/projects/vitest-dev-vitest-4fbeln'
}

 RUN  v1.3.1 /home/projects/vitest-dev-vitest-4fbeln

stdout | test/square.test.ts > square
{
  url: 'file:///home/projects/vitest-dev-vitest-4fbeln/packages/lib2/test/square.test.ts',
  cwd: '/home/projects/vitest-dev-vitest-4fbeln'
}

stdout | test/double.test.ts > double
{
  url: 'file:///home/projects/vitest-dev-vitest-4fbeln/packages/lib1/test/double.test.ts',
  cwd: '/home/projects/vitest-dev-vitest-4fbeln'
}

 ✓ |lib2| test/square.test.ts (1)
 ✓ |lib1| test/double.test.ts (1)

 Test Files  2 passed (2)
      Tests  2 passed (2)
   Start at  10:08:30
   Duration  2.30s (transform 67ms, setup 0ms, collect 54ms, tests 6ms, environment 0ms, prepare 598ms)

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-4fbeln?file=packages%2Flib2%2Ftest%2Fsquare.test.ts

System Info

(stackblitz)

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.14.0 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 1.3.1 
    vite: latest => 5.1.4 
    vitest: latest => 1.3.1

Used Package Manager

npm

Validations

@hi-ogawa hi-ogawa added pending triage feat: workspace Issues and PRs related to the workspace feature p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Feb 23, 2024
@hi-ogawa hi-ogawa changed the title "cwd" is fixed to workspace root when running tests in sub directory projects "cwd" is workspace root when running tests in sub directory projects Feb 23, 2024
@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Feb 23, 2024

Actually this is impossible for node:worker_threads pool? nodejs/node#41673
Doing this only for forks would be inconsistent and more confusing, so this may be wontfix with adding some documentation.

@hi-ogawa hi-ogawa added pending triage and removed p3-minor-bug An edge case that only affects very specific usage (priority) labels Feb 23, 2024
@sheremet-va
Copy link
Member

Hm, I thought the problem was with config.root, not with process.cwd(). Yes, it is impossible to change the cwd for workspace projects. The best solution we can give is to manually set the working directory in setupFiles of a specific workspace with process.chdir and enable pool forks for that project.

@hi-ogawa hi-ogawa added the documentation Improvements or additions to documentation label Feb 26, 2024
@chriswheeldon-peakon
Copy link
Contributor

chriswheeldon-peakon commented Feb 26, 2024

Before I found that this issue had already been created I was creating a reproducible example for it here: https://github.com/chriswheeldon-peakon/vitest-workspace-cwd. Just in case that's useful for anyone.

My two cents is that I do think that this leaves vitest frustratingly at odds with other monorepo executors (e.g. npm -w and nx).

For me the problem has arisen with packages that are using the config package (https://www.npmjs.com/package/config). This expects a config directory to exist within the working directory. The config directory can be overriden using an environment variable so a workaround is to force that environment variable in a per-workspace setup file e.g. what I've done here: chriswheeldon-peakon/vitest-workspace-cwd@2999c23. This isn't pretty and I don't know yet if there is a more elegant way of getting the desired behaviour.

@hi-ogawa hi-ogawa removed the documentation Improvements or additions to documentation label Feb 26, 2024
@qraynaud
Copy link

qraynaud commented Apr 9, 2024

I found this solution I find elegant since it works for every pool type, maybe it could be streamlined into vitest :

vitest.config.mts:

export default defineProject({
  test: {
    // Setup
    setupFiles: ["./vitest.chdir.mts", /* anything */],
  }
});

vitest.chdir.mts:

import process from "process";

import { vi } from "vitest";

vi.spyOn(process, "cwd").mockReturnValue(new URL(".", import.meta.url).pathname);

@paul-vd
Copy link

paul-vd commented Apr 11, 2024

This is also problematic when you have plugins vite plugins which rely on the process.cwd

@hi-ogawa
Copy link
Contributor Author

This is also problematic when you have plugins vite plugins which rely on the process.cwd

@paul-vd Can you share how plugins use process.cwd? Vitest workspace cannot support that unfortunately, but it might be possible to approximate that by config.root, which plugins can get by configResolved(config) { config.root }.

@sheremet-va
Copy link
Member

sheremet-va commented Apr 12, 2024

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.

@qraynaud
Copy link

@sheremet-va is there a rationale for this? I think it should be also changed for tests.

@sheremet-va
Copy link
Member

@sheremet-va is there a rationale for this? I think it should be also changed for tests.

Technical limitations. Workers do not support process.chdir API.

@qraynaud
Copy link

Yeah but you can replace process.cwd by a custom implementation no?

@spence-novata
Copy link

I'm also running into this issue due to to node-config using process.cwd().

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 pending triage
Projects
None yet
Development

No branches or pull requests

6 participants