Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch.json: how to reference an environment variable

In order to define my environment variables in a single place a configured a task in which a run a shell script. The task is run as preLaunchTask in my launch.json.

In my launch.json I now try to reference the environment variables I configured in the script (like export AWS_REGION="eu-west-1").

The launch.json looks as follows:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            //..
            "env": {
                //"AWS_REGION": "us-east-1",        //works
                "AWS_REGION": "${env:AWS_REGION}",  //doesn't work, why?
            },
            "args": [],
            "preLaunchTask": "setupEnv",
        }
    ] }
like image 368
Dunken Avatar asked May 29 '19 06:05

Dunken


People also ask

How can we access the environment variable?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables.

Where do I put launch json or code?

VS Code keeps debugging configuration information in a launch.json file located in a .vscode folder in your workspace (project root folder) or in your user settings or workspace settings. To create a launch.json file, click the create a launch.json file link in the Run start view.

What is CWD launch json?

There is a launch.json file...there you should be able to set the cwd property...( cwd stands for current working directory )

What is a launch json file in VS Code?

A launch.json file is used to configure the debugger in Visual Studio Code. Visual Studio Code generates a launch.json (under a .vscode folder in your project) with almost all of the required information.


1 Answers

  • Doesn't work, why?

According to this post from user weinand...

The ".env" file is read and processed after VS Code has substituted variables in the launch config. So your debugged program will indeed see the environment variable "FOO" with the correct value but VS Code's variable substitution in the launch.json will not see it.

The reason for this is that ".env" files are a node.js concept and not a generic platform mechanism. So VS Code does not know anything about .env files, but the node.js debugger knows about .env files.

... this functionality in launch.json is specific for applications running on Node.js, although that's not what M$ explains in their documentations for VSCode.

  • Possible solution

For Python applications (possibly for other platforms as well) environment variables defined in a .env file (or whatever name you like) will be available for your application as long as the following configuration is present in launch.json...

{
    "version": "0.2.0",
    "configurations": [
        {
            [...]
            "envFile": "${workspaceFolder}/.env", // Path to the ".env" file.
            [...]
        }
    ]
}

Note that just exporting a variable...

export SOMEVAR_A=1234

... will not make the environment variable SOMEVAR_A available for the application being executed by the VSCode debugger nor for the settings - especially inside "env" and "args" ("configurations") - in launch.json as, for example, in this case...

{
    "version": "0.2.0",
    "configurations": [
        {
            [...]
            "env": {
                "SOMEVAR_A": "${env:SOMEVAR_A}"
            },
            "args": [
                "${env:SOMEVAR_A}"
            ]
            [...]
        }
    ]
}

NOTE: In our tests the ${env:SOMEVAR_A} syntax did not work in any scenario. That is, didn't work for the application ("env") and didn't work for the settings ("args") in launch.json.


PLUS I: Dirt Hack

For values present in "args" ("configurations") you can use the hack below...

{
    "version": "0.2.0",
    "configurations": [
        {
            [...]
            "envFile": "${workspaceFolder}/.env",
            "args": [
                "`source \"${workspaceFolder}/.env\";echo ${SOMEVAR_A}`"
            ]
            [...]
        }
    ]
}

... as the configuration in "envFile" doesn't work.

Notice, although, that the following construction...

[...]
"args": [
    "`echo ${SOMEVAR_A}`"
]
[...]

... would also work for "args" as long as the environment variable "SOMEVAR_A" has been previously exported in the conventional way.

The same reasoning would work for a tasks (tasks.json), but in both cases we can't guarantee that.


TIP: An .env File Example

SOMEVAR_A="abcd"
SOMEVAR_B="efgh"
SOMEVAR_C=123456

PLUS II: Export Variables

There are cases where you will need to export variables (eg. export SOMEVAR_A="abcd") so that they can be consumed by certain resources. In these cases there may be problems, because the fact that we export variables prevents (we don't know why) that they are seen in the context of the "envFile" configuration "envFile": "${workspaceFolder}/.env".

A workaround to get around these limitations is to add set -a before the variables set and set +a after it. With this we were able to meet the two scenarios as this example...

#!/usr/bin/env bash

set -a    
SOMEVAR_A="abcd"
SOMEVAR_B="efgh"
SOMEVAR_C=123456
set +a

... or in a more compatible and safe way use set -a/set +a as in this example...

[...]
"args": [
    "`set -a;source \"${workspaceFolder}/.env\";set +a;echo ${SOMEVAR_A}`"
[...]

VSCode's support for environment variables is a mess! 🙄


  • Conclusion

We don't know if the limitations we are dealing with here are from VSCode's own design or are bugs. Anyway, it doesn't seem to make much sense.

These procedures were tested on Manjaro Linux (Arch based).

Thanks! 🤗

[Ref(s).: https://unix.stackexchange.com/a/79077/61742 , https://stackoverflow.com/a/30969768/3223785 ]

like image 113
Eduardo Lucio Avatar answered Nov 15 '22 08:11

Eduardo Lucio