getsentry / getsentry/sentry-javascript

@sentry/sveltekit: Incorrect source mapping when adapter-node merges files containing multiple injected debug IDs

Offen
#20,084 2 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Bug SvelteKit
Vorherrschende Sprache
TypeScript
Sterne
8.7k
Forks
1.8k
Ø Merge
1 T. 17 Std.
Gemergte PRs (30 T.)
515

Beschreibung

### Is there an existing issue for this?

- [x] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- [x] I have reviewed the documentation https://docs.sentry.io/
- [x] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

### How do you use Sentry?

Self-hosted/on-premise

### Which SDK are you using?

@sentry/sveltekit

### SDK Version

10.47.0

### Framework Version

SvelteKit 2.50.2

### Link to Sentry event

_No response_

### Reproduction Example/SDK Setup

https://github.com/dsp28/sveltekit-sentry

### Steps to Reproduce

1. Create a minimal SvelteKit project with `@sveltejs/adapter-node` and `@sentry/sveltekit`:
```sh
npx sv@0.13.1 create --template minimal --types ts --add sveltekit-adapter="adapter:node" --install npm .
npm install @sentry/sveltekit@10.47.0 --save
```
2. Set up Sentry following the [SvelteKit manual setup guide](https://docs.sentry.io/platforms/javascript/guides/sveltekit/manual-setup/), including `sentrySvelteKit()` in vite.config.ts with `build.sourcemap = true`.
3. Create a route with both a universal `load` function in `+page.ts` and `actions` in `+page.server.ts`. Throw an error from the action:
```ts
// src/routes/+page.ts
import { redirect } from '@sveltejs/kit';
import { resolve } from '$app/paths';
import type { PageLoad } from './$types';

export const load: PageLoad = async () => {
if (Math.random() > 0.99) return redirect(303, resolve('/won'));
};
```
```ts
// src/routes/+page.server.ts
import type { Actions } from './$types';

export const actions: Actions = {
default: async ({ request }) => {
if (await request.formData().then(d => d.get('action') === 'throw'))
throw new Error('Sentry Test Error');
return { success: true };
},
};
```
4. Run `npm run build` and inspect the output in chunks.

More details: see reproduction repo.

### Expected Result

The error thrown in `+page.server.ts` should be correctly source-mapped in Sentry, pointing to the original +page.server.ts file and the correct line number.

### Actual Result

The error is captured but **incorrectly source-mapped** in Sentry.

**Root cause:** (as I understand it) During `vite build`, the `sentrySvelteKit` plugin injects a unique debug ID snippet into each intermediate file in output (which maps 1:1 to source files). The `@sveltejs/adapter-node` build stage then merges some of these files into single output chunks — for example, `+page.ts` and `+page.server.ts` are bundled together.

The resulting chunk contains **two** debug ID injection snippets — one from each original source file:

```js
// First debug ID (from +page.ts)
!(function() {
try {
// ...
n && (e._sentryDebugIds = e._sentryDebugIds || {},
e._sentryDebugIds[n] = "982f403b-6069-4cb5-a799-2b9ffef9767f",
e._sentryDebugIdIdentifier = "sentry-dbid-982f403b-6069-4cb5-a799-2b9ffef9767f");
} catch (e2) {}
})();

// ... +page.ts code ...

// Second debug ID (from +page.server.ts)
!(function() {
try {
// ...
n && (e._sentryDebugIds = e._sentryDebugIds || {},
e._sentryDebugIds[n] = "983f3adb-d513-4148-9398-a76b21644b32",
e._sentryDebugIdIdentifier = "sentry-dbid-983f3adb-d513-4148-9398-a76b21644b32");
} catch (e2) {}
})();

// ... +page.server.ts code (contains the thrown error) ...
```

The sourcemap emitted by the adapter correctly references both original source files, but Sentry's sourcemap upload only registers the **first** debug ID in the file. Consequently, any error originating from code positioned after the second debug ID (i.e. `+page.server.ts`) cannot be correctly source-mapped.

### Additional Context

_No response_

### Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding `+1` or `me too`, to help us triage it.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.