【问题标题】:Set environment variables in task.json in vscode for WSL在 vscode for WSL 中的 task.json 中设置环境变量
【发布时间】:2018-10-23 16:15:14
【问题描述】:

我尝试为将在我的 Windows 子系统 Linux 中运行的 Visual Studio Code 任务设置环境变量。但是,它似乎不起作用。这是我的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"
                }
            }

        },
    ]
}

输出是:

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



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

请注意,默认情况下,shell 已手动修改为 C:\WINDOWS\SysNative\bash.exefor WSL,推荐使用 herehere

【问题讨论】:

  • environment variable not found在你的情况下你需要使用$env:test
  • @Mark 谢谢!我更改为 ${env:test} 仍然无法正常工作。似乎根本没有设置环境变量。
  • @YuxiangWang 答案是here。您根本无法在 tasks.json 任务定义的 argscommand 中引用以这种方式设置的环境变量。

标签: visual-studio-code vscode-settings windows-subsystem-for-linux vscode-tasks


【解决方案1】:

选项对象超出任何任务,所以:

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

然后像这样访问选项参数:

$env:test  or    ${env:test}
]

【讨论】:

  • 只有在我输入 "$test" 而不是 "$env:test" 时才有效。
猜你喜欢
  • 1970-01-01
  • 2020-02-24
  • 2018-09-20
  • 2020-11-10
  • 2017-02-17
  • 2019-05-17
  • 1970-01-01
  • 2023-01-24
  • 1970-01-01
相关资源
最近更新 更多