sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.77k stars 1.96k forks source link

throw error(500, message) results in "internal error" instead of message when sveltekit() is called elsewhere #9029

Open wvhulle opened 1 year ago

wvhulle commented 1 year ago

Describe the bug

I have a monorepo with several sveltekit subprojects. To recycle my setup I have created a viteconfig that is share among the subprojects. This viteconfig is not inside the same directory as svelte.config.js.

To see the error, execute npx run dev or pnpm exec vite dev. The internal error that you can see in the browser should not be displayed. Instead, the user provided error should be shown.

The bug disappears when sveltekit() is called inside the root directory (as would be the case in the default setup). Looking into the source code, the function sveltekit or load_config depends on the cwd. However, changing the cwd before calling sveltekit() does change the error. Instead of showing the error in the browser using the fallback error page, the browser displays "internal error" and the terminal window show that the server outputs "undefined" when the error is thrown.

This bug means that I have to duplicate my vite config across different subprojects in a monorepo because the sveltekit() function does not follow cwd (when it comes to errors). It seems like other things, apart from the sveltekit error, run.

Reproduction

https://github.com/wvhulle/sveltekit-error-page-vite-plugin

Logs

wvhulle@work ~/D/s/subapp (master) [1]> pnpm run dev

> sveltekit-error-page-vite-plugin@0.0.1 dev /home/wvhulle/Documents/sveltekit-error-page-vite-plugin/subapp
> vite dev

  VITE v4.1.1  ready in 1620 ms

  ➜  Local:   http://localhost:3000/
  ➜  Network: use --host to expose
  ➜  press h to show help
undefined
undefined
2:30:08 PM [vite-plugin-svelte] ssr compile done.
package                             files     time     avg
sveltekit-error-page-vite-plugin        1   29.0ms  29.0ms
@sveltejs/kit                           2    3.9ms   1.9ms
undefined

### System Info

```Shell
System:
    OS: Linux 5.15 Linux Mint 21.1 (Vera)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
    Memory: 6.90 GB / 15.36 GB
    Container: Yes
    Shell: 3.5.1 - /usr/bin/fish
  Binaries:
    Node: 19.4.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.2.0 - /usr/local/bin/npm
  Browsers:
    Chromium: 109.0.5414.119
    Firefox: 109.0.1
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.0.0 
    @sveltejs/kit: ^1.5.0 => 1.5.6 
    svelte: ^3.54.0 => 3.55.1 
    vite: ^4.0.0 => 4.1.1 


### Severity

annoyance

### Additional Information

I suspect that this bug could be solved with the possibility of an inline config in vite.
https://github.com/sveltejs/kit/issues/5485
However, since changing the cwd does not solve this and the rest keeps working, I suspect this is related more to the implementation of the throw error.
wvhulle commented 1 year ago

I would like to pass cwd to https://github.com/sveltejs/kit/blob/9259bbad26ae593e82becd74a745214e41f654a9/packages/kit/src/exports/vite/index.js#L113 but it is disabled.

wvhulle commented 1 year ago

Actually after reading some more, and testing process.cwd, it seems like process.cwd has no effect, so it will have be solved in another way.

Rich-Harris commented 1 year ago

It's critical that your app and your config are using the same 'copy' of SvelteKit, otherwise this code, which ensures that error(...) objects are correctly identified as errors that are safe to display to the user...

https://github.com/sveltejs/kit/blob/f0407dce7dd942a643e88ade6a3374041341ba64/packages/kit/src/exports/vite/dev/index.js#L340-L352

...will not work. There are two ways you could do this:

  1. only have @sveltejs/kit installed in the project root, not in subapp etc
  2. use linking: "@sveltejs/kit": "link:../node_modules/@sveltejs/kit" (works with pnpm, other package managers will differ)

Perhaps there's a way we can detect this situation and throw a useful error.

wvhulle commented 1 year ago

Hi, thanks! I hadn't thought about that. I will test it out. Meanwhile I forked kit and adapted the sveltekit() vite plugin to also take arguments which solves some of these and related problems.