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

SWC may generate invalid source map when inputSourceMap is provided #8789

Closed
noyobo opened this issue Mar 28, 2024 · 20 comments · Fixed by #8800 or #9050
Closed

SWC may generate invalid source map when inputSourceMap is provided #8789

noyobo opened this issue Mar 28, 2024 · 20 comments · Fixed by #8800 or #9050
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.

@jd-carroll
Copy link

As an FYI - All of the errors we were seeing as a result of missing sourcemaps are not appearing. The sourcemaps are still not there, but we are at least not seeing those specific errors.

Thank you @kdy1!

@kdy1
Copy link
Member

kdy1 commented Jun 13, 2024

I'm working on this, and the failure happens at

swc/crates/swc/src/lib.rs

Lines 423 to 425 in 54ac992

sourcemap::SourceMap::from_slice(s.as_bytes()).context(
"failed to read input source map from user-provided sourcemap",
)?,
.

sourcemap::SourceMap::from_slice(s.as_bytes()) fails, which means the input source map is a wrong source map file. And I think error in an explicitly passed file should not be ignored.

@kdy1 kdy1 removed their assignment Jun 13, 2024
@noyobo
Copy link
Author

noyobo commented Jun 14, 2024

I added the babel transform process for comparison. From the results, it seems that babel's transform is always normal.

https://github.com/noyobo/swc-issues/blob/676db973ad2d5085adbb9e46188ae5c8a2a2a935/transform.mjs#L58

Using swc transform babelResult, I got a new error:

[Error: data did not match any variant of untagged enum InputSourceMap at line 1 column 2320758] {
  code: 'InvalidArg'
}

Babel transforms babelResult again and it passes

https://github.com/noyobo/swc-issues/blob/676db973ad2d5085adbb9e46188ae5c8a2a2a935/transform.mjs#L73

@kdy1
Copy link
Member

kdy1 commented Jun 14, 2024

It does not mean that the input source map is valid. babel may not validate the input source map before processing. Closing as the explicit input was wrong.

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 14, 2024
@noyobo
Copy link
Author

noyobo commented Jun 14, 2024

ok, but invalid inputSourceMap is generated from first transform of swc. 😅

https://github.com/noyobo/swc-issues/blob/676db973ad2d5085adbb9e46188ae5c8a2a2a935/transform.mjs#L13~L27

I'll see if there are smaller examples later.

@kdy1 kdy1 reopened this Jun 14, 2024
@kdy1 kdy1 changed the title transform providing an inputSourceMap throws an error [bad reference to source #299] SWC may generate invalid source map Jun 14, 2024
@kdy1 kdy1 changed the title SWC may generate invalid source map SWC may generate invalid source map when inputSourceMap is provided Jun 14, 2024
@kdy1
Copy link
Member

kdy1 commented Jun 14, 2024

My guess is that sourcemap uses a hashmap internally and that causes a problem for input source maps that have duplicate contents.

[crates/swc_common/src/source_map.rs:1223:13] orig.sources().count() = 300
[crates/swc_common/src/source_map.rs:1226:17] id = 0
[crates/swc_common/src/source_map.rs:1226:17] id = 1
[crates/swc_common/src/source_map.rs:1226:17] id = 2
[crates/swc_common/src/source_map.rs:1226:17] id = 3
[crates/swc_common/src/source_map.rs:1226:17] id = 4
[crates/swc_common/src/source_map.rs:1226:17] id = 5
[crates/swc_common/src/source_map.rs:1226:17] id = 6
[crates/swc_common/src/source_map.rs:1226:17] id = 7
[crates/swc_common/src/source_map.rs:1226:17] id = 8
[crates/swc_common/src/source_map.rs:1226:17] id = 9
[crates/swc_common/src/source_map.rs:1226:17] id = 10
[crates/swc_common/src/source_map.rs:1226:17] id = 11
[crates/swc_common/src/source_map.rs:1226:17] id = 12
[crates/swc_common/src/source_map.rs:1226:17] id = 13
[crates/swc_common/src/source_map.rs:1226:17] id = 14
[crates/swc_common/src/source_map.rs:1226:17] id = 15
[crates/swc_common/src/source_map.rs:1226:17] id = 16
[crates/swc_common/src/source_map.rs:1226:17] id = 17
[crates/swc_common/src/source_map.rs:1226:17] id = 18
[crates/swc_common/src/source_map.rs:1226:17] id = 19
[crates/swc_common/src/source_map.rs:1226:17] id = 20
[crates/swc_common/src/source_map.rs:1226:17] id = 21
[crates/swc_common/src/source_map.rs:1226:17] id = 22
[crates/swc_common/src/source_map.rs:1226:17] id = 23
[crates/swc_common/src/source_map.rs:1226:17] id = 24
[crates/swc_common/src/source_map.rs:1226:17] id = 25
[crates/swc_common/src/source_map.rs:1226:17] id = 26
[crates/swc_common/src/source_map.rs:1226:17] id = 27
[crates/swc_common/src/source_map.rs:1226:17] id = 28
[crates/swc_common/src/source_map.rs:1226:17] id = 29
[crates/swc_common/src/source_map.rs:1226:17] id = 30
[crates/swc_common/src/source_map.rs:1226:17] id = 31
[crates/swc_common/src/source_map.rs:1226:17] id = 32
[crates/swc_common/src/source_map.rs:1226:17] id = 33
[crates/swc_common/src/source_map.rs:1226:17] id = 34
[crates/swc_common/src/source_map.rs:1226:17] id = 35
[crates/swc_common/src/source_map.rs:1226:17] id = 36
[crates/swc_common/src/source_map.rs:1226:17] id = 37
[crates/swc_common/src/source_map.rs:1226:17] id = 38
[crates/swc_common/src/source_map.rs:1226:17] id = 39
[crates/swc_common/src/source_map.rs:1226:17] id = 40
[crates/swc_common/src/source_map.rs:1226:17] id = 41
[crates/swc_common/src/source_map.rs:1226:17] id = 42
[crates/swc_common/src/source_map.rs:1226:17] id = 43
[crates/swc_common/src/source_map.rs:1226:17] id = 44
[crates/swc_common/src/source_map.rs:1226:17] id = 45
[crates/swc_common/src/source_map.rs:1226:17] id = 46
[crates/swc_common/src/source_map.rs:1226:17] id = 47
[crates/swc_common/src/source_map.rs:1226:17] id = 48
[crates/swc_common/src/source_map.rs:1226:17] id = 49
[crates/swc_common/src/source_map.rs:1226:17] id = 50
[crates/swc_common/src/source_map.rs:1226:17] id = 51
[crates/swc_common/src/source_map.rs:1226:17] id = 52
[crates/swc_common/src/source_map.rs:1226:17] id = 53
[crates/swc_common/src/source_map.rs:1226:17] id = 54
[crates/swc_common/src/source_map.rs:1226:17] id = 55
[crates/swc_common/src/source_map.rs:1226:17] id = 56
[crates/swc_common/src/source_map.rs:1226:17] id = 57
[crates/swc_common/src/source_map.rs:1226:17] id = 58
[crates/swc_common/src/source_map.rs:1226:17] id = 59
[crates/swc_common/src/source_map.rs:1226:17] id = 60
[crates/swc_common/src/source_map.rs:1226:17] id = 61
[crates/swc_common/src/source_map.rs:1226:17] id = 62
[crates/swc_common/src/source_map.rs:1226:17] id = 63
[crates/swc_common/src/source_map.rs:1226:17] id = 64
[crates/swc_common/src/source_map.rs:1226:17] id = 65
[crates/swc_common/src/source_map.rs:1226:17] id = 66
[crates/swc_common/src/source_map.rs:1226:17] id = 67
[crates/swc_common/src/source_map.rs:1226:17] id = 68
[crates/swc_common/src/source_map.rs:1226:17] id = 69
[crates/swc_common/src/source_map.rs:1226:17] id = 70
[crates/swc_common/src/source_map.rs:1226:17] id = 71
[crates/swc_common/src/source_map.rs:1226:17] id = 72
[crates/swc_common/src/source_map.rs:1226:17] id = 73
[crates/swc_common/src/source_map.rs:1226:17] id = 74
[crates/swc_common/src/source_map.rs:1226:17] id = 75
[crates/swc_common/src/source_map.rs:1226:17] id = 76
[crates/swc_common/src/source_map.rs:1226:17] id = 77
[crates/swc_common/src/source_map.rs:1226:17] id = 78
[crates/swc_common/src/source_map.rs:1226:17] id = 79
[crates/swc_common/src/source_map.rs:1226:17] id = 80
[crates/swc_common/src/source_map.rs:1226:17] id = 81
[crates/swc_common/src/source_map.rs:1226:17] id = 82
[crates/swc_common/src/source_map.rs:1226:17] id = 83
[crates/swc_common/src/source_map.rs:1226:17] id = 84
[crates/swc_common/src/source_map.rs:1226:17] id = 85
[crates/swc_common/src/source_map.rs:1226:17] id = 86
[crates/swc_common/src/source_map.rs:1226:17] id = 87
[crates/swc_common/src/source_map.rs:1226:17] id = 88
[crates/swc_common/src/source_map.rs:1226:17] id = 89
[crates/swc_common/src/source_map.rs:1226:17] id = 90
[crates/swc_common/src/source_map.rs:1226:17] id = 91
[crates/swc_common/src/source_map.rs:1226:17] id = 92
[crates/swc_common/src/source_map.rs:1226:17] id = 93
[crates/swc_common/src/source_map.rs:1226:17] id = 94
[crates/swc_common/src/source_map.rs:1226:17] id = 95
[crates/swc_common/src/source_map.rs:1226:17] id = 96
[crates/swc_common/src/source_map.rs:1226:17] id = 97
[crates/swc_common/src/source_map.rs:1226:17] id = 98
[crates/swc_common/src/source_map.rs:1226:17] id = 99
[crates/swc_common/src/source_map.rs:1226:17] id = 100
[crates/swc_common/src/source_map.rs:1226:17] id = 101
[crates/swc_common/src/source_map.rs:1226:17] id = 102
[crates/swc_common/src/source_map.rs:1226:17] id = 103
[crates/swc_common/src/source_map.rs:1226:17] id = 104
[crates/swc_common/src/source_map.rs:1226:17] id = 105
[crates/swc_common/src/source_map.rs:1226:17] id = 106
[crates/swc_common/src/source_map.rs:1226:17] id = 107
[crates/swc_common/src/source_map.rs:1226:17] id = 108
[crates/swc_common/src/source_map.rs:1226:17] id = 109
[crates/swc_common/src/source_map.rs:1226:17] id = 110
[crates/swc_common/src/source_map.rs:1226:17] id = 111
[crates/swc_common/src/source_map.rs:1226:17] id = 112
[crates/swc_common/src/source_map.rs:1226:17] id = 113
[crates/swc_common/src/source_map.rs:1226:17] id = 114
[crates/swc_common/src/source_map.rs:1226:17] id = 115
[crates/swc_common/src/source_map.rs:1226:17] id = 116
[crates/swc_common/src/source_map.rs:1226:17] id = 117
[crates/swc_common/src/source_map.rs:1226:17] id = 118
[crates/swc_common/src/source_map.rs:1226:17] id = 119
[crates/swc_common/src/source_map.rs:1226:17] id = 120
[crates/swc_common/src/source_map.rs:1226:17] id = 121
[crates/swc_common/src/source_map.rs:1226:17] id = 122
[crates/swc_common/src/source_map.rs:1226:17] id = 123
[crates/swc_common/src/source_map.rs:1226:17] id = 124
[crates/swc_common/src/source_map.rs:1226:17] id = 125
[crates/swc_common/src/source_map.rs:1226:17] id = 126
[crates/swc_common/src/source_map.rs:1226:17] id = 127
[crates/swc_common/src/source_map.rs:1226:17] id = 128
[crates/swc_common/src/source_map.rs:1226:17] id = 129
[crates/swc_common/src/source_map.rs:1226:17] id = 130
[crates/swc_common/src/source_map.rs:1226:17] id = 131
[crates/swc_common/src/source_map.rs:1226:17] id = 132
[crates/swc_common/src/source_map.rs:1226:17] id = 133
[crates/swc_common/src/source_map.rs:1226:17] id = 134
[crates/swc_common/src/source_map.rs:1226:17] id = 135
[crates/swc_common/src/source_map.rs:1226:17] id = 136
[crates/swc_common/src/source_map.rs:1226:17] id = 137
[crates/swc_common/src/source_map.rs:1226:17] id = 138
[crates/swc_common/src/source_map.rs:1226:17] id = 139
[crates/swc_common/src/source_map.rs:1226:17] id = 140
[crates/swc_common/src/source_map.rs:1226:17] id = 141
[crates/swc_common/src/source_map.rs:1226:17] id = 142
[crates/swc_common/src/source_map.rs:1226:17] id = 143
[crates/swc_common/src/source_map.rs:1226:17] id = 144
[crates/swc_common/src/source_map.rs:1226:17] id = 145
[crates/swc_common/src/source_map.rs:1226:17] id = 146
[crates/swc_common/src/source_map.rs:1226:17] id = 147
[crates/swc_common/src/source_map.rs:1226:17] id = 148
[crates/swc_common/src/source_map.rs:1226:17] id = 149
[crates/swc_common/src/source_map.rs:1226:17] id = 150
[crates/swc_common/src/source_map.rs:1226:17] id = 151
[crates/swc_common/src/source_map.rs:1226:17] id = 152
[crates/swc_common/src/source_map.rs:1226:17] id = 153
[crates/swc_common/src/source_map.rs:1226:17] id = 154
[crates/swc_common/src/source_map.rs:1226:17] id = 155
[crates/swc_common/src/source_map.rs:1226:17] id = 156
[crates/swc_common/src/source_map.rs:1226:17] id = 157
[crates/swc_common/src/source_map.rs:1226:17] id = 158
[crates/swc_common/src/source_map.rs:1226:17] id = 159
[crates/swc_common/src/source_map.rs:1226:17] id = 160
[crates/swc_common/src/source_map.rs:1226:17] id = 161
[crates/swc_common/src/source_map.rs:1226:17] id = 162
[crates/swc_common/src/source_map.rs:1226:17] id = 163
[crates/swc_common/src/source_map.rs:1226:17] id = 164
[crates/swc_common/src/source_map.rs:1226:17] id = 165
[crates/swc_common/src/source_map.rs:1226:17] id = 166
[crates/swc_common/src/source_map.rs:1226:17] id = 167
[crates/swc_common/src/source_map.rs:1226:17] id = 168
[crates/swc_common/src/source_map.rs:1226:17] id = 169
[crates/swc_common/src/source_map.rs:1226:17] id = 170
[crates/swc_common/src/source_map.rs:1226:17] id = 171
[crates/swc_common/src/source_map.rs:1226:17] id = 172
[crates/swc_common/src/source_map.rs:1226:17] id = 173
[crates/swc_common/src/source_map.rs:1226:17] id = 174
[crates/swc_common/src/source_map.rs:1226:17] id = 175
[crates/swc_common/src/source_map.rs:1226:17] id = 176
[crates/swc_common/src/source_map.rs:1226:17] id = 177
[crates/swc_common/src/source_map.rs:1226:17] id = 178
[crates/swc_common/src/source_map.rs:1226:17] id = 179
[crates/swc_common/src/source_map.rs:1226:17] id = 180
[crates/swc_common/src/source_map.rs:1226:17] id = 181
[crates/swc_common/src/source_map.rs:1226:17] id = 182
[crates/swc_common/src/source_map.rs:1226:17] id = 183
[crates/swc_common/src/source_map.rs:1226:17] id = 184
[crates/swc_common/src/source_map.rs:1226:17] id = 185
[crates/swc_common/src/source_map.rs:1226:17] id = 186
[crates/swc_common/src/source_map.rs:1226:17] id = 187
[crates/swc_common/src/source_map.rs:1226:17] id = 188
[crates/swc_common/src/source_map.rs:1226:17] id = 189
[crates/swc_common/src/source_map.rs:1226:17] id = 190
[crates/swc_common/src/source_map.rs:1226:17] id = 191
[crates/swc_common/src/source_map.rs:1226:17] id = 192
[crates/swc_common/src/source_map.rs:1226:17] id = 193
[crates/swc_common/src/source_map.rs:1226:17] id = 194
[crates/swc_common/src/source_map.rs:1226:17] id = 195
[crates/swc_common/src/source_map.rs:1226:17] id = 196
[crates/swc_common/src/source_map.rs:1226:17] id = 197
[crates/swc_common/src/source_map.rs:1226:17] id = 198
[crates/swc_common/src/source_map.rs:1226:17] id = 199
[crates/swc_common/src/source_map.rs:1226:17] id = 200
[crates/swc_common/src/source_map.rs:1226:17] id = 201
[crates/swc_common/src/source_map.rs:1226:17] id = 192
[crates/swc_common/src/source_map.rs:1226:17] id = 202
[crates/swc_common/src/source_map.rs:1226:17] id = 203
[crates/swc_common/src/source_map.rs:1226:17] id = 204
[crates/swc_common/src/source_map.rs:1226:17] id = 205
[crates/swc_common/src/source_map.rs:1226:17] id = 206
[crates/swc_common/src/source_map.rs:1226:17] id = 207
[crates/swc_common/src/source_map.rs:1226:17] id = 208
[crates/swc_common/src/source_map.rs:1226:17] id = 209
[crates/swc_common/src/source_map.rs:1226:17] id = 210
[crates/swc_common/src/source_map.rs:1226:17] id = 211
[crates/swc_common/src/source_map.rs:1226:17] id = 212
[crates/swc_common/src/source_map.rs:1226:17] id = 213
[crates/swc_common/src/source_map.rs:1226:17] id = 214
[crates/swc_common/src/source_map.rs:1226:17] id = 215
[crates/swc_common/src/source_map.rs:1226:17] id = 216
[crates/swc_common/src/source_map.rs:1226:17] id = 217
[crates/swc_common/src/source_map.rs:1226:17] id = 218
[crates/swc_common/src/source_map.rs:1226:17] id = 219
[crates/swc_common/src/source_map.rs:1226:17] id = 220
[crates/swc_common/src/source_map.rs:1226:17] id = 221
[crates/swc_common/src/source_map.rs:1226:17] id = 222
[crates/swc_common/src/source_map.rs:1226:17] id = 223
[crates/swc_common/src/source_map.rs:1226:17] id = 224
[crates/swc_common/src/source_map.rs:1226:17] id = 225
[crates/swc_common/src/source_map.rs:1226:17] id = 226
[crates/swc_common/src/source_map.rs:1226:17] id = 227
[crates/swc_common/src/source_map.rs:1226:17] id = 228
[crates/swc_common/src/source_map.rs:1226:17] id = 229
[crates/swc_common/src/source_map.rs:1226:17] id = 230
[crates/swc_common/src/source_map.rs:1226:17] id = 231
[crates/swc_common/src/source_map.rs:1226:17] id = 232
[crates/swc_common/src/source_map.rs:1226:17] id = 233
[crates/swc_common/src/source_map.rs:1226:17] id = 234
[crates/swc_common/src/source_map.rs:1226:17] id = 235
[crates/swc_common/src/source_map.rs:1226:17] id = 236
[crates/swc_common/src/source_map.rs:1226:17] id = 237
[crates/swc_common/src/source_map.rs:1226:17] id = 238
[crates/swc_common/src/source_map.rs:1226:17] id = 239
[crates/swc_common/src/source_map.rs:1226:17] id = 240
[crates/swc_common/src/source_map.rs:1226:17] id = 241
[crates/swc_common/src/source_map.rs:1226:17] id = 242
[crates/swc_common/src/source_map.rs:1226:17] id = 243
[crates/swc_common/src/source_map.rs:1226:17] id = 244
[crates/swc_common/src/source_map.rs:1226:17] id = 245
[crates/swc_common/src/source_map.rs:1226:17] id = 246
[crates/swc_common/src/source_map.rs:1226:17] id = 247
[crates/swc_common/src/source_map.rs:1226:17] id = 248
[crates/swc_common/src/source_map.rs:1226:17] id = 249
[crates/swc_common/src/source_map.rs:1226:17] id = 250
[crates/swc_common/src/source_map.rs:1226:17] id = 251
[crates/swc_common/src/source_map.rs:1226:17] id = 252
[crates/swc_common/src/source_map.rs:1226:17] id = 253
[crates/swc_common/src/source_map.rs:1226:17] id = 254
[crates/swc_common/src/source_map.rs:1226:17] id = 255
[crates/swc_common/src/source_map.rs:1226:17] id = 256
[crates/swc_common/src/source_map.rs:1226:17] id = 257
[crates/swc_common/src/source_map.rs:1226:17] id = 258
[crates/swc_common/src/source_map.rs:1226:17] id = 259
[crates/swc_common/src/source_map.rs:1226:17] id = 260
[crates/swc_common/src/source_map.rs:1226:17] id = 261
[crates/swc_common/src/source_map.rs:1226:17] id = 262
[crates/swc_common/src/source_map.rs:1226:17] id = 263
[crates/swc_common/src/source_map.rs:1226:17] id = 264
[crates/swc_common/src/source_map.rs:1226:17] id = 265
[crates/swc_common/src/source_map.rs:1226:17] id = 266
[crates/swc_common/src/source_map.rs:1226:17] id = 267
[crates/swc_common/src/source_map.rs:1226:17] id = 268
[crates/swc_common/src/source_map.rs:1226:17] id = 269
[crates/swc_common/src/source_map.rs:1226:17] id = 270
[crates/swc_common/src/source_map.rs:1226:17] id = 271
[crates/swc_common/src/source_map.rs:1226:17] id = 272
[crates/swc_common/src/source_map.rs:1226:17] id = 273
[crates/swc_common/src/source_map.rs:1226:17] id = 274
[crates/swc_common/src/source_map.rs:1226:17] id = 275
[crates/swc_common/src/source_map.rs:1226:17] id = 276
[crates/swc_common/src/source_map.rs:1226:17] id = 277
[crates/swc_common/src/source_map.rs:1226:17] id = 278
[crates/swc_common/src/source_map.rs:1226:17] id = 279
[crates/swc_common/src/source_map.rs:1226:17] id = 280
[crates/swc_common/src/source_map.rs:1226:17] id = 281
[crates/swc_common/src/source_map.rs:1226:17] id = 282
[crates/swc_common/src/source_map.rs:1226:17] id = 283
[crates/swc_common/src/source_map.rs:1226:17] id = 284
[crates/swc_common/src/source_map.rs:1226:17] id = 285
[crates/swc_common/src/source_map.rs:1226:17] id = 286
[crates/swc_common/src/source_map.rs:1226:17] id = 287
[crates/swc_common/src/source_map.rs:1226:17] id = 288
[crates/swc_common/src/source_map.rs:1226:17] id = 289
[crates/swc_common/src/source_map.rs:1226:17] id = 290
[crates/swc_common/src/source_map.rs:1226:17] id = 291
[crates/swc_common/src/source_map.rs:1226:17] id = 292
[crates/swc_common/src/source_map.rs:1226:17] id = 293
[crates/swc_common/src/source_map.rs:1226:17] id = 294
[crates/swc_common/src/source_map.rs:1226:17] id = 295
[crates/swc_common/src/source_map.rs:1226:17] id = 296
[crates/swc_common/src/source_map.rs:1226:17] id = 297
[crates/swc_common/src/source_map.rs:1226:17] id = 298

@kdy1
Copy link
Member

kdy1 commented Jun 14, 2024

[crates/swc_common/src/source_map.rs:1226:17] id = 201
[crates/swc_common/src/source_map.rs:1226:17] id = 192

@kdy1
Copy link
Member

kdy1 commented Jun 14, 2024

                if id == 192 {
                    dbg!(src);
                }

[crates/swc_common/src/source_map.rs:1231:21] src = "/Users/noyobo/home/gitlab/tuya-public/illuminate-base-mp/dist/../dist/src/components.ts"

@kdy1
Copy link
Member

kdy1 commented Jun 14, 2024

image

Yeap it was the cause

@kdy1
Copy link
Member

kdy1 commented Jun 14, 2024

I left getsentry/rust-sourcemap#89

@potench
Copy link

potench commented Aug 1, 2024

Something in v1.4.12 (and still present in v7.22.8) of @swc-project/swc is causing the following error when targeting es5 + sourcemap: true

Caused by:
    relative URL without a base
    at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc-0.282.3/src/lib.rs:407

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

In my case, I'm using tsup; my options currently are:

  1. Downgrade to @swc/core/1.v.4.11
  2. Disable sourcemap: false in tsup.config.ts
  3. Or change to target: es6 in tsup.config.ts

After looking at this a bit more, a minimal reproduction looks like this

npm init
npm install @swc/[email protected] [email protected] [email protected] --save-dev
# create a file to build
echo "export default () => console.log('hello world');" > index.ts
# remove dist and tsup it 
rm -rf ./dist/ && npx tsup --sourcemap --treeshake --target es5 ./index.ts 
# results in error 
# > ERROR  failed to read input source map: failed to parse inline source map url 

It looks like there's a race condition. If you run again and ./dist already has the output, then it will not fail

@kdy1
Copy link
Member

kdy1 commented Aug 2, 2024

getsentry/rust-sourcemap#91 is still not merged/published so it's expected

@kdy1 kdy1 closed this as completed in #9050 Aug 5, 2024
@kdy1 kdy1 closed this as completed in 9d65c77 Aug 5, 2024
@kdy1 kdy1 modified the milestones: Planned, 1.7.7 Aug 5, 2024
@noyobo
Copy link
Author

noyobo commented Aug 7, 2024

Wait?, I tested version 1.7.7-nightly-20240806.4, and it just didn't report an error. But the generated sourcemap seems to be unavailable

  1. Use swc transform a.js & a.js.map
console.log('==== use swc transform es6 to es5 ====');
// transformed successfully
const result = await transform(fileCode, {
  filename: 'a.js',
  sourceMaps: true,
  inputSourceMap: fileMap,
  jsc: { target: 'es5' },
  minify: true,
  module: { type: 'commonjs', strictMode: false },
});

fs.writeFileSync('./a.swc.js', result.code);
fs.writeFileSync('./a.swc.js.map', result.map);
  1. Validate transformed sourcemap
const fs = require('fs');
const path = require('path');
const { SourceMapConsumer } = require('source-map'); // 0.7.4

async function verifySourcemap({ file, map, offset, line }) {
  const sourceMap = fs.readFileSync(map, 'utf8');
  const consumer = await new SourceMapConsumer(sourceMap);
  const originalPosition = consumer.originalPositionFor({
    line: line,
    column: offset,
  });
  if (originalPosition.source === null) {
    // @swc/core@nightly always return null for source
    return null;
  }
  let miniCode = fs.readFileSync(file, 'utf8');
  miniCode = miniCode.split('\n')[line - 1];
  miniCode = miniCode.substring(offset - 20, offset + 20);

  const source = consumer.sourceContentFor(originalPosition.source);
  const code = source.split('\n')[originalPosition.line - 1];

  // 1.7.6  that works
  return { sourceFile: originalPosition.source, code, miniCode };
}

verifySourcemap({
  file: path.join(__dirname, 'a.swc.js'),
  map: path.join(__dirname, 'a.swc.js.map'),
  line: 1,
  offset: 207274,
}).then((result) => {
  console.log('swc:', result);
});

Is this a new problem?

@kdy1
Copy link
Member

kdy1 commented Aug 7, 2024

Yes, please file another issue

@swc-bot
Copy link
Collaborator

swc-bot commented Sep 6, 2024

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Sep 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
6 participants