Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug an Angular multi-project workspace in Visual Studio Code

How can I debug an Angular multi-project workspace in VSCode using the VS Code - Debugger for Chrome? After the migration to an Angular multi-project workspace, the debugging does not work anymore. I get the following message if I set a breakpoint.

Breakpoint set but not yet bound

I found a blog post about this topic: "Visual Studio Code Breakpoints for Angular Multi-Project Workspace". I added the following to my launch.json, I replaced "webRoot": "${workspaceRoot}"" with "webRoot": "${workspaceFolder}":

{
    "name": "Launch new-app in Chrome against localhost (with sourcemaps)",
    "type": "chrome",
    "request": "launch",
    "runtimeExecutable": "/usr/bin/chromium-browser",
    "runtimeArgs": [
        "--disable-extensions"
    ],
    "url": "http://localhost:4200",
    "webRoot": "${workspaceFolder}",
    "sourceMaps": true,
    "sourceMapPathOverrides": {
        "/./*": "${webRoot}/projects/new-app/*",
        "/src/*": "${webRoot}/projects/new-app/src/*",
        "/*": "*",
        "/./~/*": "${webRoot}/node_modules/*",
    },
}

I also replaced new-app with the correct app name, but it does still not work.

The folder structure:

VSCode Angular multi project folder structure

Can anyone help me to get this working?

like image 702
BuZZ-dEE Avatar asked Nov 09 '19 14:11

BuZZ-dEE


People also ask

Can I have multiple workspaces in VS Code?

You can work with multiple project folders in Visual Studio Code with multi-root workspaces. This can be helpful when you are working on several related projects at one time.

How does multiple projects work in VS Code?

You can simply do File>New Window and open the other project in the new window. Because after you close VSCODE and launch it again it opens only one of these two windows. But with workspace you keep both.


2 Answers

I get this working by using the .scripts command to find out the correct paths for the sourceMapPathOverrides property.

    {
      "name": "Launch editor in Chrome against localhost (with sourcemaps)",
      "type": "chrome",
      "request": "launch",
      "runtimeExecutable": "/usr/bin/chromium-browser",
      "runtimeArgs": [
        "--disable-extensions"
      ],
      "url": "http://localhost:4200",
      "webRoot": "${workspaceFolder}",
      "sourceMaps": true,
      "sourceMapPathOverrides": {
          "webpack:/*": "${webRoot}/projects/apps/editor/*",
          "webpack:///./src/*": "${webRoot}/projects/apps/editor/src/*",
          "/*": "*",
          "/./~/*": "${webRoot}/node_modules/*",
      },
  }
like image 179
BuZZ-dEE Avatar answered Sep 28 '22 08:09

BuZZ-dEE


i'm using angular 10 workspace with app & library, and i was having trouble setting breakpoints in my library.

i needed to tell "ng serve" to include vendor sourcemaps...

add a "sourceMap" object to your "angular.json" under projects / "my-app" / architect / "serve" / options:

        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "app:build",
            "sourceMap": {
              "scripts": true,
              "styles": true,
              "vendor": true
            }
          },
ng build my-library
ng serve my-app

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "my-app",
            "url": "http://localhost:4200", 
            "webRoot": "${workspaceFolder}/projects/my-app",
            "sourceMaps": true,
        }
    ]
}
like image 22
Datum Geek Avatar answered Sep 28 '22 08:09

Datum Geek