microsoft / vscode-chrome-debug-core

A library for implementing VS Code debug adapters for targets that implement the Chrome Debugging Protocol.
Other
157 stars 119 forks source link

sourcemap failing inside vscode but working inside chrome devtools #544

Open dmail opened 5 years ago

dmail commented 5 years ago

Depending where the sourcemap file is, vscode fails to locate the source while chrome dev tools has no problem to find it.

If you want to reproduce you can try with the first file structure where both vscode and chrome devtools are working. Then compare with the second file structure that works only in chrome devtools.

File structure working in both

file.js

const value = true

debugger

console.log(value)

file.es5.js

var value = true;

debugger;

console.log(value);
//# sourceMappingURL=file.es5.js.map

file.es5.js.map

{
  "version": 3,
  "sources": [
    "./file.js"
  ],
  "names": [
    "value",
    "console",
    "log"
  ],
  "mappings": "AAAA,IAAMA,QAAQ,IAAd;;AAEA;;AAEAC,QAAQC,GAAR,CAAYF,KAAZ",
  "file": "file.js"
}

Vscode pauses in file.js

image

Chrome dev tools pauses in file.js

image

File structure failing in vscode

file.js

const value = true

debugger

console.log(value)

file.es5.js

var value = true;

debugger;

console.log(value);
//# sourceMappingURL=__asset__/file.es5.js.map

asset/file.es5.js.map

{
  "version": 3,
  "sources": [
    "../file.js"
  ],
  "names": [
    "value",
    "console",
    "log"
  ],
  "mappings": "AAAA,IAAMA,QAAQ,IAAd;;AAEA;;AAEAC,QAAQC,GAAR,CAAYF,KAAZ",
  "file": "file.js"
}

vscode pauses inside file.es5.js

image

chrome devtools pauses in file.js

image

Additional info

When I put "trace": true inside launch.json one log draws my attention:

SourceMap: no sourceRoot specified, using script dirname

I think it corresponds to the following line in getComputedSourceRoot https://github.com/microsoft/vscode-chrome-debug-core/blob/87d2c194179033e38d68308027c1d1dbbc43c454/src/sourceMaps/sourceMapUtils.ts#L53

It looks like vscode fallbaks on the script dirname but should fallback on the sourcemap dirname. Otherwise when sourcemap file and script file are not in the same directory sourceroot is wrong.

https://github.com/microsoft/vscode-chrome-debug-core/blob/87d2c194179033e38d68308027c1d1dbbc43c454/src/sourceMaps/sourceMapUtils.ts#L52

dmail commented 5 years ago

I forgot to tell you how I execute the files:

For chrome I open the following html file.

<script src="./file.es5.js"></script>

For vscode I uses the following launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch with node",
      "type": "node",
      "request": "launch",
      "program": "${file}",
      "sourceMaps": true,
      "trace": true,
    },
  ]
}
sangxxh commented 3 years ago

@dmail is your sourceMappingURL correct? You put //# sourceMappingURL=__asset__/file.es5.js.map but your source map file is in asset folder.

dmail commented 3 years ago

I think it was a mistake when I wrote my comment, I forgot to put __asset__ and wrote asset instead. But I remember testing it with actual files and chrome was ok so I assume I had no such mistake in the actual files I used to reproduce.

Have you tried to reproduce? I will try on my side when feeling more energy to do so.

dmail commented 3 years ago

It looks like vscode fallbaks on the script dirname but should fallback on the sourcemap dirname.

Any thought regarding this suggestion ?

sangxxh commented 3 years ago

I think it was a mistake when I wrote my comment, I forgot to put __asset__ and wrote asset instead. But I remember testing it with actual files and chrome was ok so I assume I had no such mistake in the actual files I used to reproduce.

Have you tried to reproduce? I will try on my side when feeling more energy to do so.

I thought I had the same issue in my project so I created a simple web app with webpack but I couldn't replicate the problem - the debugger works fine when I put the source map in a different folder from the js file.

Not sure why it doesn't work for the other app I have though.

sangxxh commented 3 years ago

I thought I had the same issue in my project so I created a simple web app with webpack but I couldn't replicate the problem - the debugger works fine when I put the source map in a different folder from the js file.

Not sure why it doesn't work for the other app I have though.

Turns out I had some wrong webpack config for sourcemap path in my project, resulting in wrong sourceMappingURL.

From my side, VSCode debugger has no problem when we put source map in a different folder from the JS file.

dmail commented 3 years ago

I see thanks, I try to repro today and let you know if I can still reproduce

dmail commented 3 years ago

Still reproductible. VsCode still fails to find the sourcemap file while chrome devtools are fine.

vscode-notif-sourcemap-not-found

(I have updated my first comment with the correct filenames in case someone wants to reproduce).

sangxxh commented 3 years ago

How did you generate the source map files?

In my case it's "sources":["webpack:///./src/index.js"], it's different format from yours:

  "sources": [
    "../file.js"
  ],
dmail commented 3 years ago

I have generated them programatically using babel.

Webpack seems to use a special scheme (webpack://) that vscode seems to understand and resolve differently than relative path