Search code examples
visual-studio-codevscode-debugger

How to define a global variable in launch.json in VSCode?


In the VSCode's lauch.json file, I use multiple configurations for debugging different files. And some of these variables are common between configurations. I wonder if this could be possible to define a global variable in the launch.json and use it in different locations of the file?

For example, I'm looking for a way to define variable_common once and use it in config 1 and config 2:

    "configurations": [
        {
            "name": "Python: config 1",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "args": ["variable_1", "variable_2", "variable_common"]
        },

        {
            "name": "Python: config 2",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "args": ["variable_3", "variable_4", "variable_common"]
        }
]

Solution

  • With the extension Command Variable you can create custom variables

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Python: config 1",
          "type": "python",
          "request": "launch",
          "program": "${file}",
          "console": "integratedTerminal",
          "justMyCode": true,
          "args": ["variable_1", "variable_2", "${input:VAR_COMMON}"]
        },
        {
          "name": "Python: config 2",
          "type": "python",
          "request": "launch",
          "program": "${file}",
          "console": "integratedTerminal",
          "justMyCode": true,
          "args": ["variable_3", "variable_4", "${input:VAR_COMMON}"]
        }
      ],
      "inputs": [
        {
          "id": "VAR_COMMON",
          "type": "command",
          "command": "extension.commandvariable.transform",
          "args": { "text": "common_arg" }
        }
      ]
    }