chore: use subpath imports for more #app/* modules - #16795
Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/82bb5794a9ecf30738eb72e8949a9ec7bffc15c9Open in |
|
…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>
$app/env modules#app/* modules
|
there's more to do here — next up, |
| template | ||
| }) => ` | ||
| import { set_building, set_prerendering } from '$app/env/internal'; | ||
| import { set_building, set_prerendering } from '$app/env/server'; |
There was a problem hiding this comment.
did you mean
| import { set_building, set_prerendering } from '$app/env/server'; | |
| import { set_building, set_prerendering } from '#app/env/server'; |
?
There was a problem hiding this comment.
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
|
/autofix |
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>
By using subpath imports, we can get rid of grotesqueries like this:
This gets more interesting when we do the same for
$app/navigationandclient.js, but I'm starting small. No changeset because this is invisible to the userPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
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