next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Next + SWC appear to generate incorrect sourcemaps

Open markerikson opened this issue 2 years ago • 9 comments

Verify canary release

  • [X] I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 16.14.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 12.2.6-canary.2
      eslint-config-next: N/A
      react: 0.0.0-experimental-e7d0053e6-20220325
      react-dom: 0.0.0-experimental-e7d0053e6-20220325

What browser are you using? (if relevant)

N/A

How are you deploying your application? (if relevant)

Vercel, but N/A

Describe the Bug

Next generates different sourcemaps when we switch between using Babel to transpile our code, and SWC. The SWC sourcemaps are incorrect - the mapped output differs from the original source code by a few lines, causing incorrect debugging behavior.

We build https://replay.io , a time-travel debugger for JS. The Replay client uses sourcemaps on both the backend and frontend to parse and display info about the recorded application. In turn, we frequently record replays of our own client app to debug how it's working.

I've made two replays of our codebase built with Next 12.2.6-canary.2 , switching only between using Babel and SWC to transpile:

If you open up debugger/src/actions/event-listeners.ts in both files, you can see that the Babel file correctly maps all of the lines that were hit:

image

In the SWC-built version, the line hit info is off by 3-4 lines.

image

If you open up the sourcemap visualizer tool for both versions, the Babel version is correct:

  • https://app.replay.io/recording/replay-next-1226-canary2-babel--e55cb64a-aea9-400b-98d3-f3b5ad3525b7/sourcemap/o999

image

and the SWC version is wrong:

  • https://app.replay.io/recording/replay-next-1226-canary2-swc--8272ce14-288f-4076-93fb-45f22c562c8f/sourcemap/o995

image

Expected Behavior

Using SWC to compile our application would generate sourcemaps that accurately map the original source.

Link to reproduction

https://github.com/replayio/devtools/tree/bugfix/FE-434-next-sourcemaps

To Reproduce

  1. Clone https://github.com/replayio/devtools
  2. Check out commit https://github.com/replayio/devtools/commit/61605c25401897a3486545111e5f9157ef5e3886 , part of the bugfix/FE-434-next-sourcemaps branch.
  3. Run yarn to install
  4. Use Babel to build the app: yarn build, and check the build output + sourcemaps
  5. Check out the tip of the bugfix/FE-434-next-sourcemaps branch, which just disables .babelrc to use SWC for building
  6. Use SWC to build the app: yarnbuild, and check the build output + sourcemaps

markerikson avatar Aug 23 '22 20:08 markerikson

Can you try the latest version? I think this is fixed.

kdy1 avatar Oct 08 '22 03:10 kdy1

By "the latest version", do you mean 12.3.1, or the latest canary release? (I'm also seeing this issue with sourcemaps being sent to Sentry being incorrect; literally just updated to 12.3.1 this afternoon so will monitor, but if I need to update to canary that would be good to know.)

fiveable-jferg avatar Oct 19 '22 03:10 fiveable-jferg

I meant the canary

kdy1 avatar Oct 19 '22 04:10 kdy1

Whoops, somehow I missed seeing the earlier notice. Yeah, will try tomorrow!

markerikson avatar Oct 19 '22 04:10 markerikson

This is still definitely broken.

Steps to reproduce:

  • Clone https://github.com/replayio/devtools
  • Check out branch bugfix/FE-434-next-swc-sourcemaps-busted
  • yarn to install deps
  • yarn build
  • Open up https://evanw.github.io/source-map-visualization/ in a tab
  • In a file explorer, browse down to $REPLAY_REPO/.next/static/chunk/pages/recording
  • Drag [[...id]-$HASH.js and [[...id]-$HASH.js.map into the sourcemap viewer
  • In the top dropdown, look around list entry 128-125 for a file named actions/box-model.ts and select it
  • Observe that the sourcemapped original text and minified output do not align, such as the effect: async (action, listenerApi) => line:

image

For comparison, this works correctly with Babel as the compiler:

image

I'm attaching a zip file containing the output [[...id]].js and [[...id]].js.map files for 4 different build combinations:

  • 12.2, Babel compiler + Terser
  • 12.2, Babel compiler + swcminify
  • 12.3.canary-32, Babel compiler (+ swcminify by default?)
  • 12.3-canary.32, SWC compiler

Next Babel and SWC sourcemaps.zip

That may be useful in seeing what's different.

markerikson avatar Oct 19 '22 18:10 markerikson

I can't build your app because yarn failed.


❯ yarn
yarn install v1.22.19
warning Missing name in workspace at "/Users/kdy1/projects/lab/devtools/packages/markerikson-stack-client-prototype", ignoring.
[1/4] 🔍  Resolving packages...
warning Resolution field "[email protected]" is incompatible with requested version "react-is@^16.13.1"
warning Resolution field "[email protected]" is incompatible with requested version "react-is@^16.7.0"
warning Resolution field "[email protected]" is incompatible with requested version "react-is@^17.0.2"
warning @redux-devtools/app > socketcluster-client > [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
warning @redux-devtools/app > socketcluster-client > [email protected]: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
warning Resolution field "[email protected]" is incompatible with requested version "[email protected]"
warning @svgr/webpack > @svgr/plugin-svgo > svgo > [email protected]: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#browser_compatibility
error An unexpected error occurred: "https://registry.yarnpkg.com/bvaughn-architecture-demo: Not found".
info If you think this is a bug, please open a bug report with the information provided in "/Users/kdy1/projects/lab/devtools/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

And I found that the source map you posted has sourcemap for debug build (webpack HMR) Can you upload a new one?

kdy1 avatar Nov 18 '22 01:11 kdy1

Here is the [[...id]-$HASH.js and [[...id]-$HASH.js.map: sourcemap.zip

jridgewell avatar Nov 18 '22 06:11 jridgewell

Thank you!

kdy1 avatar Nov 18 '22 06:11 kdy1

Afraid I'm busy at a conference today, but I should be back online on Monday. Please let me know if you still need something more!

markerikson avatar Nov 18 '22 09:11 markerikson

@markerikson Did you post the correct input/map?

image

This is babel version, and I'm not sure if this is correct

kdy1 avatar Nov 30 '22 04:11 kdy1

@kdy : yeah, the zip file I attached should have sourcemaps from four different build setup variations, no other code changes between them.

I will say that our sourcemaps have been... seemingly flaky and inconsistent in general, and I'm not sure what other issues we might have with our setup.

In this case, I just know that switching from Babel to SWC definitely made things worse.

But yeah, that screenshot does look kinda broken too :)

markerikson avatar Nov 30 '22 04:11 markerikson

@markerikson Are you using browserslist or legacyBrowsers: false? To fix source map issue, I have to check all transform passes but I want to focus on more widely used parts first. The first one if typescript, but it would be nice if I know which build target is used

kdy1 avatar Nov 30 '22 05:11 kdy1

Yeah, our current target is chrome: 95:

markerikson avatar Nov 30 '22 05:11 markerikson

This should be resolved with https://github.com/swc-project/swc/pull/6561, though that'll need to be rolled into the next release.

jridgewell avatar Dec 02 '22 18:12 jridgewell

Sweet, thanks! I'll definitely try that out as soon as you've got it available.

markerikson avatar Dec 02 '22 18:12 markerikson

Fixed by https://github.com/vercel/next.js/pull/43652

kdy1 avatar Dec 09 '22 04:12 kdy1

Thank you! We're still on Next 12 atm, but I'll try upgrading soon and see what happens.

markerikson avatar Dec 09 '22 04:12 markerikson

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.

github-actions[bot] avatar Jan 08 '23 12:01 github-actions[bot]