kit icon indicating copy to clipboard operation
kit copied to clipboard

`read` cutting first character of path

Open Torbet opened this issue 2 years ago • 1 comments

Describe the bug

when using import { read } from '$app/server';, the first character of the path gets cut off.

I have a map.pgm in src/lib/map.pgm (but same for any path), and calling read('src/lib/map.pgm) gives:

Error: Asset does not exist: rc/lib/map.pgm

fs.readFileSync works correctly.

Calling read('ssrc/lib/map.pgm) (adding extra s at the start) does not error.

Reproduction

Described above.

Logs

Error: Asset does not exist: rc/lib/map.pgm

System Info

System:
    OS: macOS 14.3.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 187.16 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.6.2 - /opt/homebrew/bin/node
    npm: 10.2.4 - /opt/homebrew/bin/npm
  Browsers:
    Safari: 17.3.1
  npmPackages:
    @sveltejs/adapter-node: ^4.0.1 => 4.0.1 
    @sveltejs/kit: ^2.0.0 => 2.5.0 
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.2 
    svelte: ^4.2.7 => 4.2.10 
    vite: ^5.0.3 => 5.0.12

Severity

serious, but I can work around it

Additional Information

No response

Torbet avatar Feb 19 '24 13:02 Torbet

This is not the correct way to be using read(), although it doesn't look like we're documenting this well currently, as far as I can tell.

read() should be passed the path generated by Vite upon importing an asset, not just a path to the file. Vite needs to know about the file so that it can be properly included in your production build at build-time. This is the problem read() was designed to solve. Just reading the file from src at runtime won't work, because it won't be there anymore in the built production app.

Conduitry avatar Feb 19 '24 15:02 Conduitry