angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

SourceMap mapping webpack to wrong file location in VSCode Debug

Open pderks opened this issue 10 years ago • 44 comments

OS: Windows 10 angular-cli: 1.0.0-beta.16 node: 6.7.0

When creating a new project using the angular-cli and attempting to debug, none of the breakpoints I am setting are being hit. After looking into the log file for the debugger I have found that webpack is mapping to the wrong source location. I have listed repro steps along with the location of a log file, my launch configurations and an example of what I am seeing when I say that webpack is mapping to the wrong location. I was hoping someone had some insight into why this is happening.

Repro steps:

  1. Download Debugger for Chrome in VS Code
  2. Create new project using angular-cli
  3. Set Launch.json configurations to launch in chrome with diagnosticLogging set to true
  4. Set breakpoints in typescript files and Launch the app
  5. Log file can be found at C:\Users{{your username}}.vscode\extensions\msjsdiag.debugger-for-chrome-0.5.2\vscode-chrome-debug.txt

Example from the log (entire log file attached): vscode-chrome-debug.txt

SourceMap: mapping webpack:///C:/Users/pderks/Desktop/test-app/src/app/app.component.ts => c:\Users\pderks\Desktop\test-app\C:\Users\pderks\Desktop\test-app\src\app\app.component.ts

**Notice that the path c:\Users\pderks\Desktop\test-app is appearing twice.

Launch configurations: { "version": "0.2.0", "configurations": [ { "name": "Launch Chrome against localhost, with sourcemaps", "type": "chrome", "request": "launch", "url": "http://localhost:4200", "diagnosticLogging": true, "sourceMaps": true, "webRoot": "${workspaceRoot}" } ] }

pderks avatar Sep 30 '16 23:09 pderks

Thanks to your comment I finally succeeded in debugging an angular-cli (1.0.0-beta.16 with webpack) generated app in vscode.

You need to remap webpack:///* in launch.json.

Here's my lauch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Chrome against localhost, with sourcemaps",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:4200/",
            "sourceMaps": true,
            "webRoot": "${workspaceRoot}",
            "sourceMapPathOverrides": {
                "webpack:///*": "/*"
            }
        },
        {
            "name": "Attach to Chrome, with sourcemaps",
            "type": "chrome",
            "request": "attach",
            "port": 9222,
            "sourceMaps": true,
            /*"diagnosticLogging": true,*/
            "webRoot": "${workspaceRoot}",
            "url": "http://localhost:4200/*",
            "sourceMapPathOverrides": {
                "webpack:///*": "/*"
            }
        }
    ]
}

diagnosticLogging is optional (very verbose and hard to read in vscode console, the best is to read ~/.vscode/extensions/msjsdiag.debugger-for-chrome-0.5.2/vscode-chrome-debug.txt and search relevant info in any text editor).

Hope this helps.

sdiprizio avatar Oct 02 '16 10:10 sdiprizio

@sdiprizio Thanks, it seems to be detecting the source maps for the bundle now. Although, it still can't hit a single breakpoint in VSCode.

yuri1969 avatar Oct 02 '16 12:10 yuri1969

Thanks @sdiprizio this works for me.

@yuri1969 I found I needed to make sure I only had one chrome tab open for this to work.

duncanhunter avatar Oct 03 '16 01:10 duncanhunter

@sdiprizio Thanks! After mapping wepack to the correct location I was able to hit break points when navigating to different areas my site. I thought I'd note though that I had to change the override to map like this:

"sourceMapPathOverrides": { "webpack:///C:/Users/{{username}}/Dekstop/test-app/": "C:/Users/{{username}}/Desktop/test-app/" }

pderks avatar Oct 03 '16 18:10 pderks

OK, the key was analysis of the ~/.vscode/extensions/msjsdiag.debugger-for-chrome-<version>/sourceMapPathOverride.txt logfile.

You have to tweak the sourceMapPathOverrides in launch.json until you see the mappings themselves in the logfile. Eg.:

SourceMap: mapping <in_path> => <out_path>, via sourceMapPathOverrides entry - ""

It seems the problem, in my case, are Windows path delimiters vs. URL ones. The webpack path looks like: webpack:///c:/foo/bar/angular-project, although ${workspaceRoot} is: c:\foo\bar\angular-project. Thus there is a mismatch in the separators which can be worked-around by hardcoding the webpack path.

My final setup:

{
          "name": "Launch Chrome against localhost, with sourcemaps",
          "type": "chrome",
          "request": "launch",
          "url": "http://localhost:4200/",
          "sourceMaps": true,
          "webRoot": "${workspaceRoot}",
          "sourceMapPathOverrides": {
              "webpack:///c:/foo/bar/angular-project/*": "${workspaceRoot}/*"
          }
 }

yuri1969 avatar Oct 03 '16 19:10 yuri1969

The file vscode-chrome-debug.txt isn't generated in my case at that directory, but applying the launch.json @sdiprizio wrote, it worked for me. And I also had to make sure I only had one chrome tab open for this to work, like @duncanhunter

jorgeas80 avatar Oct 03 '16 23:10 jorgeas80

In Windows with

             "sourceMapPathOverrides": {
                 "webpack:///C:*":"C:/*"
             }

is enough.

Also I recommend add: "userDataDir": "C:\\temp\\chromeDummyDir", So a new chrome instance will be open with debug enabled

And change the WebRoot "webRoot": "${workspaceRoot}/src",

Another tip

"runtimeArgs": [
                "--disable-session-crashed-bubble",
                "--disable-infobars"
            ],

Now the message "Chrome didn't shut down correctly" will be disabled

rodries avatar Oct 04 '16 09:10 rodries

@rodries were you able to get this to work with webpack-dev-server?

cgatian avatar Oct 04 '16 18:10 cgatian

@cgatian if you mean "ng serve" command then yes. Run ng serve, then adjust vs code configuration with the proposed changes (select debug with Chrome). Now you can debug without problems (at least in my case)

jmesa-sistel avatar Oct 05 '16 07:10 jmesa-sistel

Great investigation work everyone! This has been an outstanding issue for a while now so I appreciate the effort that has gone into figuring out solutions.

The override seems to work, but if this is fixable within the CLI we'd like to do it. Off the top of my head it sounds like a VSCode Chrome debugger problem because:

  • source maps appear to be working correctly in chrome
  • this mapping (done by the debugger) shows some kind of broken mapping:
SourceMap: mapping webpack:///C:/Users/pderks/Desktop/test-app/src/app/app.component.ts => 
c:\Users\pderks\Desktop\test-app\C:\Users\pderks\Desktop\test-app\src\app\app.component.ts

In this issue https://github.com/Microsoft/vscode-chrome-debug/issues/158 there seem to be reports of stuff 'just working', so I wonder is something is amiss on the CLI's end.

filipesilva avatar Oct 09 '16 00:10 filipesilva

Some additional info about caching: I can get debugging working for a project as per rodries post above. But if I then attempt to debug a different project then VS Code will try to reference the sourcemaps from the first project. Using the debug output I have found these symbols are cached at the following path:

%TEMP%\com.microsoft.VSCode\node-debug2\sm-cache

I need to delete this folder before I can then successfully debug the second project. If I want to debug the first project again, I have to again delete the above sm-cache folder.

aconsolati avatar Oct 11 '16 02:10 aconsolati

@aconsolati You are right, I have a script to delete temp folder, I have to clean the %TEMP% folder when I change to another project. It will be great to create an issue about this problem in https://github.com/Microsoft/vscode-node-debug2/issues Can you do it?

jmesa-sistel avatar Oct 11 '16 07:10 jmesa-sistel

I'm not able get this working. I added this as escribed earlier

"sourceMapPathOverrides": { "webpack:///C:*":"C:/*" }

to launch,json, but when I try to set a breakpoint I get a warning [debugger-for-chrome] Cannot read property 'locations' of undefined.

Edit: noticed the comment about caching. I have to delete the cache each time any change is made to the current project (not just when switching project). If I do that and reconnect the debugger, then I am able to set and hit breakpoints.

%TEMP%\com.microsoft.VSCode\node-debug2\sm-cache

Is this not specific to using ng-cli / webpack combination, as I never have had this issue with angularJs / TS?

jkristia avatar Oct 14 '16 17:10 jkristia

Just letting people know, I still wasn't able to get this to work on a Mac, using the latest versions of everything and a vanilla sample app. A step-by-step with a single correct config file would be much appreciated.

I've never gotten VSCode Chrome debugging working well and have usually fallen back to jasmine SpecRunner and debugging in the browser instead. I'm not sure I can get that running here either (#2853 asks for it). As of this minute I can't debug, though I was able to get wallaby running fine using their example (wallabyjs/ngCliWebpackSample).

spicemix avatar Oct 25 '16 07:10 spicemix

@spicemix I've been stuggling too on Linux and no luck so far. I've just opened a stackoverflow question here.

maxime1992 avatar Nov 05 '16 21:11 maxime1992

@maxime1992 @spicemix @filipesilva

Can confirm that the config below works fine for me with angular-cli: 1.0.0-beta.19-3. Make sure to have the running app tab in chrome active before using attach. Launch will automatically make this happen, but starts another window, which you might or might not like.

I've also added the --remote-debugging-port=9222 flag to the launcher for Chrome in Ubuntu (16.04)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Chrome against localhost, with sourcemaps",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:4200",
            "sourceMaps": true,
            "diagnosticLogging": true,
            "webRoot": "${workspaceRoot}/src/app",
            "sourceMapPathOverrides": {
                "webpack:///*": "/*"
            }
        },
        {
            "name": "Attach to Chrome, with sourcemaps",
            "type": "chrome",
            "request": "attach",
            "port": 9222,
            "sourceMaps": true,
            "diagnosticLogging": true,
            "webRoot": "${workspaceRoot}/src/app",
            "sourceMapPathOverrides": {
                "webpack:///*": "/*"
            }
        }
    ]
}

krueger71 avatar Nov 09 '16 14:11 krueger71

As of @krueger71's last comment, I'm going to consider this fixed for now. If not, please comment here and make sure to mention me as well as the current state with beta 19-3.

hansl avatar Nov 10 '16 21:11 hansl

I'm trying to get it working using :

C:\Workspaces\NinjaSquad\ponyracer>ng -v angular-cli: 1.0.0-beta.18 node: 6.9.1 os: win32 x64

I may have missed something in my local configuration, I've tried : "webRoot": "${workspaceRoot}/src/app", "webRoot": "${workspaceRoot}/src", "webRoot": "${workspaceRoot}",

as for : "webpack:///C:": "C:/" "webpack:///C:_": "C:_"

I've also looked at the : %TEMP%\com.microsoft.VSCode\node-debug2\sm-cache the %TEMP% points to C:\users\MyUser\AppData\Local\Temp and there's no such com.microsoft.VSCode so my guess is that using windows & vscode it's somewhere else :p ?

here is my setup :

image

Anyway, when I add a breakpoint after attaching the debugger at the line pointed with the black arrow, the console output this :

SourceMaps.setBP: Mapped c:\Workspaces\NinjaSquad\ponyracer\src\app\menu\menu.component.ts to http://localhost:4200/main.bundle.js SourceMaps.setBP: Mapped c:\Workspaces\NinjaSquad\ponyracer\src\app\menu\menu.component.ts:17:1 to http://localhost:4200/main.bundle.js:54337:1 Paths.setBP: http://localhost:4200/main.bundle.js is already a URL SourceMaps.setBP: Mapped http://localhost:4200/main.bundle.js:54337:5 to c:\Workspaces\NinjaSquad\ponyracer\src\app\menu\menu.component.ts:17

so i've tryied to open the main.bundle.js and this is what I have :

image

The line changes times to times but always points 2 lines ahead so i'm wondering where does my mistake comes from

tebeco avatar Nov 14 '16 13:11 tebeco

@tebeco update to the latest version and see if you are still having the same issue

deebloo avatar Nov 14 '16 14:11 deebloo

you mean beta19.3 ? I'll check, I was trying to avoid that, i think that the tutorial i'm following might have a tight integration with beta-18

Update (with beta 18) : I just tried : (edited packages.json to previous 2.0.3)

rimraf ./node_modules npm install -g typescript 2.0.3

With version 2.0.9 of typescript it seems to fail

tebeco avatar Nov 14 '16 14:11 tebeco