swc: SWC may generate invalid source map when inputSourceMap is provided

Describe the bug

https://stackblitz.com/~/github.com/noyobo/swc-issues

$ npm run build 

[Error: failed to read input source map from user-provided sourcemap

Caused by:
    bad reference to source #299] {
  code: 'GenericFailure'
}

Input code

No response

Config

import {transform} from '@swc/core';
import fs from "node:fs";

const fileCode = fs.readFileSync("./a.js", "utf-8");
const fileMap = fs.readFileSync("./a.js.map", "utf-8");
// valid check https://evanw.github.io/source-map-visualization/


// transformed successfully
const result = await transform(fileCode, {
  filename: 'a.js',
  sourceMaps: true,
  inputSourceMap: fileMap,
  jsc: {target: 'es5'},
  module: {type: "commonjs", strictMode: false},
})

// fs.writeFileSync("./a.transformed.js", result.code);
// fs.writeFileSync("./a.transformed.js.map", result.map);

// Using the result again to transform throws an error
// and verification failed in https://evanw.github.io/source-map-visualization/
await transform(result.code, {
  filename: "a.js",
  sourceMaps: true,
  inputSourceMap: result.map,
  module: {type: "commonjs", strictMode: false},
}).catch((e) => {
  /**
   * [Error: failed to read input source map from user-provided sourcemap
   *
   * Caused by:
   *     bad reference to source #299] {
   *   code: 'GenericFailure'
   * }
   */
  console.log(e);
});

Playground link (or link to the minimal reproduction)

https://stackblitz.com/~/github.com/noyobo/swc-issues

SWC Info output

No response

Expected behavior

The transformed sourceMap content can be used again

Actual behavior

No response

Version

1.4.11

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Seeing the same when updating from 1.4.11 to 1.5.5.

Caused by:
    relative URL without a base
    at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/swc-0.275.0/src/lib.rs:386

  ERROR  failed to read input source map: failed to parse inline source map url
index.js.map

@davidmurdoch https://github.com/swc-project/swc/pull/8951 should fix your issue, although I’m not sure if it fixes this issue