swc icon indicating copy to clipboard operation
swc copied to clipboard

Source maps aren't loaded correctly in Chrome Node DevTools

Open pbadenski opened this issue 2 years ago • 17 comments

I ran node --inspect-brk -r @swc/register some_script.ts and then opened chrome://inspect - DevTools for Node. Source maps weren't loaded properly (was showing a transpiled JS content under a TS filename). I checked with babel and it works fine. There seems to be a difference between babel and swc that makes it break - I used debugger to dynamically modify swc source maps and managed to see it working.

It looks like embedding full filename path in sources doesn't work in Chrome DevTools for Node. What babel does, and seems to make it work - is encodes directory path in sourceRoot and just filename in sources.

This works (babel):

{
  "version": 3,
  "sourceRoot": "/Users/pbadenski/workspace/project/",
  "sources": [
    "test.ts"
  ],
  ...
}

This doesn't (swc):

{
  "version": 3,
  "sources": [
    "/Users/pbadenski/workspace/project/test.ts"
  ],
  ...
}

pbadenski avatar Oct 06 '21 17:10 pbadenski

Can you try https://swc.rs/docs/configuring-swc/#inlinesourcescontent ?

kdy1 avatar Oct 09 '21 05:10 kdy1

I tried, doesn't help unfortunately.

pbadenski avatar Oct 09 '21 16:10 pbadenski

@kdy1 Just flagging that that this and similar source map issues are pretty serious obstacle for our team. We have recently switched all development to swc. We unfortunately might need to partially go back to babel because of difficulty with debugging and exception stack traces. I think it might be similar for other teams. I'm wondering whether you thought of giving these type of issues higher priority?

pbadenski avatar Dec 08 '21 15:12 pbadenski

Do we know only chrome devtools affected by this or other tools are also broken? I recall in few tools (like jest?) saw correctly mapped source locations so curious if this is tool specifics.

Per spec,

If the sources are not absolute URLs after prepending of the “sourceRoot”, the sources are resolved relative to the SourceMap

I guess having each sources having full path without sourceroot would behave same. Maybe lacks of sourceRoot property relates with this.

kwonoj avatar Dec 08 '21 19:12 kwonoj

It seems to a bit of hit and miss. WebStorm works in some situations - but also had many issues and reports from my team that it frequently doesn't.

pbadenski avatar Dec 08 '21 21:12 pbadenski

Curiousity q: if you manually add sourceRoot: "" in existing sourcemap, does it makes any diff?

kwonoj avatar Dec 08 '21 21:12 kwonoj

I have a similar report where source maps were working when debugging unit tests with VSCode but not WebStorm. I haven't had time to investigate that issue in particular but we pinned swc/core to 1.2.100 because the source maps are broken in WebStorm in 1.2.101.

The swc deps it works with:

"@swc-node/jest": "1.4.1",
"@swc/core": "1.2.100",
"swc-loader": "0.1.15",

egilsster avatar Dec 10 '21 10:12 egilsster

@egilsster Can you try inlineSourcesContent: false?

Judging from https://github.com/swc-project/swc/milestone/202?closed=1, and there's https://github.com/swc-project/swc/pull/2471 which changes the default behavior.

kdy1 avatar Dec 10 '21 10:12 kdy1

Tried (a variation of options with inlineSourcesContent and sourceMaps) with 118 and 119 just now in VSCode and neither is working correctly at the moment while babel-loader is working. Running Jest in a debugging terminal in VSCode and none of my breakpoints are hitting. Simply reporting back but don't have the bandwidth to investigate that issue right now. Will create an issue if I manage to get a repro up.

NOTE: I am using a project with swc packages only (not the swc-node variant):

"@swc/core": "1.2.119",
"@swc/jest": "0.2.14",
"swc-loader": "0.1.15",

egilsster avatar Dec 13 '21 12:12 egilsster

It looks like there are multiple different roots to investigate (from @swc/register to something else). Anyone experiencing this issue mind share exact repro steps for each? It's hard to do guesswork with currently known information.

kwonoj avatar Dec 14 '21 04:12 kwonoj

Just FYI looks, I'm extremely happy. I haven't had any problems since recently - I will keep this thread updated. Thank you!! :star:

pbadenski avatar Dec 24 '21 12:12 pbadenski

Can you try the latest version of @swc/core? (v1.2.155)

Patches in https://github.com/swc-project/swc/pull/4007 are very likely to fix this issue.

kdy1 avatar Mar 14 '22 11:03 kdy1

Can anyone make an executable repro case? node.js seems to consume sourcemap of swc without any problem.

See https://github.com/kdy1/swc/tree/6cea544afa481e063cff6d8d493d8d12dadb280a/crates/swc/tests/stacktrace for exmaple repro case.

kdy1 avatar Mar 16 '22 00:03 kdy1

Can you try the latest version? (v1.2.156) There was a bug fix for a module that only contains export * from './foo' and such module is super common, so I think it can be the cause of this.

kdy1 avatar Mar 16 '22 10:03 kdy1

If it helps, I'm still having the same issue and was able to create a quick repo to reproduce it: https://github.com/daviduzumeri/swctest

Any help would be appreciated, as we're pretty much back to console.log debugging over here.

daviduzumeri avatar Aug 25 '22 17:08 daviduzumeri

I found that sourcemap crate does not support sourceRoot. https://docs.rs/sourcemap/6.1.0/sourcemap/struct.SourceMapBuilder.html

kdy1 avatar Aug 30 '22 05:08 kdy1

Ref: https://github.com/getsentry/rust-sourcemap/issues/50

kdy1 avatar Aug 30 '22 05:08 kdy1

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-bot avatar Feb 23 '24 12:02 swc-bot