Skip to content

fix: ensure version is defined when using $app/env with explicit env vars#15971

Merged
teemingc merged 2 commits into
sveltejs:mainfrom
Nic-Polumeyv:fix-app-env-version-define
Jun 7, 2026
Merged

fix: ensure version is defined when using $app/env with explicit env vars#15971
teemingc merged 2 commits into
sveltejs:mainfrom
Nic-Polumeyv:fix-app-env-version-define

Conversation

@Nic-Polumeyv
Copy link
Copy Markdown
Contributor

@Nic-Polumeyv Nic-Polumeyv commented Jun 7, 2026

When experimental.explicitEnvironmentVariables is enabled, $app/environment throws and apps import from $app/env instead. $app/env re-exports version from internal.js (export const version = __SVELTEKIT_APP_VERSION__), but unlike $app/environment it never references import.meta.hot, so Vite doesn't force its client to load. If $app/env is imported before the Vite client has loaded, version evaluates while the define is missing and the browser throws:

ReferenceError: __SVELTEKIT_APP_VERSION__ is not defined

This happens when $app/env is imported early, e.g. from hooks.client.js. Importing it from a route component is too late to hit it, since the client is already up by then, which is why it's intermittent. I ran into it in a real app after enabling experimental.explicitEnvironmentVariables.

It came in with #15934, which moved version onto the __SVELTEKIT_APP_VERSION__ define and added import.meta.hot to $app/environment, but not to the newly split $app/env.

The fix mirrors $app/environment. I left that file's import.meta.hot in place to keep this minimal, though it's now redundant with this one.

Added a regression test in options-2: an early $app/env import in hooks.client.js plus a load of the page. It fails on main (the app never finishes hydrating) and passes with this change.


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 test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jun 7, 2026

🦋 Changeset detected

Latest commit: 27717f0

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

@Nic-Polumeyv
Copy link
Copy Markdown
Contributor Author

The two failing jobs look unrelated to this change. test-kit (20) crashed at the Node level (Assertion failed: (req_wrap_async) != nullptr, exit 129) and test-kit (22) timed out on a remote-functions e2e test, while 18, 24 and all cross-browser runs pass. This PR only adds import.meta.hot to $app/env, so I don't think it's related, but happy to rebase or re-run if that helps.

@Nic-Polumeyv
Copy link
Copy Markdown
Contributor Author

Added a regression test in options-2 and pinned the exact trigger: the error fires when $app/env is imported before the Vite client loads (e.g. from hooks.client.js); a route-component import is too late to hit it. Verified locally that the test fails on main and passes with this change. (Updating an earlier note of mine that said a deterministic test wasn't feasible, it is.)

@Nic-Polumeyv Nic-Polumeyv force-pushed the fix-app-env-version-define branch from 064bf30 to 1ed409d Compare June 7, 2026 03:31
@teemingc
Copy link
Copy Markdown
Member

teemingc commented Jun 7, 2026

I wonder if a fix such as https://github.com/sveltejs/kit/pull/15574/changes#diff-a7244013c14e3925b24a2faf96dc33f01fae957ae2fa1a89a96fba3ee90be886R646 would be more fool proof. This isn’t the first time we’ve run into this issue and it could happen again depending on the order or presence of imported modules.

Copy link
Copy Markdown
Member

@teemingc teemingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@teemingc teemingc merged commit 17fadaa into sveltejs:main Jun 7, 2026
25 of 26 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 7, 2026
@Nic-Polumeyv Nic-Polumeyv deleted the fix-app-env-version-define branch June 7, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants