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 generate invalid source map when inputSourceMap is provided #9392

Closed
noyobo opened this issue Aug 7, 2024 · 8 comments · Fixed by #9447
Closed

SWC generate invalid source map when inputSourceMap is provided #9392

noyobo opened this issue Aug 7, 2024 · 8 comments · Fixed by #9447
Assignees
Labels
Milestone

Comments

@noyobo
Copy link

noyobo commented Aug 7, 2024

Describe the bug

I tested version 1.7.8-nightly-20240807.1, not report any error. But the generated sourcemap 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];

  // lgt 1.7.6 versions 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);
});

Input code

No response

Config

No response

Playground link (or link to the minimal reproduction)

https://github.com/noyobo/swc-issues/tree/transform-compare

yarn install
node  ./swc/build.mjs

SWC Info output

No response

Expected behavior

When inputSourceMap is provided, the sourcemap after transfrom is parsable

Actual behavior

No response

Version

1.7.8-nightly-20240807.1

Additional context

No response

@noyobo noyobo added the C-bug label Aug 7, 2024
@kdy1 kdy1 added this to the Planned milestone Aug 8, 2024
@kdy1 kdy1 self-assigned this Aug 8, 2024
@jiby-aurum
Copy link

Have the same issue, upgrading swc_core from 0.99.6 to any never version breaks processing of source provided with an existing source map.

@kdy1
Copy link
Member

kdy1 commented Aug 19, 2024

I reverted #9052 with #9437 because sourcemap::SourceMap::adjust_mappings has a bug. I'll investigate further. IIRC, it was about duplicate entry in the inputSourceMap.

@kdy1
Copy link
Member

kdy1 commented Aug 19, 2024

@noyobo
Copy link
Author

noyobo commented Aug 20, 2024

👍 Solved, delete the repo

@kdy1
Copy link
Member

kdy1 commented Aug 20, 2024

Can you update repro?

@noyobo
Copy link
Author

noyobo commented Aug 20, 2024

Can you update repro?

Sorry I made a mistake, Transform it again and it was correct.

@jiby-aurum
Copy link

@kdy1 works perfectly with swc core 0.101.4, thanks for the super quick fix.

@swc-bot
Copy link
Collaborator

swc-bot commented Sep 21, 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 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

4 participants