Setting environment variable in VS Code debugger

How to set environment variables in VS Code debugger in launch.json for Julia? For Python, the env var can be set by the “env” property:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {"MYVAR": "value"}
        },
    ]
}

Then the env var “MYVAR” is added to the current debugging session. However, when I try to do the same for Julia, I get the error “Property env is not allowed.” Instead intellisense suggest a property called “juliaEnv” (Absolute path to the Julia environment), which seems to be an entirely different thing. What is the correct way to do this?

Unfortunately not yet implemented, see here.

Is there any (relatively simple) workaround?

Can’t really think of any… I don’t think it would be difficult to properly add to the extension, if someone wanted to give it a go.

I think it should be enough to add support for env here (ideally copy that from how another language does it), and then make sure that the values from args.env get added here and here.

The Python extension has this in its package.json, which can be directly copied to Julia; it not only supports “env”, but also “envFile” which allows users to input the env variables from a file. But unfortunately I am not familiar with the making of a VS Code extension, so may not be able to help make a pull request…

One workaround would be to set the environment variables and start Julia in a regular VSCode terminal session, then connect to that session (via the “Connect external REPL” command).

I am thinking another workaround by using the preLaunchTask in launch.json. I add the following task to the task.json file:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "set_envvar",
            "type": "shell",
            "command": "export MYVAR=\"value\""
        },
    ]
}

Then I add "preLaunchTask": "set_envvar" to the launch.json. But it does not seem to work. I get the following output in VS Code terminal:

 *  Executing task: export MYVAR="value"

 *  Terminal will be reused by tasks, press any key to close it. 

 *  Executing task: # my debug task

So I guess that the terminal forgets the first task after the pre-launch task finished? Is it actually OK to set envvars in this way?

That won’t work. We spawn a new Julia process in the extension, and we (the extension) control what env vars are passed to it, so we’ll have to just implement that.

Has there been any progress on this?

I’m in the process of rewriting much of the debugger integration, and as part of that I’ll fix this.

4 Likes

Just FYI, the preview version of the extension now supports this.

4 Likes

when you write “supports this” you mean that you can now change environment variables? if so, will you please elaborate a bit on how can one do that?

Take a look at Debugging in Visual Studio Code, and in particular the env attribute which we now support.

3 Likes