Search code examples
cvisual-studio-codevscode-debugger

Constructing a program-string for debugging C-code in/for launch.json


I want to use vscode for debugging c-code on ubuntu. My buildsystem creates executable-files at a location that is pretty much constructed from the location and name of the .c file (e.g. source/modules/module1/test/aspect_test.c is built into build/modules/module1/test/module1_aspect_test).

I want to create a launch.json that runs the executable depending on the currently opened file.

My first thought was to create a task in task.json that sources a bash-script, which constructs the name and exports it a as environment-variable. I'm using this as an preLaunchTask. I thought i can use the set environment variable for the program, but the variable is empty when used in launch.json with:

"program": "${env:execName}",

My next idea was to use inputs to call the script and use the output.

"inputs": [
    {
      "id": "execName",
      "type": "command",
      "command": "shellCommand.execute",
      "args": {
         "command": "${workspaceFolder}/.vscode/make_program_name.sh ${fileDirname}/${fileBasename}"
      }
    }
],

But here i run into the problem, that ${fileDirname}/${fileBasename} seem to be unset in the inputs.

The last way i tried is to use a file that is written by the script as preLaunchTask and read in inputs:

"inputs": [
    {
        "id": "execName",
        "type": "command",
        "command": "extension.commandvariable.file.content",
        "args": {
        "fileName": "/tmp/execName.txt"
        }
    }
],

which works, but i don't really like it.

Is there a better/simpler way to do this?


Solution

  • you can use the transform command from the Command Variable extension

    "inputs": [
        {
            "id": "execName",
            "type": "command",
            "command": "extension.commandvariable.transform",
            "args": {
              "text": "${relativeFileDirname}/${fileBasenameNoExtension}"
              "find": "^source",
              "replace": "build"
            }
        }
    ]
    

    If you want variable subst in the inputs part you can use

    "inputs": [
        {
            "id": "execName",
            "type": "command",
            "command": "extension.commandvariable.transform",
            "args": {
              "text": "${command:execName}"
              "command": {
                "execName": {
                  "variableSubstArgs": true,
                  "command": "shellCommand.execute",
                  "args": {
                    "command": "${workspaceFolder}/.vscode/make_program_name.sh ${fileDirname}/${fileBasename}"
                  }
                }
              }
            }
        }
    ]