Skip to content

Instantly share code, notes, and snippets.

@Iristyle
Last active March 21, 2022 23:16
Show Gist options
  • Save Iristyle/08b7214dd16656a964e885d9ec8696ed to your computer and use it in GitHub Desktop.
Save Iristyle/08b7214dd16656a964e885d9ec8696ed to your computer and use it in GitHub Desktop.
Configuring VSCode to add ENV vars when using WSL

Assuming a file like .env in the local workspace folder that looks like

FOO=value
BAR=value2

Add the following to the VSCode settings.json

This is necessary to support the "debug test" label showing above a test in a Go XXX_test.go file

    "go.testEnvFile": "${workspaceFolder}/.env",
    "go.testFlags": ["-v"],
    "go.delveConfig": {
        "showGlobalVariables": true,
        "showLog": true
    }

To change the default debugger behavior when launching via the green play button in the "Run and Debug" dropdown in VSCode, add a new .vscode/launch.json config to the workspace that extends the default to allow using the same .env file:

{
    // 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": "Run The Jewels",
            "showGlobalVariables": true,
            "showLog" : true,
            "envFile": "${workspaceFolder}/.env"
        }
    ]
}

All of the possible launch.json settings are described in detail at https://github.com/golang/vscode-go/blob/master/docs/debugging.md#launchjson-attributes - there are a number of additional flags that may be passed to Delve. Even though the envFile setting is supposed to default to "${workspaceFolder}/.env", I found this to not actually be the case. That might be a bug in the VSCode addin (filed 2128 for version v0.32.0 at https://github.com/golang/vscode-go)

Don't forget to update the projects .gitignore to exclude an .env file which typically contains secrets

## For VSCode env vars used by tests
.vscode/*
!.vscode/launch.json
.env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment