-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix(common): Fix handling of input source maps #6561
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
swc-bump:
- swc_common
@jridgewell Can you take a look? This is not related to the minifier, but I need your help |
@@ -1,5 +1,5 @@ | |||
{ | |||
"mappings": "AEACA,CAAAA,KAAK,gBAAmB,GAAGA,KAAK,gBAAmB,IAAI,EAAE,AAAD,EAAGC,IAAI,CAAC;IAC7D;QAAC;KAAI;IACL;QACU,MAAY,SACdC,CAAuB,EACvBC,CAAmB,EACnBC,CAAmB,EACrB;YACE;gBFNXC,IAAA,SAAAC,CAAA,EAAA;gBAAA,IAAAC,IAAAD,EAAAC,IAAA;gBAAA,OAAA,CAAA,GCDDC,EAAAC,GAAA,EAAA,OAAA;oBACAC,UAAAH,EAAAI,GAAA;gBACA;YACA;YAKYP,EAAoBQ,CAAC,CAACT,IACDC,EAAoBS,CAAC,CAACV,GAAqB;gBACvCW,SAAS,WAAY;oBACtC,OAAqBA;gBDZjB;gBAAqBC,SAAA,WAAA;oBAC3C,OAAOV;gBAAM;YACd;YAAA,IAAAG,IAAAJ,EAAA,OAAAU,IAAA,CAAA;QCKM;QAKP,MAAA,SAAAZ,CAAA,EAAAc,CAAA,EAAAZ,CAAA,EAAA;YAAAa,CAAAA,OAAAC,QAAA,GAAAD,OAAAC,QAAA,IAAA,EAAA,EAAAjB,IAAA,CAAA;gBAAA;gBAAA,WAAA;oBAAA,OAAAG,EAAA;gBAAA;aAAA;QAAA;IAAA;IAAA,SAAAA,CAAA,EAAA;QAAAA,EAAAe,CAAA,CAAA,GAAA;YAAA;YAAA;YAAA;SAAA,EAAA,WAAA;YAAA,OAAAf,EAAAA,EAAAgB,CAAA,GAAA;QAAA,IAAAC,OAAAjB,EAAAe,CAAA;IAAA;CAAA", | |||
"mappings": ";;;KACS", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This worries me, but it seems to be the input sourcemap is really bad:
@@ -1281,12 +1281,17 @@ impl SourceMap { | |||
let mut col = max(chpos, linechpos) - min(chpos, linechpos); | |||
|
|||
if let Some(orig) = &orig { | |||
if let Some(token) = orig.lookup_token(line, col) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's actually (what I consider to be) a bug in lookup_token
, where it'll return an a mapping span on a previous line for spans that:
- extend beyond the last mapped line
- lines that don't have a mapping
- unmapped columns on lines with a mapping
I tried to get them to fix it in getsentry/rust-sourcemap#53, but they rejected it. This breaks with Mozilla's source map behavior, because lines endings are supposed to split the mapping from any previous one.
Anyways, the "fix" is to do:
orig.lookup_token(line, col).filter(|t| t.get_dst_line() == line)
Is this the bug you're trying to fix with the find
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the reason, but it didn't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh, we probably have an off-by-one. The visualized link you posted is definitely doing the lookup on the wrong line. The sourcemap crate uses 0-based lines, based on the readme example.
@@ -1,38 +1,33 @@ | |||
{ | |||
"mappings": "AEACA,CAAAA,KAAK,gBAAmB,GAAGA,KAAK,gBAAmB,IAAI,EAAE,AAAD,EAAGC,IAAI,CAAC;IAC7D;QAAC;KAAI;IACL;QACU,MAAY,SACdC,CAAuB,EACvBC,CAAmB,EACnBC,CAAmB,EACrB;YACE;gBFNXC,IAAA,SAAAC,CAAA,EAAA;gBAAA,IAAAC,IAAAD,EAAAC,IAAA;gBAAA,OAAA,CAAA,GCDDC,EAAAC,G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AA,EAAA,EAAAf,IAAA,CAAA;gBACA;2BACW;oBACX,OAAAG,EAAA;gBACH;aAGb;QACI;IACS;aAK0BF,CAAA,EAAG;YAC9B,GAAA;YACA;YACA;YACD;sBAN4B;YAUtB,OAD0BA,EAAoBA,EAACgB,CAAA,GAAA;QAGhE,IAAAC,OAAAjB,EAAAe,CAAA,EAAA;IACC", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix this with another PR
(Seems like a source map issue related to codegen of multiline comments.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lookup_token
with filter
was the cause.
This reverts commit 87115e7.
Fixes a few things: - Propagates the input's sourcesContent item - Propagates the input's original name, if found (else, uses our name) - Uses much faster lookup_token (instead of a full traversal, that would be sooo slow)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated visualization
@@ -1281,12 +1281,17 @@ impl SourceMap { | |||
let mut col = max(chpos, linechpos) - min(chpos, linechpos); | |||
|
|||
if let Some(orig) = &orig { | |||
if let Some(token) = orig.lookup_token(line, col) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh, we probably have an off-by-one. The visualized link you posted is definitely doing the lookup on the wrong line. The sourcemap crate uses 0-based lines, based on the readme example.
if let Some(token) = orig | ||
.tokens() | ||
.find(|token| token.get_src_line() == line - 1 && token.get_src_col() == col) | ||
{ | ||
line = token.get_src_line() + 1; | ||
col = token.get_src_col(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I notice we're not pulling the name
from the found token, which means we'll always the name found in SWC's input. But, the name could have already been changed by whatever preceeded SWC.
Description:
Related issue: