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?
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".
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.
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> .
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.
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.
The vscode Variables Reference documents these solutions nicely.
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}"]
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With