简体   繁体   English

VSCode-如何使用启动配置调试启动其自己的调试器的Node程序?

[英]VSCode - How to debug a Node program that starts its own debugger, using a launch configuration?

(Note : my specific use case may seem complex, but the underlying idea is not!) (注意:我的特定用例可能看起来很复杂,但基本思想却并非如此!)

I have a Gulp script that starts a Docker container, which itself contains and starts another Gulp script that use Nodemon to start a final Node script in debug mode! 我有一个Gulp脚本,它启动了一个Docker容器,该容器本身包含并启动了另一个使用Nodemon在调试模式下启动最终Node脚本的Gulp脚本! The Nodemon Gulp script ran inside the Docker container looks something like this : 在Docker容器中运行的Nodemon Gulp脚本如下所示:

nodemon({
    "script": `start.js`,
    "nodeArgs": [`--debug=0.0.0.0:5858`, "-nolazy"],
    "ext": "js",
    "restartable": true
});

I'd like to be able to press [F5] in Visual Studio Code, have the final script started and have VSCode's debugger attached to it! 我希望能够在Visual Studio Code中按[F5] ,启动最终脚本,并附加VSCode的调试器!

What does work : 什么工作

If I start the first Gulp script manually, in a terminal, the Docker container is started, the other embedded Gulp script with Nodemon is executed and the final script is started. 如果我手动启动第一个Gulp脚本,则在终端中,启动Docker容器,执行另一个带有Nodemon的嵌入式Gulp脚本,并启动最终脚本。 I can then press [F5] in VSCode to start an attach launch configuration and I'm able to debug! 然后,我可以在VSCode中按[F5]来启动attach启动配置,并且可以调试了! The port 5858 is exposed by Docker and everything runs fine. Docker暴露了端口5858 ,一切运行正常。

What I want : 我想要的是

I want to be able to skip the manual launching of the script in a terminal. 我希望能够跳过终端中脚本的手动启动。 I want a VSCode launch configuration that does everything by itself => launch the script in VSCode's integrated terminal and attach a debugger to the debug process started by the script itself. 我想要一个可以自己完成所有事情的VSCode启动配置=>在VSCode的集成终端中启动脚本,并将调试器附加到脚本本身启动的调试过程中。

I tried : 我试过了

  1. An "request": "attach" launch configuration with a preLaunchTask task. 带有preLaunchTask任务的"request": "attach"启动配置。 That task is launching the first Gulp script. 该任务正在启动第一个Gulp脚本。 The problem with this approach is that the preLaunchTask task never ends : it starts the first script in the terminal (The task has a : " "_runner": "terminal" ") but in the end the final script is listening and doesn't exit (it's actually listening for requests)... This seems to prevent the VSCode debugger to start because the preLaunchTask task never exit. 这种方法的问题在于preLaunchTask任务永远不会结束:它在终端中启动第一个脚本(该任务具有:“ "_runner": "terminal" ”),但最后,最后一个脚本正在侦听而没有退出(实际上正在监听请求)...这似乎阻止了VSCode调试器启动,因为preLaunchTask任务永远不会退出。

  2. A "request": "launch" launch configuration that starts the first script. 启动第一个脚本的"request": "launch"启动配置。 But here, even if it looks like it is going to work (the orange debugging bar appears), the debugging never actually work. 但是在这里,即使看起来像要工作(出现橙色的调试栏),调试也从未真正起作用。 If I understand correctly, this is because a launch launch configuration starts a Node debugger by itself (on the specified port) and therefore the debugger starts by Nodemon, inside the Docker container, will never be listened to. 如果我理解正确,那是因为launch启动配置会自行(在指定端口上)启动Node调试器,因此调试器将永远不会被Docker容器内的Nodemon启动。

In other words : I simply want to hit [F5] so a script is launched in the integrated terminal, without debugger, and then VSCode will attach a debugger to the resulting 127.0.0.1:5858 debug process, wathever how this process is actually started. 换句话说:我只是想点击[F5]以便在集成终端中启动脚本,而无需调试器,然后VSCode会将调试器附加到生成的127.0.0.1:5858调试过程中,无论该过程实际上是如何开始的。

UPDATE : I also had a suggestion on Github about trying to use a compound launch configuration, but it also doesn't work: https://github.com/Microsoft/vscode/issues/36685 更新 :我在Github上也有一个关于尝试使用compound启动配置的建议,但它也不起作用: https : //github.com/Microsoft/vscode/issues/36685

Your first try, an attach config with a preLaunchTask is correct. 您的第一次尝试是带有preLaunchTaskattach配置正确。 You probably just need one adjustment. 您可能只需要进行一次调整。 By default, vscode will be waiting for the task to terminate, so you need to tell it that the task will run in the background, by adding "isBackground": true . 默认情况下,vscode将等待任务终止,因此您需要通过添加"isBackground": true来告知任务该任务将在后台运行。 Then you need to tell it which patterns to watch for in the task's output to know when the task is complete. 然后,您需要在任务的输出中告诉它要监视的模式,以知道任务何时完成。 This bit is a little annoying, because you have to do this with a problemMatcher, but this task should not contribute problems, so you need to give it a regex that won't match anything. 这有点令人讨厌,因为您必须使用problemMatcher来执行此操作,但是此任务不应引起问题,因此您需要为其提供一个不匹配任何内容的正则表达式。 eg: 例如:

"problemMatcher": {
    "pattern": {
        "regexp": "__________"
    },
    "background": {
        "activeOnStart": false,
        "beginsPattern": "Some pattern when the debugging process is about to start",
        "endsPattern": "Ready for attach"
    }
}

The task runner is watching the program output to match endsPattern - when some output matches, then it will know the program is ready for the debugger to attach. 任务运行者正在监视程序输出以匹配endsPattern当某些输出匹配时,它将知道程序已准备好连接调试器。 If your script isn't producing any output, you should add some console.log after invoking nodemon . 如果您的脚本没有产生任何输出,则应在调用nodemon之后添加一些console.log。

Usually the problemMatcher is for matching problems output by the build task, and the regex can match the filename, line, and error message. 通常,problemMatcher用于匹配构建任务输出的问题,而正则表达式可以匹配文件名,行和错误消息。 But here we are just using it for the "background" patterns, so we give it a dummy regex. 但是这里我们只是将其用于“背景”模式,因此我们为其提供了一个虚拟正则表达式。 Here's a thread describing this workaround and how it could be easier in the future by moving the "background" patterns out of the patternMatcher: https://github.com/Microsoft/vscode/issues/6209#issuecomment-289411630 这是描述此解决方案的线程,以及将来如何通过将“背景”模式移出patternMatcher来使它更容易: https : //github.com/Microsoft/vscode/issues/6209#issuecomment-289411630

And here is the documentation on watching tasks, for more details: https://code.visualstudio.com/docs/editor/tasks#_background-watching-tasks 以下是观看任务的文档,以了解更多详细信息: https : //code.visualstudio.com/docs/editor/tasks#_background-watching-tasks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM