【问题标题】:What are the correct beginsPattern and endsPattern for a background Task in VSCode?VSCode 中后台任务的正确 beginPattern 和 endsPattern 是什么?
【发布时间】:2018-01-27 17:48:26
【问题描述】:

我有一个静态网站(即只有 html 和客户端 JavaScript),我在本地调试时使用 python 提供服务。我有一个 VSCode 任务将正确启动 python,并试图在 Debugger for Chrome 启动任务上将该任务设置为 preLaunchTask。期望的行为是,每当我开始调试下面的serve 任务时,可以确保网站得到服务。

如果我正确理解后台任务,可以设置beginsPatternendsPattern 来表示状态更改。

我期待当python回显时

在 0.0.0.0 端口 8000 (http://0.0.0.0:8000/) 上提供 HTTP ...

stdout,下面的problemMatcher 将向启动任务发出信号,它已经开始。相反,启动任务会一直等待,直到任务的 shell 命令终止后才会继续。

可以配置任务来实现这种行为吗?

启动配置

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:8000",
            "webRoot": "${workspaceFolder}/webroot",
            "preLaunchTask": "serve"
        }
    ]
}

服务任务

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "serve",
            "type": "shell",
            "command": "python3 -m http.server",
            "windows": {
                "command": "py -m http.server"
            },
            "isBackground": true,
            "options": {
                "cwd": "${workspaceFolder}/webroot"
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "dedicated"
            },
            "problemMatcher": {
                "owner": "custom",
                "pattern":[
                    {
                        "regexp": "^([^\\s].*)$",
                        "file": 1,
                        "location": 2,
                        "message": 3
                    }
                ],
                "background": {
                    "activeOnStart": true,
                    "beginsPattern":"^Serving HTTP (.*)$",
                    "endsPattern":"^Keyboard interrupt received, exiting(.*)$"
                }
            }            
        }
    ]
}

【问题讨论】:

    标签: visual-studio-code vscode-tasks


    【解决方案1】:

    所以我们也遇到了类似的问题:想在 Docker 内运行的 Django 应用程序上设置一个调试器。在我的设置中,调试器启动了一个preLaunchTask,它启动了远程解释器调试器,除此之外(比如安装ptvsd

    原始步骤:

    1. preLaunchTask 调用脚本 (./run-debug.sh)。
    2. 此脚本使用以下命令调用远程调试器: docker container exec -it my_app python debug.py runserver --noreload --nothreading 0.0.0.0:8000
    3. debug.py 文件中,有一条打印语句可以知道调试器已启动。

    这不起作用,显然,VSCode 没有捕获调试器的输出。相反,在run-debug.sh 文件中,我添加了一个回显语句:Starting debugger session: VSCode 捕获了 ^_^。这解决了我的问题。

    tasks.json,相关问题匹配器:

    "problemMatcher": {
                    "pattern": [
                        {
                          "regexp": ".",
                          "file": 1,
                          "location": 2,
                          "message": 3
                        }
                      ],
                      "background": {
                        "beginsPattern": "^Starting debugger session:",
                        "endsPattern": ".",
                    }
                }
    

    run-debug.sh 脚本,相关部分:

    # Start remote process
    echo 'Starting debugger session:' #VSCode beginsPattern will catch this!
    docker container exec -it my_app python debug.py runserver --noreload --nothreading 0.0.0.0:8000
    

    【讨论】:

      猜你喜欢
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多