sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
18.43k stars 1.88k forks source link

Vite deduplication causing problems with non-deterministic asset order #2456

Open mvolfik opened 2 years ago

mvolfik commented 2 years ago

Describe the bug

Vite (thankfully) deduplicates byte-to-byte equal asset files. In my project I create the assets dynamically with a custom plugin using pluginContext.emitFile(). However, I heavily use async, so the order of the emit calls is not deterministic, therefore the single file from vite deduplication has different name in client-side and server-side code. And I build with adapter-static, which crawls ssr code, but copies client assets, so it finds a non-existing file.

Reproduction

https://github.com/mvolfik/probable-waffle

In plugin.js, the same file is emitted for both imports with different name. Manual delays are added to emit kitten2 first in ssr mode, and kitten1 otherwise.

Logs

$ svelte-kit build
vite v2.5.8 building for production...
transforming (1) .svelte-kit/build/components/layout.svelte
✓ 16 modules transformed.
.svelte-kit/output/client/_app/assets/kitten1-d3409436          9.52 KiB
.svelte-kit/output/client/_app/manifest.json                    1.26 KiB
.svelte-kit/output/client/_app/layout.svelte-99156149.js        0.53 KiB / brotli: 0.31 KiB
.svelte-kit/output/client/_app/error.svelte-4b13167a.js         1.55 KiB / brotli: 0.64 KiB
.svelte-kit/output/client/_app/pages/index.svelte-df531a7b.js   1.25 KiB / brotli: 0.55 KiB
.svelte-kit/output/client/_app/assets/start-61d1577b.css        0.16 KiB / brotli: 0.11 KiB
.svelte-kit/output/client/_app/chunks/vendor-82fce94c.js        7.07 KiB / brotli: 2.63 KiB
.svelte-kit/output/client/_app/start-f0c4fa82.js                17.64 KiB / brotli: 5.62 KiB
vite v2.5.8 building SSR bundle for production...
transforming (1) .svelte-kit/build/app.js
✓ 16 modules transformed.
.svelte-kit/output/server/app.js   51.12 KiB

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-static
> 404 /_app/assets/kitten2-aa8efc02 (linked from /)
Error: 404 /_app/assets/kitten2-aa8efc02 (linked from /)
    at file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:86:11
    at visit (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:213:5)
    at async visit (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:281:6)
    at async prerender (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:291:6)
    at async Object.prerender (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:352:4)
    at async adapt (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/adapter-static/index.js:13:4)
    at async adapt (file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/chunks/index4.js:377:2)
    at async file:///home/volfmatej/temp/nondet-plugin/node_modules/@sveltejs/kit/dist/cli.js:877:5
error Command failed with exit code 1.

(removed irrelevant warnings for missing <img alt )

System Info

System:
    OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (4) x64 Intel(R) Core(TM) i3-3110M CPU @ 2.40GHz
    Memory: 290.81 MB / 7.35 GB
    Container: Yes
    Shell: 3.1.0 - /usr/bin/fish
  Binaries:
    Node: 14.17.6 - /usr/bin/node
    Yarn: 1.22.11 - ~/.local/bin/yarn
    npm: 7.21.0 - ~/.local/bin/npm
  Browsers:
    Chromium: 93.0.4577.82
    Firefox: 92.0
  npmPackages:
    @sveltejs/adapter-static: next => 1.0.0-next.19 
    @sveltejs/kit: next => 1.0.0-next.169 
    svelte: ^3.42.6 => 3.42.6

Severity

blocking an upgrade

Additional Information

No response

Rich-Harris commented 2 years ago

Is this still occurring? I cloned the repro and updated svelte.config.js to support newer versions of SvelteKit, and it built fine — the outputted index.html looks like this...

<p><img src="/_app/assets/kitten1-d3409436"></p>
<p><img src="/_app/assets/kitten1-d3409436"></p>

...and _app/assets contains kitten1-d3409436 as expected. It's possible that it was fixed in Vite since this issue was raised?

mvolfik commented 2 years ago

Thanks for checking. The issue is unfortunately still there - it just seemed to disappear because Vite 2.7.0 slightly changed the plugin API (ssr: bool became options?: { ssr?: bool }, so the comparison was always false. I pushed updated version of the reproduction repo, which reproduces the bug with latest dependencies

Rich-Harris commented 1 year ago

Finally took another look at this and yes, it's still happening. I don't think it's in SvelteKit's power to fix though, this is a Vite/Rollup issue. It might be worth trying to create a non-SvelteKit repro and filing it on the Vite tracker if you're still affected by this.