Skip to content

Commit 34f1ec5

Browse files
authored
fix: strip virtual module prefix from error messages (#10776)
1 parent b4c76c8 commit 34f1ec5

File tree

7 files changed

+49
-34
lines changed

7 files changed

+49
-34
lines changed

.changeset/gorgeous-shrimps-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: strip virtual module prefix from error messages

packages/kit/src/exports/vite/graph_analysis/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import path from 'node:path';
22
import { posixify } from '../../../utils/filesystem.js';
3+
import { strip_virtual_prefix } from '../utils.js';
34

4-
const ILLEGAL_IMPORTS = new Set(['\0$env/dynamic/private', '\0$env/static/private']);
5+
const ILLEGAL_IMPORTS = new Set([
6+
'\0virtual:$env/dynamic/private',
7+
'\0virtual:$env/static/private'
8+
]);
59
const ILLEGAL_MODULE_NAME_PATTERN = /.*\.server\..+/;
610

711
/**
@@ -51,10 +55,14 @@ export function module_guard(context, { cwd, lib }) {
5155
chain.map(({ id, dynamic }, i) => {
5256
id = normalize_id(id, lib, cwd);
5357

54-
return `${' '.repeat(i * 2)}- ${id} ${dynamic ? 'dynamically imports' : 'imports'}\n`;
55-
}) + `${' '.repeat(chain.length)}- ${id}`;
58+
return `${' '.repeat(i * 2)}- ${strip_virtual_prefix(id)} ${
59+
dynamic ? 'dynamically imports' : 'imports'
60+
}\n`;
61+
}) + `${' '.repeat(chain.length)}- ${strip_virtual_prefix(id)}`;
5662

57-
const message = `Cannot import ${id} into client-side code:\n${pyramid}`;
63+
const message = `Cannot import ${strip_virtual_prefix(
64+
id
65+
)} into client-side code:\n${pyramid}`;
5866

5967
throw new Error(message);
6068
}

packages/kit/src/exports/vite/graph_analysis/index.spec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ test('throws an error when importing $env/static/private', () => {
4444
importedIds: ['~/src/routes/+page.svelte']
4545
},
4646
'~/src/routes/+page.svelte': {
47-
importedIds: ['\0$env/static/private']
47+
importedIds: ['\0virtual:$env/static/private']
4848
}
4949
},
50-
`Cannot import \0$env/static/private into client-side code:
50+
`Cannot import $env/static/private into client-side code:
5151
- src/routes/+page.svelte imports
52-
- \0$env/static/private`
52+
- $env/static/private`
5353
);
5454
});
5555

@@ -60,12 +60,12 @@ test('throws an error when dynamically importing $env/static/private', () => {
6060
importedIds: ['~/src/routes/+page.svelte']
6161
},
6262
'~/src/routes/+page.svelte': {
63-
dynamicallyImportedIds: ['\0$env/static/private']
63+
dynamicallyImportedIds: ['\0virtual:$env/static/private']
6464
}
6565
},
66-
`Cannot import \0$env/static/private into client-side code:
66+
`Cannot import $env/static/private into client-side code:
6767
- src/routes/+page.svelte dynamically imports
68-
- \0$env/static/private`
68+
- $env/static/private`
6969
);
7070
});
7171

@@ -76,12 +76,12 @@ test('throws an error when importing $env/dynamic/private', () => {
7676
importedIds: ['~/src/routes/+page.svelte']
7777
},
7878
'~/src/routes/+page.svelte': {
79-
importedIds: ['\0$env/dynamic/private']
79+
importedIds: ['\0virtual:$env/dynamic/private']
8080
}
8181
},
82-
`Cannot import \0$env/dynamic/private into client-side code:
82+
`Cannot import $env/dynamic/private into client-side code:
8383
- src/routes/+page.svelte imports
84-
- \0$env/dynamic/private`
84+
- $env/dynamic/private`
8585
);
8686
});
8787

@@ -92,12 +92,12 @@ test('throws an error when dynamically importing $env/dynamic/private', () => {
9292
importedIds: ['~/src/routes/+page.svelte']
9393
},
9494
'~/src/routes/+page.svelte': {
95-
dynamicallyImportedIds: ['\0$env/dynamic/private']
95+
dynamicallyImportedIds: ['\0virtual:$env/dynamic/private']
9696
}
9797
},
98-
`Cannot import \0$env/dynamic/private into client-side code:
98+
`Cannot import $env/dynamic/private into client-side code:
9999
- src/routes/+page.svelte dynamically imports
100-
- \0$env/dynamic/private`
100+
- $env/dynamic/private`
101101
);
102102
});
103103

packages/kit/src/exports/vite/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { assets_base, find_deps } from './build/utils.js';
1818
import { dev } from './dev/index.js';
1919
import { is_illegal, module_guard, normalize_id } from './graph_analysis/index.js';
2020
import { preview } from './preview/index.js';
21-
import { get_config_aliases, get_env } from './utils.js';
21+
import { get_config_aliases, get_env, strip_virtual_prefix } from './utils.js';
2222
import { write_client_manifest } from '../../core/sync/write_client_manifest.js';
2323
import prerender from '../../core/postbuild/prerender.js';
2424
import analyse from '../../core/postbuild/analyse.js';
@@ -336,7 +336,7 @@ function kit({ svelte_config }) {
336336
async resolveId(id) {
337337
// treat $env/static/[public|private] as virtual
338338
if (id.startsWith('$env/') || id.startsWith('__sveltekit/') || id === '$service-worker') {
339-
return `\0${id}`;
339+
return `\0virtual:${id}`;
340340
}
341341
},
342342

@@ -358,24 +358,24 @@ function kit({ svelte_config }) {
358358
})
359359
) {
360360
const relative = normalize_id(id, normalized_lib, normalized_cwd);
361-
throw new Error(`Cannot import ${relative} into client-side code`);
361+
throw new Error(`Cannot import ${strip_virtual_prefix(relative)} into client-side code`);
362362
}
363363
}
364364

365365
switch (id) {
366-
case '\0$env/static/private':
366+
case '\0virtual:$env/static/private':
367367
return create_static_module('$env/static/private', env.private);
368368

369-
case '\0$env/static/public':
369+
case '\0virtual:$env/static/public':
370370
return create_static_module('$env/static/public', env.public);
371371

372-
case '\0$env/dynamic/private':
372+
case '\0virtual:$env/dynamic/private':
373373
return create_dynamic_module(
374374
'private',
375375
vite_config_env.command === 'serve' ? env.private : undefined
376376
);
377377

378-
case '\0$env/dynamic/public':
378+
case '\0virtual:$env/dynamic/public':
379379
// populate `$env/dynamic/public` from `window`
380380
if (browser) {
381381
return `export const env = ${global}.env;`;
@@ -386,12 +386,12 @@ function kit({ svelte_config }) {
386386
vite_config_env.command === 'serve' ? env.public : undefined
387387
);
388388

389-
case '\0$service-worker':
389+
case '\0virtual:$service-worker':
390390
return create_service_worker_module(svelte_config);
391391

392392
// for internal use only. it's published as $app/paths externally
393393
// we use this alias so that we won't collide with user aliases
394-
case '\0__sveltekit/paths': {
394+
case '\0virtual:__sveltekit/paths': {
395395
const { assets, base } = svelte_config.kit.paths;
396396

397397
// use the values defined in `global`, but fall back to hard-coded values
@@ -429,7 +429,7 @@ function kit({ svelte_config }) {
429429
`;
430430
}
431431

432-
case '\0__sveltekit/environment': {
432+
case '\0virtual:__sveltekit/environment': {
433433
const { version } = svelte_config.kit;
434434

435435
return dedent`

packages/kit/src/exports/vite/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,5 @@ export function not_found(req, res, base) {
9797
);
9898
}
9999
}
100+
101+
export const strip_virtual_prefix = /** @param {string} id */ (id) => id.replace('\0virtual:', '');

packages/kit/test/apps/dev-only/test/test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test.describe.serial('Illegal imports', () => {
1111
wait_for_started: false
1212
});
1313
expect(await page.textContent('.message-body')).toBe(
14-
'Cannot import \0$env/dynamic/private into client-side code'
14+
'Cannot import $env/dynamic/private into client-side code'
1515
);
1616
});
1717

@@ -20,7 +20,7 @@ test.describe.serial('Illegal imports', () => {
2020
wait_for_started: false
2121
});
2222
expect(await page.textContent('.message-body')).toBe(
23-
'Cannot import \0$env/dynamic/private into client-side code'
23+
'Cannot import $env/dynamic/private into client-side code'
2424
);
2525
});
2626

@@ -29,7 +29,7 @@ test.describe.serial('Illegal imports', () => {
2929
wait_for_started: false
3030
});
3131
expect(await page.textContent('.message-body')).toBe(
32-
'Cannot import \0$env/static/private into client-side code'
32+
'Cannot import $env/static/private into client-side code'
3333
);
3434
});
3535

@@ -38,7 +38,7 @@ test.describe.serial('Illegal imports', () => {
3838
wait_for_started: false
3939
});
4040
expect(await page.textContent('.message-body')).toBe(
41-
'Cannot import \0$env/static/private into client-side code'
41+
'Cannot import $env/static/private into client-side code'
4242
);
4343
});
4444

packages/kit/test/build-errors/env.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test('$env/dynamic/private is not statically importable from the client', () =>
1010
stdio: 'pipe',
1111
timeout: 60000
1212
}),
13-
/.*Cannot import \0\$env\/dynamic\/private into client-side code:.*/gs
13+
/.*Cannot import \$env\/dynamic\/private into client-side code:.*/gs
1414
);
1515
});
1616

@@ -22,7 +22,7 @@ test('$env/dynamic/private is not dynamically importable from the client', () =>
2222
stdio: 'pipe',
2323
timeout: 60000
2424
}),
25-
/.*Cannot import \0\$env\/dynamic\/private into client-side code:.*/gs
25+
/.*Cannot import \$env\/dynamic\/private into client-side code:.*/gs
2626
);
2727
});
2828

@@ -34,7 +34,7 @@ test('$env/static/private is not statically importable from the client', () => {
3434
stdio: 'pipe',
3535
timeout: 60000
3636
}),
37-
/.*Cannot import \0\$env\/static\/private into client-side code:.*/gs
37+
/.*Cannot import \$env\/static\/private into client-side code:.*/gs
3838
);
3939
});
4040

@@ -46,6 +46,6 @@ test('$env/static/private is not dynamically importable from the client', () =>
4646
stdio: 'pipe',
4747
timeout: 60000
4848
}),
49-
/.*Cannot import \0\$env\/static\/private into client-side code:.*/gs
49+
/.*Cannot import \$env\/static\/private into client-side code:.*/gs
5050
);
5151
});

0 commit comments

Comments
 (0)