Can not find sourcemaps while debugging in VSCode
I managed to connect from VSCode to the electron app, using this launch.config:
{
"name": "Debug",
"type": "chrome",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"runtimeArgs": [
"${workspaceRoot}/dist",
"--enable-logging",
"--remote-debugging-port=9222"
],
"sourceMaps": true,
"webRoot": "${workspaceRoot}/dist"
}
But I can't set any breakpoinst cause sourcemaps are not found. I tried to include this line
"sourceMapPathOverrides": {
"webpack:///./*": "${webRoot}/*"
}
from the Angular CLI launch.json file, but to no avail. Is there anyone who got this to work ?
I would be happy, if someone could help us.
I would also love to know how to debug from VSCode!
Here is what I did to debug from VSCode and Chrome Developer Tools:
-
Sourcemaps for the main process: Add
--sourceMap trueto the"build:electron:main"script in package.json to create sourcemaps for main.ts when build the electron application. Full script:"build:electron:main": "tsc main.ts --outDir dist --sourceMap true && copyfiles package.json dist && cd dist && npm install --prod && cd ..", -
Sourcemaps for the renderer process: Delete the flowing lines in
webpack.config.jsto create sourcemaps for all Angular code:
// plugins.push(new SourceMapDevToolPlugin({
// "filename": "[file].map[query]",
// "moduleFilenameTemplate": "[resource-path]",
// "fallbackModuleFilenameTemplate": "[resource-path]?[hash]",
// "sourceRoot": "webpack:///"
// }));
- Create a configuration in the
launch.jsonto run the Electron application in debug mode:
{
// 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": "node",
"request": "launch",
"name": "Electron: Main",
"program": "${workspaceFolder}\\main.ts",
"outFiles": [
"${workspaceRoot}/dist/main.js"
],
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"runtimeArgs": [
"--remote-debugging-port=9223",
"./dist"
]
},
]
}
- Start debuging with above configuration in Visual Studio Code. I can set breakpoints in main.ts now. But to debug the Angular code, I need to open the Chome Developer Tools and set breakpoints.
Summary:
- Debug the main process code in Visual Studio Code
- Debug the render process code in Chrome Developer Tools
I try to config to debug all Angular code in Visual Studio Code but not success.
If anyone want to help, here is my full configurations in the 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": "node",
"request": "launch",
"name": "Electron: Main",
"program": "${workspaceFolder}\\main.ts",
"outFiles": [
"${workspaceRoot}/dist/main.js"
],
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"runtimeArgs": [
"--remote-debugging-port=9223",
"./dist"
]
},
{
"name": "Electron: Renderer",
"type": "chrome",
"request": "attach",
"port": 9223,
"webRoot": "${workspaceRoot}/dist",
"timeout": 30000
},
],
"compounds": [
{
"name": "Electron: All",
"configurations": [
"Electron: Main",
"Electron: Renderer"
]
}
]
}
But now I need to start debugging with "Electron: All" configuration.
Try this:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Electron: Main",
"program": "${workspaceFolder}\\main.ts",
"outFiles": [
"${workspaceRoot}/dist/main.js"
],
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"runtimeArgs": [
"--remote-debugging-port=9223",
"./dist"
]
},
{
"name": "Electron: Renderer",
"type": "chrome",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"runtimeArgs": [
"${workspaceRoot}/dist/main.js",
"--remote-debugging-port=9222"
],
"webRoot": "${workspaceRoot}"
},
]
}
I figure out how to debug the render process and the entire Angular project, follow the steps from the @nccnm comment from 1 to 2 and for step 3 you must use the following config in your launch.json:
{
"name": "Electron: Renderer",
"type": "chrome",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"runtimeArgs": [
"${workspaceRoot}/dist/main.js",
"--remote-debugging-port=9222"
],
"timeout": 30000,
//"preLaunchTask": "npmStart",
"webRoot": "${workspaceRoot}"
}
Also you need to modify the script for electron in the package.json file like this:
"electron:serve": "npm run build:electron:main && electron ./dist --serve --remote-debugging-port=9222"
Also don't forget to disable the dev tools in main.ts comment the line with the code win.webContents.openDevTools();
With this you can execute using npm start and then start the debugger, or you can use the preLaunchTask and create a tasks.json for run the project before the debugger.
This config helps me:
{
"name": "Electron: Renderer",
"type": "chrome",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"runtimeArgs": [
"--remote-debugging-port=9222",
".",
],
"sourceMapPathOverrides": {
"webpack:\\*": "${workspaceRoot}/*"
},
"sourceMaps": true,
"timeout": 30000,
//"preLaunchTask": "npmStart",
"webRoot": "${workspaceRoot}/dist"
}
Not working now.
Spent hours today, finally got the launch debugger working for the main process.
{
"type": "node",
"request": "launch",
"name": "Electron Main",
"program": "${workspaceFolder}/main.ts",
"outFiles": [
"${workspaceRoot}/main.js",
],
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"args": ["--serve"],
"runtimeArgs": [
".",
],
"outputCapture": "std",
"preLaunchTask": "npm: electron:serve-tsc",
},
Keep in mind my main.ts file outputs main.js in the same top level directory. And more importantly, you need to have the ng serve running ahead of time because this is not running off of the dist folder. Its serving from the devkit server.
Hope this helps