简体   繁体   English

如何链接 VS Code 问题匹配器的目录

[英]How to link directories for VS Code problem matcher

I am writing a new build task for compiling C++ in VS Code.我正在编写一个新的构建任务,用于在 VS Code 中编译 C++。 The task involves compiling the code inside of a Docker container.该任务涉及在 Docker 容器内编译代码。 For example例如

docker exec -it my_container make

Here is what I have in my task.json file这是我在task.json文件中的内容

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "docker",
            "args": [
                "exec",
                "-it",
                "my_container",
                "make"
            ],
            "group": "build",
            "presentation": {
                "reveal": "always",
                "panel": "dedicated"
            },
            "options": {
                "cwd": "${workspaceRoot}"
            },
            "problemMatcher": {
                "base": "$gcc",
                "fileLocation": "absolute"
            }
        }
    ]
}

I'm able to run the task, and everything compiles correctly.我能够运行任务,并且一切都正确编译。 However, VS Code cannot find the files that have build errors in them.但是,VS Code 无法找到其中包含构建错误的文件。 That is because the output for an looks something like this:那是因为 an 的输出看起来像这样:

/host/my_project/src/my_file.cpp:105:46: error: passing 'const SomeClass' as 'this' argument discards qualifiers [-fpermissive]

The path that is listed is the absolute path to the file in the Docker container.列出的路径是 Docker 容器中文件的绝对路径。 When you click on one of the files in the Problems tab, it tries to jump to /host/my_project/src/my_file.cpp , but it doesn't exist.当您单击问题选项卡中的文件之一时,它会尝试跳转到/host/my_project/src/my_file.cpp ,但它不存在。 Instead, the file lives in /home/me/projects/my_project/src/my_file.cpp .相反,该文件位于/home/me/projects/my_project/src/my_file.cpp

I've tried a few things to fix this, none of which seem to work.我已经尝试了一些方法来解决这个问题,但似乎都不起作用。 I tried changing the problemMatcher to the one outlined in the documentation and trying to use a different regex that would remove the absolute part of the path (eg convert /host/my_project/src/my_file.cpp to /src/my_file.cpp , and set the fileLocation to relative ).我尝试将problemMatcher更改为文档中概述的那个,并尝试使用不同的正则表达式来删除路径的绝对部分(例如将/host/my_project/src/my_file.cpp转换为/src/my_file.cpp ,以及将 fileLocation 设置为relative )。 However I'm not well-versed enough in regex to get it right.但是,我对正则表达式不够精通,无法正确使用。 Referencing some regex from here , I came up with this这里引用一些正则表达式,我想出了这个

"problemMatcher": {
    "owner": "cpp",
    "fileLocation": ["relative", "${workspaceFolder}"],
    "pattern": {
    "regexp": "^[^/]/[^/]*/[^/]*/(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
        "file": 1,
        "line": 2,
        "column": 3,
        "severity": 4,
        "message": 5
    }
}

I also tried the other options in that answer, but none of them work.我还尝试了该答案中的其他选项,但它们都不起作用。 Another thing I tried was creating a symlink between the folders by running ln -s /host ~/projects .我尝试的另一件事是通过运行ln -s /host ~/projects在文件夹之间创建符号链接。 This also did not work, and still tried to open the file in the Docker container这也不起作用,仍然尝试在Docker容器中打开文件

Does anyone have any suggestions?有没有人有什么建议?

It looks like you just have to include the container name plus slashes: /host/ in the regex (and treat the path as relative), so here goes:看起来你只需要在正则表达式中包含容器名称加斜杠: /host/ (并将路径视为相对路径),所以这里是:

The slashes must be escaped, so you have to add \\/host\\/ after the beginning of that regex.斜杠必须被转义,因此您必须在该正则表达式的开头后添加\\/host\\/

Try this config, I can run it as a task (control-shift-B) and generate problems with it (control-shift-M).试试这个配置,我可以将它作为一个任务运行(control-shift-B)并产生问题(control-shift-M)。 However it doesn't let me control-click the errors in the console, I guess one would need to edit the c++ extension's regex for this.但是它不允许我控制单击控制台中的错误,我想需要为此编辑 c++ 扩展的正则表达式。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "echoCommand": true,
    "tasks": [
        {
            "label": "build",
            "command": "YOUR BUILD COMMAND HERE",
            "type": "shell",
            "group": "build",
            "presentation": {
                "reveal": "always",
                "panel": "dedicated"
             },
            "options": {
                "cwd": "${workspaceRoot}",
            },
            "problemMatcher": {
                "base": "gcc",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^\/host\/(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

However, the path detection in the terminal is unaffected.但是,终端中的路径检测不受影响。 I wish this regex would let us control+click the GCC messages to jump to the code.我希望这个正则表达式能让我们控制+单击 GCC 消息以跳转到代码。

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

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