VSCode: Environment variable in task not found

最后都变了- 提交于 2019-12-10 10:58:01

问题


I am trying to define my own environment variables in the tasks.json in VSCode. Following every link I have found so far, I tried the following:

{
"version": "2.0.0",
"type": "shell",
"options": {
    "env": {
        "APP_NAME": "myApp"
    }
},
"problemMatcher": {
    "owner": "cpp",
    "fileLocation": [
        "relative",
        "${workspaceFolder}"
    ],
    "pattern": {
        "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
        "file": 1,
        "line": 2,
        "column": 3,
        "severity": 4,
        "message": 5
    }
},
"presentation": {
    "echo": true,
    "reveal": "always",
    "focus": false,
    "panel": "dedicated",
    "showReuseMessage": false
},
"tasks": [
    {
        "label": "Build Release",
        "command": "python ./scripts/build_app.py $APP_NAME",
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
    ]
}

I need this because we are running our build process via a python script and therefore I need to give it the name of the application I want to build. As I am having also python scripts for testing, executing, debugging and so on, I would prefer to change the app name only once in the tasks.json and not in every task itself.

According to the guidelines this should be possible in the way I did it, but in the powershell console the $APP_NAME variable is not substituted. Also neither in the cmd nor bash shell it seems to work.

I would be very grateful for any help someone could give me.


回答1:


Use$env:APP_NAME in your case. See referencing environment variables.

Environment variables

You can also reference environment variables through ${env:Name} syntax (for example, ${env:PATH}).

{
    "type": "node",
    "request": "launch",
    "name": "Launch Program",
    "program": "${workspaceFolder}/app.js",
    "cwd": "${workspaceFolder}",
    "args": [ "${env:USERNAME}" ]
}

Note: Be sure to match the environment variable name's casing, for example ${env:Path} on Windows.




回答2:


Your usage is incorrect for 1 specific reason.

Environment variables used in the tasks.json options object cannot be referenced in the tasks other attributes, specifically command in the OPs case:

options: Override the defaults for cwd (current working directory), env (environment variables), or shell (default shell). Options can be set per task but also globally or per platform. Environment variables configured here can only be referenced from within your task script or process and will not be resolved if they are part of your args, command, or other task attributes.

See options

Try removing the reference to the first argument in the ./scripts/build_app.py script as the value for $APP_NAME, and instead use os.environ['APP_NAME'] from within in the ./scripts/build_app.py script.




回答3:


If you are using powershell, use %VAR% instead of $VAR.

"command": "python ./scripts/build_app.py %APP_NAME%",


来源:https://stackoverflow.com/questions/52963423/vscode-environment-variable-in-task-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!