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

fix(common): Fix handling of input source maps #6561

Merged
merged 25 commits into from Dec 2, 2022
Merged

Conversation

@kdy1 kdy1 added this to the Planned milestone Dec 1, 2022
@kdy1 kdy1 self-assigned this Dec 1, 2022
@kdy1 kdy1 changed the title fix(es/transforms): Fix sourcemap fix(es): Fix input source map option Dec 1, 2022
@kdy1 kdy1 changed the title fix(es): Fix input source map option fix(common): Fix input source map option Dec 1, 2022
@kdy1 kdy1 changed the title fix(common): Fix input source map option fix(common): Fix handling of input source maps Dec 1, 2022
Copy link
Member Author

@kdy1 kdy1 left a 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

@kdy1 kdy1 requested a review from jridgewell December 1, 2022 06:51
@kdy1
Copy link
Member Author

kdy1 commented Dec 1, 2022

@jridgewell Can you take a look? This is not related to the minifier, but I need your help

@kdy1 kdy1 marked this pull request as ready for review December 1, 2022 06:53
kodiakhq[bot]
kodiakhq bot previously approved these changes Dec 1, 2022
kodiakhq[bot]
kodiakhq bot previously approved these changes Dec 1, 2022
@@ -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",
Copy link
Contributor

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:

crates/swc_common/src/source_map.rs Outdated Show resolved Hide resolved
@@ -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) {
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. Thank you!

Copy link
Member Author

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.

Copy link
Contributor

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.

kodiakhq[bot]
kodiakhq bot previously approved these changes Dec 2, 2022
@@ -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,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": "MACIC,gBAAA,GAAAD,KAAAC,gBAAA,IAAA,EAAA,EAAAC,IAAA,CAAA;;QAEC;KACD;;cAEQ,SAAAC,CAAA,EAAAC,CAAA,EAAAC,CAAA,EAAA;YACA;gBACIC,IAAI,SAAYH,CAAI,EAAA;gBACpB,IAAAC,IAAsBD,EAAAI,IAAkD;uBACpE,CAAA,GAAAC,EAAAC,GAAe,AAAL,EAAK,OAAG;oBACtBC,UAAAN,EAAAO,GAAA;gBACJ;YACA;gBAEyBP,IAAAC,EAAAO,CAAA,CAAAR,GAAS;yBAC1B,WAAqB;oBACzB,OAAAU;gBACqB;yBACjB,WAAqB;oBACzB,OAAAR;gBACJ;YACqB,EAAA;YAGjC,IAAAE,IAAAH,EAAA,OAAAS,IAAA,CAAA;QACc;cACD,SAAOX,CAAA,EAAAC,CAAA,EAAQC,CAAA,EAAG;oBACfY,QAAA,GAAAD,OAAAC,QAAA,IAAA,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",
Copy link
Member Author

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.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

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.

Visualized

@kdy1 kdy1 requested a review from jridgewell December 2, 2022 03:49
kodiakhq[bot]
kodiakhq bot previously approved these changes Dec 2, 2022
kodiakhq[bot]
kodiakhq bot previously approved these changes Dec 2, 2022
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)
kodiakhq[bot]
kodiakhq bot previously approved these changes Dec 2, 2022
jridgewell
jridgewell previously approved these changes Dec 2, 2022
Copy link
Contributor

@jridgewell jridgewell left a 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) {
Copy link
Contributor

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();
Copy link
Contributor

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.

crates/swc/tests/fixture/sourcemap/004/output/index.map Outdated Show resolved Hide resolved
@kdy1 kdy1 enabled auto-merge (squash) December 2, 2022 07:10
@kdy1 kdy1 dismissed stale reviews from jridgewell and kodiakhq via 96f07c5 December 2, 2022 07:10
@kdy1 kdy1 merged commit 4af52c7 into swc-project:main Dec 2, 2022
@kdy1 kdy1 deleted the srcmap-fast branch December 2, 2022 07:58
@kdy1 kdy1 modified the milestones: Planned, v1.3.22 Dec 9, 2022
@swc-project swc-project locked as resolved and limited conversation to collaborators Jan 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
2 participants