From d2a2b58457a2293621e837a0f97f912f399d1879 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Sat, 23 Sep 2023 02:21:16 +0800 Subject: [PATCH 1/6] use vite.ssrFixStacktrace instead of rewriteStacktrace --- packages/kit/src/exports/vite/dev/index.js | 13 +++++++------ packages/kit/src/runtime/server/utils.js | 10 +--------- packages/kit/src/runtime/shared-server.js | 6 +++--- packages/kit/src/types/internal.d.ts | 2 +- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/packages/kit/src/exports/vite/dev/index.js b/packages/kit/src/exports/vite/dev/index.js index d8e2c7b40102..562342cd7e88 100644 --- a/packages/kit/src/exports/vite/dev/index.js +++ b/packages/kit/src/exports/vite/dev/index.js @@ -54,7 +54,7 @@ export async function dev(vite, vite_config, svelte_config) { /** @param {string} url */ async function loud_ssr_load_module(url) { try { - return await vite.ssrLoadModule(url); + return await vite.ssrLoadModule(url, { fixStacktrace: true }); } catch (/** @type {any} */ err) { const msg = buildErrorMessage(err, [colors.red(`Internal server error: ${err.message}`)]); @@ -248,9 +248,10 @@ export async function dev(vite, vite_config, svelte_config) { }; } - /** @param {string} stack */ - function fix_stack_trace(stack) { - return stack ? vite.ssrRewriteStacktrace(stack) : stack; + /** @param {Error} error */ + function fix_stack_trace(error) { + vite.ssrFixStacktrace(error); + return error.stack; } await update_manifest(); @@ -393,7 +394,7 @@ export async function dev(vite, vite_config, svelte_config) { } catch (e) { const error = coalesce_to_error(e); res.statusCode = 500; - res.end(fix_stack_trace(/** @type {string} */ (error.stack))); + res.end(fix_stack_trace(error)); } }); @@ -523,7 +524,7 @@ export async function dev(vite, vite_config, svelte_config) { } catch (e) { const error = coalesce_to_error(e); res.statusCode = 500; - res.end(fix_stack_trace(/** @type {string} */ (error.stack))); + res.end(fix_stack_trace(error)); } }); }; diff --git a/packages/kit/src/runtime/server/utils.js b/packages/kit/src/runtime/server/utils.js index ec5831b7e4de..6e5a4ee1b6ad 100644 --- a/packages/kit/src/runtime/server/utils.js +++ b/packages/kit/src/runtime/server/utils.js @@ -99,15 +99,7 @@ export async function handle_error_and_jsonify(event, options, error) { return error.body; } else { if (__SVELTEKIT_DEV__ && typeof error == 'object') { - error = new Proxy(error, { - get: (target, property) => { - if (property === 'stack') { - return fix_stack_trace(target.stack); - } - - return Reflect.get(target, property, target); - } - }); + fix_stack_trace(error); } return ( diff --git a/packages/kit/src/runtime/shared-server.js b/packages/kit/src/runtime/shared-server.js index ed1c5efcb472..0ac0420d1d0b 100644 --- a/packages/kit/src/runtime/shared-server.js +++ b/packages/kit/src/runtime/shared-server.js @@ -4,8 +4,8 @@ export let private_env = {}; /** @type {Record} */ export let public_env = {}; -/** @param {string} stack */ -export let fix_stack_trace = (stack) => stack; +/** @param {any} _error */ +export let fix_stack_trace = (_error) => ''; /** @type {(environment: Record) => void} */ export function set_private_env(environment) { @@ -17,7 +17,7 @@ export function set_public_env(environment) { public_env = environment; } -/** @param {(stack: string) => string} value */ +/** @param {(error: Error) => string} value */ export function set_fix_stack_trace(value) { fix_stack_trace = value; } diff --git a/packages/kit/src/types/internal.d.ts b/packages/kit/src/types/internal.d.ts index 5dba7f1aa8d0..84ffcd4bc4ef 100644 --- a/packages/kit/src/types/internal.d.ts +++ b/packages/kit/src/types/internal.d.ts @@ -32,7 +32,7 @@ export interface ServerInternalModule { set_private_env(environment: Record): void; set_public_env(environment: Record): void; set_version(version: string): void; - set_fix_stack_trace(fix_stack_trace: (stack: string) => string): void; + set_fix_stack_trace(fix_stack_trace: (error: unknown) => string): void; } export interface Asset { From 6621f431934ddf510b9adfef8c16efe89a5ce889 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Sat, 23 Sep 2023 02:23:33 +0800 Subject: [PATCH 2/6] changeset --- .changeset/clean-panthers-remain.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/clean-panthers-remain.md diff --git a/.changeset/clean-panthers-remain.md b/.changeset/clean-panthers-remain.md new file mode 100644 index 000000000000..80ce832da8ef --- /dev/null +++ b/.changeset/clean-panthers-remain.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: correct line numbers in stack trace From a7aa9eb6feb0c8d9b6f05993326ce86613f26287 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Sat, 23 Sep 2023 03:31:05 +0800 Subject: [PATCH 3/6] oops missed one --- packages/kit/src/exports/vite/dev/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/exports/vite/dev/index.js b/packages/kit/src/exports/vite/dev/index.js index 562342cd7e88..267f7c04c51b 100644 --- a/packages/kit/src/exports/vite/dev/index.js +++ b/packages/kit/src/exports/vite/dev/index.js @@ -233,7 +233,7 @@ export async function dev(vite, vite_config, svelte_config) { for (const key in manifest_data.matchers) { const file = manifest_data.matchers[key]; const url = path.resolve(cwd, file); - const module = await vite.ssrLoadModule(url); + const module = await vite.ssrLoadModule(url, { fixStacktrace: true }); if (module.match) { matchers[key] = module.match; From 429d7d474f1e2e798bb01536ffd1c45fb38137a6 Mon Sep 17 00:00:00 2001 From: Nayan Gautam Date: Sun, 24 Sep 2023 17:49:47 +0545 Subject: [PATCH 4/6] fix stack trace when loading server file because it later loads the hooks file where the user may have errors --- packages/kit/src/exports/vite/dev/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/exports/vite/dev/index.js b/packages/kit/src/exports/vite/dev/index.js index 267f7c04c51b..3e6267f8e41a 100644 --- a/packages/kit/src/exports/vite/dev/index.js +++ b/packages/kit/src/exports/vite/dev/index.js @@ -455,7 +455,7 @@ export async function dev(vite, vite_config, svelte_config) { // we have to import `Server` before calling `set_assets` const { Server } = /** @type {import('types').ServerModule} */ ( - await vite.ssrLoadModule(`${runtime_base}/server/index.js`) + await vite.ssrLoadModule(`${runtime_base}/server/index.js`, { fixStacktrace: true }) ); const { set_fix_stack_trace } = await vite.ssrLoadModule( From 5bf306fc989b48400a613f7915a7c20785a845b4 Mon Sep 17 00:00:00 2001 From: Nayan Gautam Date: Sun, 24 Sep 2023 17:50:52 +0545 Subject: [PATCH 5/6] dedupe error logging in loud_ssr_load_module vite stopped doing it automatically some point I guess --- packages/kit/src/exports/vite/dev/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/kit/src/exports/vite/dev/index.js b/packages/kit/src/exports/vite/dev/index.js index 3e6267f8e41a..a70be05c9cc8 100644 --- a/packages/kit/src/exports/vite/dev/index.js +++ b/packages/kit/src/exports/vite/dev/index.js @@ -58,7 +58,10 @@ export async function dev(vite, vite_config, svelte_config) { } catch (/** @type {any} */ err) { const msg = buildErrorMessage(err, [colors.red(`Internal server error: ${err.message}`)]); - vite.config.logger.error(msg, { error: err }); + if (!vite.config.logger.hasErrorLogged(err)) { + vite.config.logger.error(msg, { error: err }); + } + vite.ws.send({ type: 'error', err: { From b29758b0cd2b0be42c20474c6555745d6dc3709b Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Sun, 24 Sep 2023 23:04:28 +0800 Subject: [PATCH 6/6] Update shared-server.js --- packages/kit/src/runtime/shared-server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/runtime/shared-server.js b/packages/kit/src/runtime/shared-server.js index 0ac0420d1d0b..778e31aad481 100644 --- a/packages/kit/src/runtime/shared-server.js +++ b/packages/kit/src/runtime/shared-server.js @@ -4,8 +4,8 @@ export let private_env = {}; /** @type {Record} */ export let public_env = {}; -/** @param {any} _error */ -export let fix_stack_trace = (_error) => ''; +/** @param {any} error */ +export let fix_stack_trace = (error) => error?.stack; /** @type {(environment: Record) => void} */ export function set_private_env(environment) {