Skip to content

Server asset path mismatch when running vite preview #11020

@teemingc

Description

@teemingc

Describe the bug

Typically, we're suppose to access a server asset by joining process.cwd() and the path of the asset imported and handled by Vite.

// +page.server.js
import path from 'node:path';
import fs from 'node:fs';
import text_file from './file.txt';

export async function load() {
    // we can get the absolute path with `process.cwd()`
	const filepath = path.join(process.cwd(), text_file);
	const text = fs.readFileSync(filepath, { encoding: 'utf-8' });
	return { text };
}

However, this doesn't work when running vite preview or node build with the Node adapter.

// +page.server.js
import path from 'node:path';
import fs from 'node:fs';
import text_file from './file.txt';

export async function load() {
    // the filepath is incorrect because the path returned by `process.cwd()` is not near the server assets.
    // So we need to change the path when not in dev??
	const filepath = path.join(process.cwd(), text_file);
	const text = fs.readFileSync(filepath, { encoding: 'utf-8' });
	return { text };
}

This can be fixed by changing the current directory when vite preview is run. Hence, the filepath would correctly return /.svelte-kit/output/server/_app/... instead of the project root /_app/... which doesn't exist.

const dir = join(svelte_config.kit.outDir, 'output/server');

if (!fs.existsSync(dir)) {
	throw new Error(`Server files not found at ${dir}, did you run \`build\` first?`);
}

+ // always set the working directory to where our server assets are located
+ process.chdir(dir)

https://github.com/sveltejs/kit/blob/master/packages/kit/src/exports/vite/preview/index.js

A similar thing occurs when we use adapter-node. The path returned by process.cwd() keeps changing depending on from which directory we start the server. This could be considered the intended behaviour, but it also leads to unwanted behaviour (such as being unable to correctly resolve the filepath as shown in the example above).

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-y1duvx?file=src%2Froutes%2F%2Bpage.server.js

  1. Run the application with pnpm dev, this has no issue.
  2. Build the application with pnpm build.
  3. Preview the build with pnpm preview. The page cannot render because the filepath is incorrect in the load function.

Logs

Error: ENOENT: no such file or directory, open '/home/projects/sveltejs-kit-template-default-y1duvx/_app/immutable/assets/file.c0535e4b.txt'
    at Object.openSync (https://sveltejskittemplatedefaulty1du-uwot.w-credentialless.staticblitz.com/blitz.5a198b5c.js:49:16217)
    at Object.readFileSync (https://sveltejskittemplatedefaulty1du-uwot.w-credentialless.staticblitz.com/blitz.5a198b5c.js:49:17820)
    at load (file:///home/projects/sveltejs-kit-template-default-y1duvx/.svelte-kit/output/server/entries/pages/_page.server.js:21:32)
    at load_server_data (file:///home/projects/sveltejs-kit-template-default-y1duvx/.svelte-kit/output/server/index.js:1119:42)
    at eval (file:///home/projects/sveltejs-kit-template-default-y1duvx/.svelte-kit/output/server/index.js:2537:24) {
  syscall: 'open',
  errno: -2,
  code: 'ENOENT',
  path: '/home/projects/sveltejs-kit-template-default-y1duvx/_app/immutable/assets/file.c0535e4b.txt'
}
Error: ENOENT: no such file or directory, open '/home/projects/sveltejs-kit-template-default-y1duvx/_app/immutable/assets/file.c0535e4b.txt'
    at Object.openSync (https://sveltejskittemplatedefaulty1du-uwot.w-credentialless.staticblitz.com/blitz.5a198b5c.js:49:16217)
    at Object.readFileSync (https://sveltejskittemplatedefaulty1du-uwot.w-credentialless.staticblitz.com/blitz.5a198b5c.js:49:17820)
    at load (file:///home/projects/sveltejs-kit-template-default-y1duvx/.svelte-kit/output/server/entries/pages/_page.server.js:21:32)
    at load_server_data (file:///home/projects/sveltejs-kit-template-default-y1duvx/.svelte-kit/output/server/index.js:1119:42)
    at eval (file:///home/projects/sveltejs-kit-template-default-y1duvx/.svelte-kit/output/server/index.js:2537:24) {
  syscall: 'open',
  errno: -2,
  code: 'ENOENT',
  path: '/home/projects/sveltejs-kit-template-default-y1duvx/_app/immutable/assets/file.c0535e4b.txt'
}

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.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.9.2 - /usr/local/bin/pnpm
  npmPackages:
    @sveltejs/adapter-node: ^1.3.1 => 1.3.1 
    @sveltejs/kit: ^1.20.4 => 1.27.5 
    svelte: ^4.0.5 => 4.2.3 
    vite: ^4.4.2 => 4.5.0

Severity

serious, but I can work around it

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions