Debugging with Bazel in Visual Studio Code with Ubuntu

Viewed 4738

I read this Bazel VS Code tutorial.

Currently, I have a problem with reproducing the tutorial on my system. I am using Ubuntu 18.04 and bazel 0.27.0 and Visual Studio Code (Version 1.35.1)

My files:

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build Example (Debug)",
            "type": "shell",
            "command": "bazel build :example -c dbg",
            "linux": {
                "command": "bazel build :example -c dbg --spawn_strategy=standalone",
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
        }
    ]
}

My launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Example",
            "type": "cppdbg",
            "request": "launch",
            "preLaunchTask": "Build Example (Debug)",
            "program": "${workspaceFolder}/bazel-out/k8-dbg/bin/example",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/bazel-out/k8-dbg/bin/example.runfiles/__main__/",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "sourceFileMap": {
                "/home/newton/.cache/bazel/_bazel_newton/7a12d285425fc99da0ce87e2a08f2f36/execroot/__main__/": "${workspaceFolder}"
            }
        }
    ]
}

The following screenshot shows in the lower left corner that an error is reported when I try to start debugging: "Unable to read file (Error: File not found..."

Error reported by VSCode

I am looking for any suggestion on how to fix this and to make it work.

2 Answers

See this.

Here's my config:

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "bazel build --compilation_mode=dbg //...",
            "group": {
                "kind": "build",
                "isDefault": true
            },
        }
    ]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "ExampleMihai",
            "preLaunchTask": "build",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/mihai/.cache/bazel/_bazel_mihai/89c1625b12241e64b746e0e58fdc7159/execroot/__main__/bazel-out/k8-dbg/bin/unit/test_static_string",
            "args": [],
            "stopAtEntry": false,
            "cwd": "/home/mihai/git/containers",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ]
        },
    ]
}

I use sourceFileMap to fix the "unable to read file" error on Linux. The following configuration uses the "official" paths in bazel-bin/:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "launch",
            "type": "cppdbg",
            "preLaunchTask": "build",
            "request": "launch",
            "program": "${workspaceFolder}/bazel-bin/example/example",
            "cwd": "${workspaceFolder}/bazel-bin/example/example.runfiles/__main__",
            "linux": {
                "sourceFileMap": {
                   "/proc/self/cwd": "${workspaceFolder}",
                }
            }
        }
    ]
}
Related