4

I read many related questions where debugging does not work. In my case it works but it's "weird". I set breakpoint at one line of code but it breaks in completely diferrent line. I feel it's related to source maps, but not sure what it is.

Environment:

  • Latest vscode(1.21.1)
  • Latest Chrome debugger extension installed
  • Lastest npm/angular CLI
  • Latest Chrome (65.0.33...)

Generated project using CLI with ng new

Following config added for F5 launcher(launch.json):

 "configurations": [        
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:4200",
            "webRoot": "${workspaceFolder}"
        }
    ]

This is code of app.component.ts:

import { Component, OnInit } from '@angular/core';
import { isUndefined } from 'util';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = "Portal";

  ngOnInit(): void {

    this.test();
  }

  test() {
    const person = { FirstName: "John" };
    person.FirstName = "sf";

    let b = "test string";

    b = "other string";
  }
}

Set breakpoint on something like b = "other string"

Start as usual in terminal ng serve Click F5 - Chrome opens and displays main screen.

Problem 1: Chrome doesn't brake at all.

When I refresh Chrome page - Breakpoint get hit but in a wrong place. It can be in ngOnInit or on Component line, pretty "random".

Problem 2: Breaking happens on a wrong line

Is there any way to make this work so I can set and debug without pain using VSCode?

3
  • What OS are you on? I'm on a Mac and get the same behavior. It works the first time but then the breakpoints starts getting off. Restarting chrome and ng-serve seems to work its not really satisfactory. Commented Apr 2, 2018 at 23:27
  • It's on Windows Commented Apr 5, 2018 at 16:09
  • Possible duplicate of Visual Studio Code breakpoint appearing in wrong place Commented Oct 1, 2019 at 14:26

2 Answers 2

0

It's hard to say why things behave oddly with your setup. I also use VSCode to debug Angular apps and my setup works pretty well, I'll share it here so that hopefully it can be of help.

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "Launch Chrome",
      "url": "http://localhost:4200",
      "webRoot": "${workspaceRoot}",
      "sourceMaps": true,
      "userDataDir": "${workspaceRoot}/.vscode/chrome",
      "runtimeArgs": [
        "--disable-session-crashed-bubble"
      ]
    },
    {
      "name": "Attach Chrome",
      "type": "chrome",
      "request": "attach",
      "url": "http://localhost:4200",
      "port": 9222,
      "webRoot": "${workspaceRoot}",
      "sourceMaps": true
    }
  ]
}

  • Serve your angular app with ng serve

You should now be able to start debugging your app (with F5) and all the breakpoints should work fine.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answer! Can you explain what is the second "attach Chrome" config does? I did make this change - same thing. Brakepoint missed on first run. If I "refresh" - breaks on some invalid/random line comparing to what I set. Latest Chrome debugger extension installed
Weird... see if this answer can be of any help: stackoverflow.com/questions/40443217/…
That answer doesn't help. In my case it's technically "working" because once it breaks I can step through, etc. Looks like problem is with "maps" where breakpoint line # is not correctly checked/applied
0

This seems to be related to this question:

Angular CLI 1.7.0 and Visual Studio Code - can't set breakpoints

The solution there was to install an older version of the angular cli.

2 Comments

I can set breakpoints, they just not get hit properly and move
That is the issue in the other question too, even if the title says not able to set, the poster there could set them but they are not hit correctly. Changing to and older Angular cli work for me also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.