Skip to content

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

@dsp28

Description

@dsp28

Is there an existing issue for this?

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:
    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, 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:
    // 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'));
    };
    // 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:

// 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.

Metadata

Metadata

Assignees

No one assigned
    No fields configured for issues without a type.

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions