Skip to content

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

@hi-ogawa

Description

@hi-ogawa

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    Status

    Discussing

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions