Skip to content

[@rollup/plugin-typescript]: Source-map source paths break if "sourceRoot" is specified #1948

Open

Description

@Haringat

Expected Behavior

Each path reference inside the source map leads to a source file inside the source root.

Actual Behavior

The paths are invalid if a source root is specified and repeat the path to the source file after the path to the directory containing the source file (see dist/index.js.map inside the stackblitz)

Additional Information

If the sourceRoot option is removed from the compilerOptions, it works as expected.

Activity

  1. shellscape commented on Mar 8, 2026

    @shellscape
    Collaborator

    @CharlieHelps using the reproduction link and the issue description, please do a best-effort triage of this issue and recommend a reasonable fix. If you have questions, please ask them first and provide 1-4 suggested answers in a b c d format. this has come up in issues in this repo in the past and there are commits around this subject, be sure to research and consider those.

  2. charliecreates commented on Mar 8, 2026

    @charliecreates
    Contributor

    Thanks for the ping — I did a best-effort triage and looked at prior related work.

    I can reproduce this on master with a local fixture mirroring the report (sourceRoot: "./src"): final map.sources entries get duplicated path segments (examples: .../src/foo/src/foo/foo.ts, .../src/src/index.ts). Removing sourceRoot produces correct paths.

    Likely root cause:

    • packages/typescript/src/outputFile.ts (findTypescriptOutput) currently passes the emitted TS .map through unchanged.
    • packages/typescript/src/index.ts returns that map directly from load().
    • With relative sourceRoot, map paths are not rebased for Rollup’s collapse base (dirname(id)), so path segments effectively get re-applied and produce invalid/duplicated sources paths.

    Related history I considered:

    Recommended fix:

    1. In packages/typescript/src/outputFile.ts, parse and rewrite emitted sourcemaps before returning them:
      • fold sourceRoot into each sources entry,
      • resolve/rebase to paths relative to dirname(id),
      • normalize to POSIX (/) with existing normalization utilities,
      • remove sourceRoot after folding to avoid downstream double-application,
      • skip rebasing for URL-like sourceRoot / sources entries.
    2. Add a regression fixture + test for this exact case (sourceRoot: "./src" with nested imports) asserting no duplicated segments in final map.sources.
    3. Keep companion regression coverage for the Incorrect sourcemap sources paths when outDir differs from source directory #1966/fix(typescript): rebase sourcemap sources paths when outDir differs from source directory #1967 outDir scenario to avoid regressions across both path-rebasing paths.

    If helpful, I can follow up with a PR implementing this approach.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions