Skip to content

chore: use subpath imports for more #app/* modules - #16795

Merged
Rich-Harris merged 12 commits into
version-3from
tidy-up-env
Aug 14, 2026
Merged

chore: use subpath imports for more #app/* modules#16795
Rich-Harris merged 12 commits into
version-3from
tidy-up-env

Conversation

@Rich-Harris

Copy link
Copy Markdown
Member

By using subpath imports, we can get rid of grotesqueries like this:

export const version = BROWSER ? payload.version : __SVELTEKIT_APP_VERSION__;

This gets more interesting when we do the same for $app/navigation and client.js, but I'm starting small. No changeset because this is invisible to the user


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.

@pkg-svelte-dev

pkg-svelte-dev Bot commented Aug 14, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 82bb579:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/82bb5794a9ecf30738eb72e8949a9ec7bffc15c9

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

@changeset-bot

changeset-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 82bb579

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@svelte-docs-bot

Copy link
Copy Markdown

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Additional Suggestion:

app_env_private in module_ids.js points to the renamed-away path runtime/app/env/private.js, breaking server-only import enforcement for $app/env/private.

Fix on Vercel

Rich-Harris and others added 4 commits August 13, 2026 20:26
…path `runtime/app/env/private.js`, breaking server-only import enforcement for `$app/env/private`.

This commit fixes the issue reported at packages/kit/src/exports/vite/module_ids.js:19

## Bug

This PR renamed `packages/kit/src/runtime/app/env/private.js` → `packages/kit/src/runtime/app/env/private/index.js` (verified: only `private/index.js` exists now).

But `packages/kit/src/exports/vite/module_ids.js` still computed:

```js
export const app_env_private = posixify(
  fileURLToPath(new URL('../../runtime/app/env/private.js', import.meta.url))
);
```

which resolves to a non-existent path `.../runtime/app/env/private.js`.

`$app/env/private` is resolved at build time via the `$app` alias (`replacement:` ), producing the real module id `.../runtime/app/env/private/index.js`. Since `app_env_private` no longer equals that id:

*   In `src/exports/vite/index.js` (~line 830), `exactRegex(app_env_private)` in the `load.filter.id` no longer matches the actual module id, so the server-only enforcement handler is never invoked for `$app/env/private`.
*   In `src/exports/vite/utils.js` (~line 122), `normalize_id` no longer maps the id back to `$app/env/private`, so the `is_server_only === '$app/env/private'` check fails.

**Failure mode / trigger:** importing `$app/env/private` from client-side code. Previously this produced an illegal-import error blocking private env vars from reaching client bundles; with the stale path, that enforcement silently no longer fires, risking private environment variables leaking into client output.

Note that the sibling constant `app_server` already correctly points to `server/index.js`, confirming the intended pattern.

## Fix

Updated the path to `../../runtime/app/env/private/index.js`:

```js
export const app_env_private = posixify(
  fileURLToPath(new URL('../../runtime/app/env/private/index.js', import.meta.url))
);
```

The `utils.spec.js` expectation (`normalize_id(app_env_private, [], cwd)` → `'$app/env/private'`) still holds, since it compares against the same constant.


Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <hello@rich-harris.dev>
@Rich-Harris Rich-Harris changed the title chore: tidy up $app/env modules chore: use subpath imports for more #app/* modules Aug 14, 2026
@Rich-Harris

Copy link
Copy Markdown
Member Author

there's more to do here — next up, $app/state — but the PR is already getting quite large so I'll stop there

template
}) => `
import { set_building, set_prerendering } from '$app/env/internal';
import { set_building, set_prerendering } from '$app/env/server';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

did you mean

Suggested change
import { set_building, set_prerendering } from '$app/env/server';
import { set_building, set_prerendering } from '#app/env/server';

?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No. This file is written inside the .svelte-kit directory, where SvelteKit's subpath imports don't apply. We could either use an absolute path here (which would arguably be neater) or continue using the alias — for now I chose the alias

Comment thread packages/kit/types/index.d.ts
@teemingc

Copy link
Copy Markdown
Member

/autofix

@Rich-Harris
Rich-Harris merged commit 5acccb1 into version-3 Aug 14, 2026
27 checks passed
@Rich-Harris
Rich-Harris deleted the tidy-up-env branch August 14, 2026 13:17
Rich-Harris added a commit that referenced this pull request Aug 14, 2026
Follow-up to #16795 — this applies the same thinking to `$app/state`. It
allows us to get rid of the awful `BROWSER` hack and colocate logic more
sensibly, with less indirection.

Again, no changeset, since no user-observable changes.

---

### Please don't delete this checklist! Before submitting the PR, please
make sure you do the following:

- [x] 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
- [x] This message body should clearly illustrate what problems it
solves.
- [ ] Ideally, include a test that fails without this PR but passes with
it.

### Tests

- [x] 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

- [x] Please ensure that 'Allow edits from maintainers' is checked. PRs
without this option may be closed.

---------

Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
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.

4 participants