Skip to content

fix: eagerly initialise dynamic env vars from process.env at module-eval time - #16303

Merged
teemingc merged 15 commits into
version-3from
elliott/eager-env-init
Sep 1, 2026
Merged

fix: eagerly initialise dynamic env vars from process.env at module-eval time#16303
teemingc merged 15 commits into
version-3from
elliott/eager-env-init

Conversation

@elliott-with-the-longest-name-on-github

@elliott-with-the-longest-name-on-github elliott-with-the-longest-name-on-github commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

closes #14286

Dynamic $app/env values are normally populated when Server.init() calls set_env(). The problem is that instrumentation.server.js deliberately runs before Server.init(), so importing $app/env there gives you undefined. More subtly, instrumentation can share a chunk with application code and cause that code to evaluate early too, permanently capturing those undefined values at module scope.

Basically, instrumentation should run before the application, but not before the minimum runtime state it is allowed to observe exists.

This adds an environment initializer to builder.instrument(). Adapters provide the final env module location and generate the small platform-specific initializer; Kit handles the relative import, imports it before instrumentation, and only then dynamically imports the application entrypoint. The same ordering now applies in dev and preview.

The official adapters initialize from their native runtime source:

  • Node, Vercel serverless and Netlify serverless use process.env
  • Cloudflare uses env from cloudflare:workers
  • Netlify edge uses Deno.env.toObject()

Added tests for generated import paths, custom facades, module evaluation order, filename collisions, and a built adapter-node app reading dynamic env at instrumentation module scope.

@pkg-svelte-dev

pkg-svelte-dev Bot commented Jul 9, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 91afeee:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/91afeeed060aede8c09439926c818ec6f8d297f8

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16303

@changeset-bot

changeset-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 91afeee

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@sveltejs/kit Patch
@sveltejs/adapter-node Major
@sveltejs/adapter-cloudflare Major
@sveltejs/adapter-vercel Major
@sveltejs/adapter-netlify Major

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

@svelte-docs-bot

Copy link
Copy Markdown

@SobolievOleksii

Copy link
Copy Markdown

Tested this branch against the repro for #16288 (adapter-node + src/instrumentation.server.js): the server crashes at startup —

build/__sveltekit_env_init.js:2
import { set_env } from './env.js';
SyntaxError: The requested module './env.js' does not provide an export named 'set_env'

The facade written in builder.js hardcodes import { set_env } from './env.js' relative to the entrypoint, but for adapter-node build/env.js is the adapter's own runtime helper (export { env, env_prefix, timeout_env }) — and kit's env module with set_env doesn't end up at a stable relative path after the adapter's bundling step. Full details in #16288 (comment) — happy to re-test after an adjustment.

@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
Contributor Author

Yeah, this is still a draft, not ready yet 😅

@teemingc
teemingc force-pushed the elliott/externalize-opentelemetry-api branch from fc13f64 to eb39e12 Compare August 10, 2026 18:34
Base automatically changed from elliott/externalize-opentelemetry-api to version-3 August 10, 2026 19:24
@elliott-with-the-longest-name-on-github
elliott-with-the-longest-name-on-github force-pushed the elliott/eager-env-init branch 4 times, most recently from e17ef38 to c80e50c Compare August 10, 2026 22:24
@elliott-with-the-longest-name-on-github
elliott-with-the-longest-name-on-github changed the base branch from version-3 to elliott/o11y-docs August 10, 2026 22:24
Base automatically changed from elliott/o11y-docs to version-3 August 10, 2026 22:51
@elliott-with-the-longest-name-on-github elliott-with-the-longest-name-on-github added the needs-platform-tests This PR needs to run platform tests in order to merge. label Aug 24, 2026
Comment thread packages/kit/src/exports/public.d.ts Outdated
Comment thread packages/kit/src/exports/public.d.ts Outdated
Comment thread packages/kit/src/exports/public.d.ts
Comment thread documentation/docs/60-appendix/35-migrating-to-sveltekit-3.md Outdated
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
Comment thread packages/adapter-vercel/test/apps/basic/package.json Outdated
Comment thread documentation/docs/60-appendix/35-migrating-to-sveltekit-3.md
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
Comment thread documentation/docs/60-appendix/35-migrating-to-sveltekit-3.md
Comment thread documentation/docs/60-appendix/35-migrating-to-sveltekit-3.md
Comment thread documentation/docs/60-appendix/35-migrating-to-sveltekit-3.md Outdated
Comment thread documentation/docs/60-appendix/35-migrating-to-sveltekit-3.md Outdated
Comment thread documentation/docs/60-appendix/35-migrating-to-sveltekit-3.md
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
Comment thread documentation/docs/60-appendix/35-migrating-to-sveltekit-3.md
@teemingc
teemingc merged commit 1b79845 into version-3 Sep 1, 2026
40 checks passed
@teemingc
teemingc deleted the elliott/eager-env-init branch September 1, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-platform-tests This PR needs to run platform tests in order to merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

imports from $env are undefined when imported in src/instrumentation.server.js

3 participants