Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set secret environment variables in launch.json of VSCode for debugging?

I'm using VSCode debugger and and setting environment variables via the env property. I also have database password and secrets that I need to set as env vars but I'd like to check in launch.json so the debugging settings can be shared amongst the team etc.

Is there a way I can set these sensitive environment variables for debugging without actually checking it into source control?

like image 529
n00b Avatar asked Feb 27 '19 23:02

n00b


People also ask

How do I set specific environment variables when debugging in Visual Studio?

Go to Project -> Properties... Under Configuration Properties -> Debugging, edit the 'Environment' value to set environment variables. For example, if you want to add the directory "c:\foo\bin" to the path when debugging your application, set the 'Environment' value to "PATH=%PATH%;c:\foo\bin".

How do I change the debugging environment in VS Code?

Once you have your launch configuration set, start your debug session with F5. Alternatively, you can run your configuration through the Command Palette (Ctrl+Shift+P) by filtering on Debug: Select and Start Debugging or typing 'debug ' and selecting the configuration you want to debug.

How do I set an environment variable in debug?

From the main menu, select Run | Edit Configurations or choose Edit Configurations from the run/debug configurations selector on the toolbar. In the Run/Debug Configurations dialog, select a configuration you want to add the environment variables to. Type the variable name and value: <name>=<value> .

How do I change VS Code environment variables?

Click the Advanced system settings link. Click Environment Variables. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Reopen Command prompt window, and run your java code.


1 Answers

There are a couple of ways to reference sensitive data from Visual Studio Code (vscode) inside launch.json without including the data in the launch.json file.

  • Environment Variables
  • Input Variables : Prompt
  • Input Variables : Custom Command

The vscode Variables Reference documents these solutions nicely.

Environment Variables (docs)

You can put your sensitive variables in an environment variable (perhaps loaded via your shell profile such as in .bash_profile for example). You can then reference it "through the ${env:Name} syntax (for example, ${env:USERNAME})."

{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "program": "${workspaceFolder}/app.js",
  "cwd": "${workspaceFolder}",
  "args": ["${env:USERNAME}"]
}

Input Variables (docs)

Prompt for Input

You can prompt for sensitive information on launch by defining a promptString input type as seen in the below configuration extracted from the docs. Below ${input:componentName} is used as an argument and defined below in an inputs section as a promptString for prompting you for the value when you run the associated task.

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "ng g",
      "type": "shell",
      "command": "ng",
      "args": ["g", "${input:componentType}", "${input:componentName}"]
    }
  ],
  "inputs": [
    /** skipping componentType definition for brevity -- see docs for that */
    {
      "type": "promptString",
      "id": "componentName",
      "description": "Name your component.",
      "default": "my-new-component"
    }
  ]
}

The definition of a promptString is as follows:

  • description: Shown in the quick input, provides context for the input.
  • default: Default value that will be used if the user doesn't enter something else.
  • password: Set to true to input with a password prompt that will not show the typed value.

Run a Custom Command for the Input

You can also run a custom command for getting the input. The docs use the following configuration as an example. Note that like other input types that the command must be defined in the inputs section with a type.

{
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Run specific test",
      "program": "${workspaceFolder}/${input:pickTest}"
    }
  ],
  "inputs": [
    {
      "id": "pickTest",
      "type": "command",
      "command": "extension.mochaSupport.testPicker",
      "args": {
        "testFolder": "/out/tests"
      }
    }
  ]
}

The other options for the command type are:

  • command: Command being run on variable interpolation.
  • args: Optional option bag passed to the command's implementation.
like image 110
Sam Avatar answered Oct 12 '22 13:10

Sam