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

Nested arrays cause off-by-one error in generated sourcemaps #8066

Open
marcins opened this issue Oct 5, 2023 · 1 comment · May be fixed by #8069
Open

Nested arrays cause off-by-one error in generated sourcemaps #8066

marcins opened this issue Oct 5, 2023 · 1 comment · May be fixed by #8069
Assignees
Labels
Milestone

Comments

@marcins
Copy link

marcins commented Oct 5, 2023

Describe the bug

When SWC transforms a nested array, the resulting sourcemap has an off-by-one error where the mapping for the end of one of the arrays points to an invalid location in the source.

I validated this by taking the input code below and running it through this code:

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

async function main() {
    const result = await transform(fs.readFileSync('./src/index.js', 'utf8'), {
        sourceMaps: true,
    });
    fs.writeFileSync('./dist/index.js', result.code, 'utf8');
    fs.writeFileSync('./dist/index.js.map', result.map + "\n//# sourceMappingURL=index.js.map",'utf8');
}

main().catch(e => {
    process.exitCode = 1;
    console.error(e);
})

.. and then trying to load the resulting output with source-map-explorer which fails with:

  Your source map refers to generated column 7 on line 2, but the source only contains 6 column(s) on that line.
  Check that you are using the correct source map.

See also the attached Source Map Visualiser link in the repro.

Input code

var a = [[]];

Config

n/a

Playground link (or link to the minimal reproduction)

https://evanw.github.io/source-map-visualization/#NTMAdmFyIGEgPSBbCiAgICBbXQpdOwovLyMgc291cmNlTWFwcGluZ1VSTD1pbmRleC5qcy5tYXAxMjcAeyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxhbm9uPiJdLCJzb3VyY2VzQ29udGVudCI6WyJ2YXIgYSA9IFtbXV07Il0sIm5hbWVzIjpbImEiXSwibWFwcGluZ3MiOiJBQUFBLElBQUlBLElBQUk7SUFBQyxFQUFFO0NBQUMifQ==

SWC Info output

Operating System:
    Platform: darwin
    Arch: arm64
    Machine Type: arm64
    Version: Darwin Kernel Version 22.6.0: Fri Sep 15 13:41:28 PDT 2023; root:xnu-8796.141.3.700.8~1/RELEASE_ARM64_T6000
    CPU: (10 cores)
        Models: Apple M1 Pro

Binaries:
    Node: 18.18.0
    npm: 9.8.1
    Yarn: 1.22.19
    pnpm: N/A

Relevant Packages:
    @swc/core: 1.3.91
    @swc/helpers: 0.5.2
    @swc/types: 0.1.5


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

Next.js info:
    output: N/A

Expected behavior

The sourcemap will be valid.

Actual behavior

The sourcemap is invalid, the second closing paren is mapped past the end of the line:

image

Version

1.3.91

Additional context

https://evanw.github.io/source-map-visualization/#NTMAdmFyIGEgPSBbCiAgICBbXQpdOwovLyMgc291cmNlTWFwcGluZ1VSTD1pbmRleC5qcy5tYXAxMjcAeyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxhbm9uPiJdLCJzb3VyY2VzQ29udGVudCI6WyJ2YXIgYSA9IFtbXV07Il0sIm5hbWVzIjpbImEiXSwibWFwcGluZ3MiOiJBQUFBLElBQUlBLElBQUk7SUFBQyxFQUFFO0NBQUMifQ==

@marcins marcins added the C-bug label Oct 5, 2023
@github-actions github-actions bot closed this as completed Oct 5, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Oct 5, 2023
@swc-project swc-project unlocked this conversation Oct 5, 2023
@swc-project swc-project deleted a comment from github-actions bot Oct 5, 2023
@kdy1 kdy1 reopened this Oct 5, 2023
@kdy1
Copy link
Member

kdy1 commented Oct 5, 2023

Oops. It seems like we need to add some more domains to allowlist.

@kdy1 kdy1 removed the invalid link label Oct 5, 2023
@kdy1 kdy1 self-assigned this Oct 5, 2023
@kdy1 kdy1 added this to the Planned milestone Oct 5, 2023
@kdy1 kdy1 linked a pull request Oct 5, 2023 that will close this issue
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.

2 participants