vitest icon indicating copy to clipboard operation
vitest copied to clipboard

vite config `root` is ignored on workspace

Open hi-ogawa opened this issue 1 year ago • 2 comments

Describe the bug

Though we know maintaining cwd is tricky, can we respect explicit root config such as this?

// packages/client/vite.config.ts

import { defineConfig } from 'vitest/config';
import { join } from 'node:path';

export default defineConfig({
  // neither works
  root: './dir',
  // root: join(import.meta.dirname, './dir'),

  plugins: [
    {
      name: 'dump-root',
      configResolved(config) {
        // this is always "/abs-path-to/packages/client"
        // regardless of `root` config
        console.log('[dump-root]', config.root);
      },
    },
  ],
});

For example, this can affect Vite's project absolute path resolution as in the reproduction below.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-rwukz2?file=packages%2Fclient%2Fvitest.config.ts

  • npm run test fails
  • npm run test-no-workspace succeeds

System Info

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.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-react: ^4.2.1 => 4.3.3 
    @vitest/ui: latest => 2.1.4 
    vite: latest => 5.4.10 
    vitest: latest => 2.1.4

Used Package Manager

npm

Validations

hi-ogawa avatar Nov 05 '24 10:11 hi-ogawa

I think we might need this line:

https://github.com/vitest-dev/vitest/blob/cf0cbf6a37a461f9040d456aa934d13aa7fb9617/packages/vitest/src/node/create.ts#L42

Although it looks very weird, I would prefer if we rewrote this to a config hook that respects user's root option.

sheremet-va avatar Nov 05 '24 11:11 sheremet-va

Is this issue related to it? We're having trouble getting the workspace to work from the root directory (both in CLI and the extension), whereas running the projects individually works fine.

TheDutchCoder avatar Dec 18 '24 14:12 TheDutchCoder

Is this issue related to it? We're having trouble getting the workspace to work from the root directory (both in CLI and the extension), whereas running the projects individually works fine.

I think nuxt uses cwd to resolve aliases and not the root. In your reproduction, the root is set correctly.

Vitest workspace doesn't support multi-cwd projects at the moment, every projects runs in the same working directory (from where you run the command)

If you are running the command from the the folder where the config is located, Vitest cannot find any tests.

sheremet-va avatar Jan 09 '25 17:01 sheremet-va

Adding a +1 and a report from:

  • https://github.com/vitest-dev/vitest/discussions/8362

Relative paths don't work but your suggested join works:

import { defineConfig } from "vitest/config";
import path from "node:path";

export default defineConfig({
  test: {
    root: path.join(import.meta.dirname, "src"),
  },
});

fregante avatar Jul 30 '25 17:07 fregante