fix: rework lazily remote function files discovery
#14456
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
Instead of using manualChunks which turns out to cause some weird chunking bugs, we instead use a virtual module which we force to be loaded at the very end after all other modules have been discovered. By that time we have collected all remote function files and can generated a record of hash->import. Vite will then do the proper chunking without further interference by us. Thanks @bluwy for the tip!
Fixes #14444 and possibly #14430 and #14519
Putting this into draft because while working on this I discovered that remote functions in node_modules are not loaded at dev time - but this is not new to this PR, it's failing on main, too. Investigating. Also fixes a bug in dev mode where remote functions might not be transformed if Vite decides to append a query parameter (we checked if the file ends with .remote.js/ts but they could end with .remote.js/ts?v=123)
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:.
Info
🦋 Changeset detected
Latest commit: 42ae134
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
Instead of using manualChunks which turns out to cause some weird chunking bugs, we instead use a virtual module which we force to be loaded at the very end after all other modules have been discovered. By that time we have collected all remote function files and can generated a record of hash->import. Vite will then do the proper chunking without further interference by us. Fixes #14444 and possibly #14430
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 ;)