0

I have the following in my workspace settings.json file:

"terminal.integrated.env.osx": {
    "AUTH_TOKEN": "secret_XXXXXX"
}

However, when trying to pass this via a launch command (defined in launch.json):

    {
        "name": "Example: Query",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/examples/query.py",
        "args": [ "${env:AUTH_TOKEN}" ]
    }

The resulting command contains an empty string for the argument:

/usr/bin/env /.../.venv/bin/python /.../debugpy/launcher 58644 -- /.../examples/query.py ""

However, if I print the variable from within the script, it is set properly.

I believe there is an ordering issue, such that the launch.json commands are generated before the terminal environment is set up - resulting in empty vars. Any ideas how to propagate the env value to the command line?

Update: I have also tried using a .env file for the variables (rather than settings.json), but the result is the same.

2 Answers 2

1

Try using "env" in launch.json...

{
          "name": "Example: Query",
          "type": "python",
          "request": "launch",
          "program": "${workspaceFolder}/examples/query.py",
          "args": ["${AUTH_TOKEN}"], // using var from env on args
          "env": {
                "AUTH_TOKEN": "XXXX",
                "ENV2" : "XXX"
          }
            
}

you can use envs from file too

{
    // ...
    "args": ["${AUTH_TOKEN}"],
    "envFile": "${workspaceFolder}/local.env",
}

3
  • Same result... The env vars are not resolved in the launch config args.
    – jheddings
    Commented Nov 11, 2021 at 3:31
  • try both... declare in "env":{"AUTH_TOKEN":"XX"}" and using on args "args":["${AUTH_TOKEN}"] ( answer has been refreshed ) Commented Nov 11, 2021 at 16:04
  • no dice... launch just escapes the variable reference. I've already guessed at many of these solutions, just looking for someone who knows of a workaround.
    – jheddings
    Commented Nov 11, 2021 at 17:19
0

You can create a .env file, and then put the variable in there, and read it from the environmental variables in the program instead of it being an argument.

1
  • Yes, but that will change the behavior of the program, which is not desired. I appreciate the pointer, but still hoping there is a way to pass via command line args, which preserves the intent of the script. Thanks!
    – jheddings
    Commented Nov 11, 2021 at 2:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.