Skip to content

webpack/rspack: transform hook sourcemaps are dropped or not composed #616

Closed

Description

@SyMind

Describe the bug

The webpack and Rspack adapters do not preserve source maps returned by a transform hook correctly:

  1. When the loader has no input source map, the adapter discards res.map.
  2. When both an input map and res.map exist, the adapter forwards only res.map instead of composing both mapping layers, so mappings from earlier loaders are lost.

As a result, the generated bundle reports incorrect original source locations even though the transform hook returns a valid source map.

Reproduction

https://github.com/SyMind/unplugin-rspack-sourcemap-repro

pnpm install
pnpm repro

The reproduction uses [email protected] and @rspack/[email protected]. Both cases run an unplugin transform that prepends one generated line and returns the corresponding high-resolution source map. The second case also runs a preceding loader that prepends a line and returns its own source map.

Actual result:

without-input-map: expected source line 1, received line 2
with-input-map: expected source line 1, received line 2

Expected behavior

The map returned by the transform hook should be preserved. The generated statement should trace back to line 1 of src/index.js in both cases, so pnpm repro should succeed.

When an input map is also present, the adapter should compose the transform map with the input map so the complete loader chain is retained.

Root cause

Both webpack-like transform loaders currently use this callback logic:

callback(null, res.code, map == null ? map : (res.map || map))

This explicitly passes a nullish input map when no input map exists, dropping res.map. When both maps exist, it selects one map without composing them.

This is related to #154 and follows up on #561 / #562: exposing inputSourceMap makes manual composition possible, but the adapter output still does not preserve the complete transform chain by default.

A fix with regression tests is available in #615.

Activity

  1. tamerkalla commented on Sep 5, 2026

    @tamerkalla

    The thing that makes this class of bug expensive is that a dropped or uncomposed map is indistinguishable from a good one at rest. It parses, it loads, the debugger opens it, and nothing anywhere says the positions stopped being true.

    It is measurable from the two artifacts alone, though, without knowing which hook dropped what. Three properties hold for the output of any correct generator: a mapping never points past the end of a line, never lands strictly inside an identifier, and when it carries a name the original source at that position starts with that name.

    I ran that over esbuild and tsc output first as a control, because a check that flags everything is worth nothing: 371 mappings and 26 names, zero violations. Then I prepended three lines to an already-mapped bundle without recomposing, which is the same shape as a transform hook returning code and no map: 88 of 93 mappings stopped landing on a token. The two cases separate cleanly, so the condition is testable rather than only reportable.

    If that is useful for pinning this as a regression test, the check is one function over the built code and the parsed map: https://www.npmjs.com/package/sourcemap-truth

  2. added
    bug
    on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugrspackwebpack

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions