How to link directories for VS Code problem matcher

Viewed 2069

I am writing a new build task for compiling C++ in VS Code. The task involves compiling the code inside of a Docker container. For example

docker exec -it my_container make

Here is what I have in my task.json file

{
    "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. That is because the output for an looks something like this:

/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. 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. Instead, the file lives in /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 (e.g. convert /host/my_project/src/my_file.cpp to /src/my_file.cpp, and set the fileLocation to 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. This also did not work, and still tried to open the file in the Docker container

Does anyone have any suggestions?

1 Answers

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:

The slashes must be escaped, so you have to add \/host\/ after the beginning of that regex.

Try this config, I can run it as a task (control-shift-B) and generate problems with it (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.

{
    // 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.

Related