fix: force remote module chunks to isolate themselves
#14571
Closing issue
Describe the bug
Running vite build works in sveltekit @2.38.1 but fails in 2.39.0 -> 2.41.0 versions the issue appears to occur due to changes in https://github.com/sveltejs/kit/blob/3120d170b157b5a00369b3ee66d3cbe4d3c6fc03/packages/kit/src/exports/vite/index.js
These are the errors I am seeing:
[plugin vite-plugin-sveltekit-remote] Sourcemap is likely to be incorrect: a plugin (vite-plugin-sveltekit-remote) was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help (x4)
and
node:internal/event_target:1101
process.nextTick(() => { throw err; });
^
ReferenceError [Error]: Cannot access 'info_v2' before initialization
at file:///Users/em/Dev/app/.svelte-kit/output/server/chunks/remote-eooeoj.js:1288:91
at ModuleJob.run (node:internal/modules/esm/module_job:345:25)
at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:651:26)
at async analyse (file:///Users/em/Dev/app/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:51:19)
at async MessagePort.<anonymous> (file:///Users/em/Dev/app/node_modules/@sveltejs/kit/src/utils/fork.js:23:16)
Reproduction
This is difficult to reproduce as it seems to only occur when the generated remote files are large and split into multiple files.
Logs
System Info
System:
OS: macOS 15.6
CPU: (12) arm64 Apple M2 Max
Memory: 988.89 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.19.0 - ~/.volta/tools/image/node/22.19.0/bin/node
Yarn: 4.9.4 - ~/.volta/tools/image/yarn/4.9.4/bin/yarn
npm: 11.6.0 - ~/.volta/tools/image/npm/11.6.0/bin/npm
bun: 1.2.0 - ~/.volta/bin/bun
Browsers:
Brave Browser: 121.1.62.156
Edge: 140.0.3485.66
Safari: 18.6
npmPackages:
@sveltejs/adapter-node: ^5.3.2 => 5.3.2
@sveltejs/enhanced-img: ^0.8.1 => 0.8.1
@sveltejs/kit: 2.41.0 => 2.41.0
@sveltejs/vite-plugin-svelte: ^6.2.0 => 6.2.0
svelte: 5.38.10 => 5.38.10
vite: 7.1.5 => 7.1.5
Severity
blocking an upgrade
Additional Information
Not that this helps much but the line in question occurs in what is essentially a compiled version of a bunch of drizzle queries in one big file. Specifically it looks like this.
const { date: _info_date, ...provider_info_v2_columns } = getTableColumns(info_v2);
Well above that line 1288, on line 27 is
import { i as info_v2, ... } from "./remote-12p15ba.js";
And that remote file seems to be the drizzle schema, at the end of the file.
export {
info_v2 as i,
...
}
So the output seems correct but for whatever reason it is complaining that the drizzle table has not yet been initialized, which leads me to believe it is attempting to run the code in the build environment without the imports.
Pull request
This is my attempt to provide an alternative to #14456, which is extremely clever but makes me very nervous. It works, but as soon as another plugin tries the same trick (repeatedly getting a list of every module in the graph and loading it, until there are no new modules to load) you end up in a deadlock situation. The 30 second timeout really isn't a solution. It's almost certain that very few users would ever encounter this, but for the ones that did it would be a very confusing and annoying bug.
This PR sticks with the manualChunks trick, but adds another layer: any modules imported by .remote.ts files are also put in their own chunks. This has the effect of forcing each chunk corresponding to a .remote.ts module to only contain that module, and none of the dependencies that Rollup incorrectly deems safe to include in the chunk.
My hunch — or at least, my hope — is that Rollup gets confused about how to structure the graph because it's not expecting these chunks to be treated as entries (resulting in the bad kind of circular dependency) and that this change renders that moot. It's possible that I haven't fixed it at all, but have rather pushed the bug 'one level down' so to speak, but I think it's worth a shot.
Fixes #14444 and possibly #14430
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- This message body should clearly illustrate what problems it solves.
- Ideally, include a test that fails without this PR but passes with it.
Tests
- Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.
Edits
- Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.
Info
🦋 Changeset detected
Latest commit: 7b831dd
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
| Name | Type |
|---|---|
| @sveltejs/kit | Patch |
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
Pro tip: You can prefix GitHub URLs of issues, PRs or discussions with svcl.dev/ to view them on this page! Also try it on a GitHub release ;)