14

I tried to set environment variables for my Visual Studio Code tasks that will run in my Windows Subsystem Linux. However, it does not seem to work. Here is my tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test env",
            "type": "shell",
            "command": "echo",
            "args": [
                "$test"
            ],
            "options": {
                "env": {
                    "test": "test_string"
                }
            }

        },
    ]
}

The output is:

> Executing task in folder ex12-test: echo $test <



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

Note that by default shell has been manually modified to C:\WINDOWS\SysNative\bash.exefor WSL, as recommended here and here.

Yuxiang Wang
  • 8,095
  • 13
  • 64
  • 95
  • 1
    See [environment variable not found](https://stackoverflow.com/questions/52963423/vscode-environment-variable-in-task-not-found) In your case you need to use `$env:test` – Mark Jan 17 '19 at 03:57
  • @Mark Thank you! I changed to ${env:test} and it still wouldn't work. It seems like the environment variable wasn't set at all. – Yuxiang Wang Jan 18 '19 at 06:29
  • 1
    @YuxiangWang the answer is [here](https://stackoverflow.com/questions/52963423/vscode-environment-variable-in-task-not-found/56887403#56887403). You simply cannot reference environment variables set in this manner in the `args` or `command` of your tasks.json task definition. – njappboy Jul 04 '19 at 11:58

2 Answers2

17

The options object goes outside of any task, so:

format
  "version": "2.0.0",
  "options": {
      "env": {
        "test": "test_string"
      }
   }
   "tasks": [
    {
        "label": "test env",
        "type": "shell",
        "command": "echo",
        "args": [
            "$env:test"
        ],
    },

And then access the options argsuments like so:

$env:test  or    ${env:test}
]
Mark
  • 143,421
  • 24
  • 428
  • 436
  • 5
    It only worked me if I put `"$test"` instead of `"$env:test"`. – juanmah Jun 12 '20 at 12:48
  • So it is not possible to set an environment variable for a specific task only? – godo Mar 29 '22 at 12:37
  • @godo it's possible here: https://code.visualstudio.com/Docs/editor/tasks#_custom-tasks. Seems like you'd need to add an `"options"` object to the specific task – Thomas Bui May 26 '22 at 19:10
6

What I eventually did for my VS Code (v1.67.1) is:

  • Add my env variables into a settings.json
  • Use the traditional %ENV_VAR% option.

So in the settings.json, create this:

{
  "terminal.integrated.env.windows": { // switch to your os, check `Open Workspace Settings` in the Command Pallete then search for `environment`
    "AWS_REGION": "us-east-2", 
    "AWS_ID": "my_id"
  }
}

In the tasks.json, use the proper ENV variable reference:

"command": "echo %AWS_REGION%"

Thomas Bui
  • 188
  • 3
  • 10
  • 1
    In tasks.json, worked me only this: `"command": "echo ${env:AWS_REGION}"` It depends on used shell. – Mattia72 Oct 07 '22 at 08:37