使用 VSCode 调试 electron-forge 应用程序

Debug electron-forge app with VSCode

我正在尝试使用 VSCode(电子主进程,而不是渲染)调试我的 electron-forge 项目,但到处都是错误。我安装了包含所有依赖项的 electron-forge 包并初始化了我的项目。

我遵循了 this 的指示,我的 launch.json VSCode 是:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Electron Main",
            "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-forge-vscode-win.cmd",
            "cwd": "${workspaceRoot}"
        }
    ]
}

但是当我在 VSCode 中点击 F5 进行调试时,我得到了 Attribute "runtimeExecutable" does not exist 因为 electron-forge 是全局安装的所以 [=21= 中没有这样的文件] 目录

然后根据this我改成了"runtimeExecutable",我的launch.json如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Electron Main",
            "runtimeExecutable": "electron-forge-vscode-win.cmd",
            "cwd": "${workspaceRoot}"
        }
    ]
}

命令行是:

electron-forge-vscode-win.cmd --debug-brk=17423 --nolazy 
√ Locating Application
√ Preparing native dependencies
√ Launching Application

可是还是什么都没发生。我的电子应用程序启动了,但没有像 --debug-brk 参数那样停止。

接下来,我在 launch.json 中添加了一行:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "name": "Electron Main",
            "runtimeExecutable": "electron-forge-vscode-win.cmd",
            "protocol": "inspector"
        }
    ]
}

使用此命令行启动:

electron-forge-vscode-win.cmd --inspect=11172 --debug-brk 
√ Locating Application
√ Preparing native dependencies
√ Launching Application

注: 11172 为随机端口号

现在我收到这个错误:Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:11172)

在协议后的launch.json中添加【"port": 11172】即可。

我认为您需要添加 "protocol"="legacy" 到您的启动配置。这是假设您使用的节点版本 < 8.x

我得出的结论是,如果您使用 electron-forge 或 electron-compile,则不能使用 VSCode 来调试主要的 electron 进程。在这两种情况下,VSCode 调试器都会忽略断点。 BrowserWindow 直接出现,VSCode 调试控制台中出现以下消息 window:

Debugging with inspector protocol because a runtime executable is set.
c:\Users\paulk\OneDrive\dev\forge-debug/node_modules/.bin/electron.CMD --inspect=16988 --debug-brk .
Debugger listening on ws://127.0.0.1:16988/9cead160-c448-4b33-a8a2-2dff6f51ed59

有时当我关闭浏览器 window 时,"close all windows" 事件处理程序中的断点被命中。关闭 window 后,调试控制台中出现以下消息:

Debugger attached.

也许这表明 VSCode 调试器在 BrowserWindow 关闭后才会附加。

我认为问题出在 electron-forge 使用的 electron-compile 上。可能跟动态编译有关。

使用 VSCode 调试简单的 Electron 应用程序轻而易举。此外,普通 Electron 在调试器中发出不同的消息 window:

Debugging with inspector protocol because a runtime executable is set.
c:\Users\paulk\OneDrive\dev\electron-quick-start/node_modules/.bin/electron.CMD --inspect=37884 --debug-brk .
Debugger listening on port 37884.
Warning: This is an experimental feature and could change at any time.

这表明普通 Electron 使用与 electron-compile 不同的方法连接到调试器。

遗憾的是electron-forge不支持VSCode主进程调试。这对我来说毫无用处。此外,electron-forge 和 electron-compile 的开发者似乎并不认为这是一个问题。了解 electron-forge 和 electron-compile 的开发人员以及这些包的用户正在使用哪些调试器来调试主进程代码会很有帮助。