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

(v2) Error: Failed to load url when dynamic importing js files generated during tests with relative path #6152

Closed
6 tasks done
hi-ogawa opened this issue Jul 17, 2024 · 6 comments
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Jul 17, 2024

Describe the bug

I was investigating a test failure when upgrading Vitest v2 in vite-plugin-react repo vitejs/vite-plugin-react#347 and this seems to be the cause.

//// src/repro.test.ts

// this works
test("absolute ok", async () => {
	await writeFile("src/generated.js", "export default 'hello'");
	const mod = await import(path.join(import.meta.dirname, "./generated.js"));
	console.log(mod);
});

// this is error
test("relative error", async () => {
	await writeFile("src/generated.js", "export default 'hello'");
	const mod = await import("./generated.js");
	console.log(mod);
});

Reproduction

code: https://github.com/hi-ogawa/reproductions/tree/main/vitest-plugin-react-v2
stackblitz: https://stackblitz.com/github/hi-ogawa/reproductions/tree/main/vitest-plugin-react-v2?file=src%2Frepro.test.ts

$ rm src/generated.js
$ npx vitest -t relative

 FAIL  src/repro.test.ts > relative error
Error: Failed to load url ./generated.js (resolved id: ./generated.js) in /home/projects/rlmibvxopx.github/src/repro.test.ts. Does the file exist?
 ❯ loadAndTransform node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-D8YhmIY-.js:52451:17

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.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:
    vitest: 2.0.3 => 2.0.3

Used Package Manager

pnpm

Validations

@hi-ogawa hi-ogawa changed the title Error: Failed to load url when dynamic importing js files generated during tests with relative path (v2) Error: Failed to load url when dynamic importing js files generated during tests with relative path Jul 17, 2024
@sheremet-va
Copy link
Member

Are you sure this is Vitest 2 issue, and not Vite 5? The error happens during static analysis and seems legit to me.

@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Jul 17, 2024

I'm not really sure and it's hard to believe, but I can only reproduce from Vitest v2 while Vite version is pinned (as latest 5.3.4).

I haven't debug the issue much, but just one surprising thing I found is that the test just succeeded when I added VITE_NODE_DEBUG_DUMP=1 npx vitest -t relative.

@hi-ogawa
Copy link
Contributor Author

Hmm, after double checking reproduction, it doesn't look like this fails always 100% (but almost 95% locally).
Now after setting server.fs.cachedChecks: false, it looks like the test is passing 100%, so the issue might be some race condition.

I'll check if I can reproduce it on Vite SSR alone.

@toomuchdesign
Copy link

Running into the same issue (and only after Vitest v2 update).
Setting server.fs.cachedChecks: false seems to fix it.

Relevant PR:
toomuchdesign/openapi-ts-json-schema#289

@sheremet-va sheremet-va added p2-to-be-discussed Enhancement under consideration (priority) and removed pending triage labels Jul 22, 2024
@sheremet-va sheremet-va moved this to P2 - 5 in Team Board Jul 22, 2024
@sheremet-va sheremet-va added p3-minor-bug An edge case that only affects very specific usage (priority) and removed p2-to-be-discussed Enhancement under consideration (priority) labels Jul 22, 2024
@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Jul 25, 2024

I filed an issue on Vite vitejs/vite#17760.
I'm still not sure why this started to show up since Vitest v2. I bisected and it really seems to happen from first beta v2.0.0-beta.1, but I don't have a clue what's affecting it https://github.com/vitest-dev/vitest/releases/tag/v2.0.0-beta.1.

At least this seems to be an inconsistent behavior depending on server.fs.cachedChecks, so if it's fixed there, then Vitest side should be happy.

@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Jul 30, 2024

I found why this started to happen since Vitest v2. It looks like Vite disables "cached fs" when users had custom server.watch.ignored, which Vitest had it before v2 but removed in v2.0.0-beta.1 vitejs/vite#15920

@sheremet-va sheremet-va moved this from P2 - 5 to Discussing in Team Board Aug 1, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
Archived in project
Development

No branches or pull requests

3 participants