5

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.

4 Answers 4

4

Environment variables set in tasks.json aren't picked up by ${env:..} in tasks.json, nor will they be substituted when given as arguments to the shell. You can however add custom settings in settings.json, and reference those in tasks.json using ${config:...}.

e.g. settings.json:

{ "APP_NAME": "myApp", } 

tasks.json:

{ "version": "2.0.0", "type": "shell", "options": { "env": { "APP_NAME": "${config:APP_NAME}" } }, "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 ${config:APP_NAME}", "group": { "kind": "build", "isDefault": true } } ] } 
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

1

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.

Comments

0

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

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

1 Comment

Thank you for the fast reply. Unfortunatly I already tried this and this doesn't work either (at least in powershell)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.