vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.93k stars 1.16k forks source link

`cacheDir` not honored if `deps.optimizer.{mode}.enabled` is true #6733

Open Pistonight opened 1 day ago

Pistonight commented 1 day ago

Describe the bug

cacheDir not honored if deps.optimizer.{mode}.enabled is true. vitest will save cache to node_modules/.vite as if cacheDir is not specified.

Reproduction

Run

npm create vite@latest
npm i

Edit the config

/// <reference types="vitest/config" />
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  cacheDir: ".vite",
  plugins: [react()],
  test: {
    deps: {
      optimizer: {
        ssr: {
          enabled: true // toggling this to repro
        }
      }
    }
  }
})

Create a test src/App.test.tsx

import { describe, expect, test } from "vitest";
describe("App", () =>{
    test("works", () => {
        expect(1).not.toBe(2);
    });
});

Run

npx vitest
  1. Observe cache is unexpectedly created in node_modules/.vite.
  2. Toggle test.deps.optimizer.ssr.enabled to false, and run npx vitest again
  3. Observe cache is created in .vite as expected

System Info

System:
    OS: Windows 11 10.0.26120
    CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
    Memory: 41.33 GB / 63.91 GB
  Binaries:
    Node: 18.20.4 - ~\dotbin\extra\portable\nvm\symlink\node.EXE
    npm: 10.7.0 - ~\dotbin\extra\portable\nvm\symlink\npm.CMD
    bun: 1.1.6 - ~\.bun\bin\bun.EXE
  Browsers:
    Edge: Chromium (127.0.2651.86)
    Internet Explorer: 11.0.26100.1
  npmPackages:
    @vitejs/plugin-react: ^4.3.2 => 4.3.2
    vite: ^5.4.8 => 5.4.9
    vitest: ^2.1.3 => 2.1.3

Used Package Manager

npm

Validations

Pistonight commented 1 day ago

I did notice that the deprecated cache.dir option behave correctly.

Seems like it's not checking vite cacheDir here but it should?

https://github.com/vitest-dev/vitest/blob/a939779f1db473f8be3de8454bc94de70cfe5b97/packages/vitest/src/node/plugins/utils.ts#L44-L64

sheremet-va commented 1 day ago

Seems like it's not checking vite cacheDir here but it should?

Looks like a bug indeed. PR is welcome!

Pistonight commented 1 day ago

Thanks for confirming, just to clarify my understanding: how do these 3 properties relate and resolve exactly?

  1. Vite cacheDir
  2. Vitest server.deps.cacheDir
  3. Vitest cache.dir
hi-ogawa commented 22 hours ago

As par the change https://github.com/vitest-dev/vitest/pull/5229, the intent is to put things under cacheDir + '/vitest'. Vitest users don't need to configure server.deps.cacheDir and the others should mostly look like:

const finalCacheDir = cache.dir ?? join(cacheDir, "vitest")
                      //                ^^^^^^^^
                      // this is probably hard-coded as `node_modules/.vite`,
                      // which is a bug

By default Vite has cacheDir = 'node_modules/.vite', so Vitest uses node_modules/.vite/vitest. If you explicitly set cacheDir = '.vite', Vitest should probably use .vite/vitest.