c# - 如何在 vscode 中访问 tasks.json 中的 .env 变量?

标签 c# visual-studio-code environment-variables vscode-tasks

我试图在我的 tasks.json 中的任务中使用环境变量vscode 中的 C# 项目文件.

在我的 launch.json文件我有这个代码来解析一个 .env 文件:

"configurations": [
  {
    ...
    "envFile": "${workspaceFolder}/.env",
  }
]

然后我在 tasks.json归档此任务:
{
  "label": "login",
  "command": "sh",
  "type": "shell",
  "args": [
    "${workspaceFolder}/etc/login.sh",
    "${env:USERNAME}",
    "${env:PASSWORD}"
  ]
}

这似乎是从 https://code.visualstudio.com/docs/editor/tasks 隐含的代码,但是(通过在另一个任务中通过回显进行测试)我发现了最后两个 args为空白。上网查了一下,我想我找到了原因,configurations..envtasks 使用自己,而不是被 task.json 访问运行,因此无法访问。

我如何创建(使用)这些 env variablestasks.json ?

最佳答案

结帐 https://code.visualstudio.com/docs/editor/tasks-appendix

  /**
   * The environment of the executed program or shell. If omitted
   * the parent process' environment is used.
   */
  env?: { [key: string]: string };
例子
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "diagnostic",
            "type": "shell",
            "options": {
                "cwd": "${workspaceFolder}/beam",
                "env": {
                    "FOO": "baz",
                }
            },
            "command": "printenv",
            "problemMatcher": []
        }
    ]
}
我不知道从文件中提取这些文件,例如 launch.json
我会在 vscode repo 上打开一个问题

关于c# - 如何在 vscode 中访问 tasks.json 中的 .env 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58009115/

相关文章:

preprocessor - 如何根据默认的 Inno Setup 变量设置 ISPP 定义?

javascript - 在 Nest.js 中定义 Node 环境

python - Microsoft Python 语言服务器无法识别 `for` 循环 `zip()`

visual-studio-code - VSCode 未在文件夹打开时运行任务

amazon-ec2 - 未在给定路径中创建环境变量

c# - 使用 nunit 测试测试内部函数的工作

gulp - 无法在 Visual Studio Code 中进行自动 Markdown 编译

c# - 如何检查一个对象是否是列表枚举类型的对象?

c# - 如何从 MSBuild 调用 Path.Combine?

c# - 从 IDataReader 获取值的 Null 安全方法