Skip to content

Failed to import dynamically generated files on SSR when server.fs.cachedChecks #17760

@hi-ogawa

Description

@hi-ogawa

Describe the bug

Related issue on Vitest vitest-dev/vitest#6152

Importing dynamically generated js files during runtime seems to cause Error: Failed to load url ....
It works when setting server.fs.cachedChecks: true.

  • src/repro.js
import { writeFile } from "node:fs/promises";

async function main() {
	await writeFile("src/generated.js", "export default 'hello'");
	const mod = await import("./generated.js");
	console.log(mod);
}

await main();
  • repro-vite.js
import { createServer } from "vite";

async function main() {
	const server = await createServer({
		configFile: false,
		server: {
			fs: {
				// works when disabling `cachedChecks`
				// cachedChecks: false,
			},
		},
	});
	await server.ssrLoadModule("/src/repro.js");
        // oddly it works if load full path
        // await server.ssrLoadModule(path.resolve("src/repro.js")))
}

main();
  • Command
$ rm -f src/generated.js && node repro-vite.js
Error: Failed to load url /src/generated.js (resolved id: /src/generated.js). Does the file exist?
    at loadAndTransform (file:///home/projects/gqairrmzbn.github/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-D8YhmIY-.js:52451:17)
    at async instantiateModule (file:///home/projects/gqairrmzbn.github/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-D8YhmIY-.js:53403:44) {
  code: 'ERR_LOAD_URL'
}

Node.js 18.20.3

Reproduction

https://github.com/hi-ogawa/reproductions/tree/main/vitest-plugin-react-v2

Steps to reproduce

Open https://stackblitz.com/github/hi-ogawa/reproductions/tree/main/vitest-plugin-react-v2?file=README.md and run node repro-vite.js.

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:
    vite: 5.3.4 => 5.3.4

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p4-importantViolate documented behavior or significantly improves performance (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions