Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform providing an inputSourceMap throws an error [bad reference to source #299] #8789

Open
noyobo opened this issue Mar 28, 2024 · 5 comments · Fixed by #8800
Open

transform providing an inputSourceMap throws an error [bad reference to source #299] #8789

noyobo opened this issue Mar 28, 2024 · 5 comments · Fixed by #8800
Assignees
Labels
Milestone

Comments

@noyobo
Copy link

noyobo commented Mar 28, 2024

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

@noyobo noyobo added the C-bug label Mar 28, 2024
@noyobo noyobo changed the title tansform providing an inputSourceMap throws an error [bad reference to source #299] transform providing an inputSourceMap throws an error [bad reference to source #299] Mar 28, 2024
@kdy1 kdy1 added this to the Planned milestone Mar 29, 2024
@kdy1 kdy1 self-assigned this Apr 2, 2024
@kdy1 kdy1 closed this as completed in #8800 Apr 3, 2024
kdy1 added a commit that referenced this issue Apr 3, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.4.12 Apr 4, 2024
@noyobo
Copy link
Author

noyobo commented Apr 9, 2024

@kdy1 Is it fixed in v1.4.12? I tried to use 1.4.12 and the problem still exists.

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

> npx -y swc-info@latest && node ./transform-a.mjs


    Operating System:
        Platform: linux
        Arch: x64
        Machine Type: undefined
        Version: Ubuntu 20.04.0 LTS Tue Apr 09 2024 10:36:06 GMT+0800 (中国标准时间)
        CPU: (3 cores)
            Models: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz

    Binaries:
        Node: 18.18.0
        npm: 10.2.3
        Yarn: 1.22.19
        pnpm: 8.15.3

    Relevant Packages:
        @swc/core: 1.4.12
        @swc/helpers: N/A
        @swc/types: N/A
        

    SWC Config:
        output: N/A
        .swcrc path: N/A

    Next.js info:
        output: N/A

failed to process js file

Caused by:
    0: failed to read input source map from user-provided sourcemap
    1: bad reference to source #299

@kdy1 kdy1 reopened this Apr 9, 2024
@kdy1 kdy1 modified the milestones: v1.4.12, Planned Apr 9, 2024
@kdy1 kdy1 removed their assignment Apr 9, 2024
@kdy1 kdy1 assigned kdy1 and unassigned kdy1 Apr 29, 2024
@davidmurdoch
Copy link
Contributor

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
Copy link
Contributor

My hunch is that this issue is caused by npm packages that include references to source maps (via //# sourceMappingURL=<some-file.js.map> comments) but omit the referenced source map files from the published package.

One example is @trezor/protobuf. They've got a semi-related issue: trezor/trezor-suite#9334

@kdy1
Copy link
Member

kdy1 commented May 13, 2024

@davidmurdoch #8951 should fix your issue, although I'm not sure if it fixes this issue

kdy1 added a commit that referenced this issue May 13, 2024
**Description:**

Some libraries generate source maps but do not upload them to npm, which causes SWC to fail.

**Related issue:**

- #8789 (comment)
@noyobo
Copy link
Author

noyobo commented May 15, 2024

1.5.6 same

Sorry, I can't provide a smaller reproducible example yet, I don't know what boundary conditions are triggered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

3 participants