fix(build): RangeError - Maximum call stack size exceeded
#14450
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
It is currently possible for a chunk to import chunks that import itself. I'm not exactly sure where this occurs or why as I can't reproduce it in a simple repository, but a simple fix is to prevent importing already visited chunks.
This addresses #14444
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: 5dacc73
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
This doesn't address why remote functions are being chunked oddly, it does however, speed up the build for any chunks that reference a previous referenced chunk. Which is to say, aside from not doom looping, this also doesn't reprocess chunks when other chunks converge on them. E.G.
flowchart
chunks --> chunk1
chunks --> chunk2
chunks --> chunk3
chunk1 --> chunk4
chunk2 --> chunk4
chunk3 --> chunk4
chunk4 --> chunk5
Without the PR chunk4 and chunk5 will be processed three times.
This also bails out when chunks reference eachother in a loop allowing the build to finish.
flowchart LR
chunk1 --> chunk2
chunk2 --> chunk3
chunk3 --> chunk4
chunk4 --> chunk1
It is currently possible for a chunk to import chunks that import itself. I'm not exactly sure where this occurs or why as I can't reproduce it in a simple repository, but a simple fix is to prevent importing already visited chunks.
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 ;)