VS Code: pass environment variables to build task - visual-studio-code

How can I pass environment variables to my build task? According to what I've found at many places, this can be specified in the tasks.json. However, I tried without success:
tasks.json:
{
"version": "2.0.0",
"options": {
"env": {
"CXX": "/usr/local/opt/llvm/bin/clang++",
"LDFLAGS": "-L/usr/local/opt/llvm/lib/c++ -Wl,-rpath,/usr/local/opt/llvm/lib/c++ -L/usr/local/opt/libomp/lib",
"CXXFLAGS": "-I/usr/local/opt/libomp/include"
},
"shell": {"executable": "/bin/zsh"}
},
"tasks": [
{
"type": "cppbuild",
"label": "make test",
"command": "gmake",
"args": [
"-f",
"Test.Makefile"
],
"options": {
"cwd": "${workspaceFolder}"
}
}
}
}
Test.Makefile:
$(info $(LDFLAGS))
$(info $(CXXFLAGS))
When I launch this task, the makefile prints no values for the variables. In my usual terminal (where I have exported these variables in my .zshrc) the variables are seen by gmake. What am I doing wrong?

Related

How to use a variable for a task command in Visual Studio Code?

I have numerous tasks in my Visual studio workspace, and I do not want to repeat the program full path in all the tasks.
I read the documentation at https://code.visualstudio.com/docs/editor/variables-reference, I don't need input variable, command variable and environment variable seams to fit to my need, but it does not work.
In my project.code-workspace file:
"tasks": {
"options": {
"env": { "longComandPath": "c:/This/is/a/long/command/that/points/to/the/program.exe" }
},
"tasks": [
{
"label": "LibFM_Lite-Regen",
"type": "shell",
"group": "build",
"command": "${env:longComandPath}",
"args": [ "-verbose" ],
"options": {
"cwd": "X",
}
}
...
]
}
The result is:
> Executing task in folder X: -verbose
-verbose is not recognized as an applet command
So "${env:longComandPath}" is evaluated as an empty string
I use Visual Studio Code 1.6.3.
Place your options object outside the tasks object:
"options": {
"env": { "longComandPath": "c:/This/is/a/long/command/that/points/to/the/program.exe" }
},
tasks:{
"command": "${env:longComandPath}",
}
Then reference it inside the tasks object.

How to set command argument in vscode task.json?

I would like to automate extension setting with some arguments on task runner in VSCode.
How can I set arguments:
string argument
menu selection
I tried something like :
{
"version": "2.0.0",
"tasks": [
{
"label": "spark setting",
"command": "${command:hdinsight.linkCluster}",
},
}
I tried applying "args" section :
{
"version": "2.0.0",
"tasks": [
{
"label": "spark setting",
"command": "${command:hdinsight.linkCluster}",
"args": ["Generic Livy Endpoint", "htttp://*****"]
},
}
But it seems doesn't work to automate.
The extension command 'linkCluster' requires both menu selection and string argument which is URI.
I haven't found a way to specify the args if you use the command variables directly in the task definition, but inputs (despite the name) allow commands with arguments:
tasks": [
{
"label": "mytask",
"command": "${input:first}",
}
],
"inputs": [
{
"id": "first",
"type": "command",
"command": "hdinsight.linkCluster",
"args": ["Generic Livy Endpoint", "htttp://*****"]
]

Tasks in VScode

Is it possible to run a task from a task in VSCode?
Example json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "Show In Chrome",
"windows": {
"command": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
},
"osx": {
"command": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
},
"args": [
"${file}"
],
"type": "process",
},
{
"taskName": "Show Coverage",
// this is the task to call another task
}
]
}
What i would like to do is have the Show Coverage task look for a file in the coverage folder and then call Show In Chrome task to show it as the arguments of that file being passed. Is this possible?
It turns out the current config from the docs website isn't updated for tasks.
However if you want a task to call other tasks because it depends on them you can simply add this configuration
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "Show In Chrome",
"identifier": "showInChrome",
"windows": {
"command": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
},
"osx": {
"command": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
},
"args": [
"${file}"
],
"type": "process",
},
{
"taskName": "Show Coverage",
"dependsOn": "showInChrome"
}
]
}
this will cause the task to run the previous task and then run whatever commands it has set as well.
Note: you can use multiple depends by using an array of identifiers.

Is it possible to pass arguments to a task in Visual Studio Code

Here's an example of my tasks.json:
{
"version": "0.1.0",
"tasks": [
{
"taskName": "test",
"suppressTaskName": true,
"command": "python",
"args": [
"tests/brewer_tests.py"
],
"isTestCommand": true
}
]
}
I can run this with shift+cmd+alt+b. I can also run it with alt+t, and choose it from the menu. Is it possible to pass additional arguments in that menu? e.g.
And you could build it into your task like so:
{
"version": "0.1.0",
"tasks": [
{
"taskName": "test",
"suppressTaskName": true,
"command": "python",
"args": [
"tests/brewer_tests.py",
$arg1 # would resolve to "ARG1"
],
"isTestCommand": true
}
]
}
Or something similar?
I used the solution from this answer until now, but since Visual Studio Code has now an official support for task prompts I will add it as an answer here.
In your tasks.json file, you add the key inputs next to your tasks. This key contains an array with all possible parameters. Note that not every task has to use all of these inputs.
All of these inputs have an id, which you will use to reference the input in your task.
Now, in the task you only need to add ${input:myInputId} whereever you need the parameter.
Example:
{
"version": "2.0.0",
"tasks": [
{
"label": "Echo param",
"type": "shell",
"command": "echo ${input:param1}",
"problemMatcher": []
},
{
"label": "Echo without param",
"type": "shell",
"command": "echo Hello",
"problemMatcher": []
},
],
"inputs": [
{
"id": "param1",
"description": "Param1:",
"default": "Hello",
"type": "promptString"
},
]
}
The task Echo param will open a prompt, which lets you input a string value and it will then print this value. The task Echo without param will simply print "Hello".
Here's what is working for me for now - using this to run a golang snippet with custom arguments.
If you add a keyboard mapping to this, the process is very straightforward.
So far tested this only under Windows - linux version is commented out for that reason
{
"label": "runwithargs",
"type": "shell",
"windows": {
"options": {
"shell": {
"executable": "powershell.exe",
"args": [
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command"
]
}
},
"command": "",
"args": [
{ "value": "$cmdargs = read-host 'Enter command line arguments';", "quoting": "weak"},
{ "value": "go run ${file} $cmdargs", "quoting": "weak"}
]
},
/*"linux": {
"command": "echo 'Enter command line arguments: '; read cmdargs;",
"args": [ "go run ${file} $cmdargs" ]
},*/
"presentation": {
"panel": "dedicated",
"focus": true
}
}
Regarding Input variables, VSCode 1.43 (Feb. 2020) adds a new feature:
promptString Password Input
The "promptString" "input" type can have "password": true, which will cause the quick input that shows to obscure the typed content like a password.

How to chain tasks in Visual Studio Code using only tasks.json?

I have been ploughing through the documentation of Visual Studio Code to figure out how to add multiple consecutive tasks to the tasks.json file.
The tasks array only allows for creating different arguments to the same command. In this example the command is echo.
{
"version": "0.1.0",
"command": "echo",
"isShellCommand": true,
"args": [],
"showOutput": "always",
"echoCommand": true,
"suppressTaskName": true,
"tasks": [
{
"taskName": "hello",
"args": ["Hello World"]
},
{
"taskName": "bye",
"args": ["Good Bye"]
}
]
}
Does tasks.json allow several tasks to be executed consecutively? For example, tsc followed by uglify?
The dependsOn feature was shipped in version 1.10.0. For example, I am using this to compile and run single file scripts in TypeScript:
{
"version": "2.0.0",
"tasks": [
{
"command": "tsc -p ${cwd}/2017-play",
"label": "tsc-compile",
"type": "shell"
},
{
"command": "node ${cwd}/2017-play/build/${fileBasenameNoExtension}.js",
"label": "node-exec",
"type": "shell",
"dependsOn": [
"tsc-compile"
],
"problemMatcher": []
}
]
}
Here is a working example that runs the tcs build and copies the source to another folder using a shell script.
This is based on various posts on StackOverflow and the documentation found here:
https://code.visualstudio.com/updates/v1_10#_more-work-on-terminal-runner
One could also make a tasks.json with two tasks with the second having a dependsOn on the first one as shown in Ben Creasy post, the two tasks would get executed when the second one is called. I needed to be able to execute one, the other or both. Many thanks to Ben, I had a hard time finding a solution before hitting this post.
BTW, when including a shell file, the commands are run with reference to the project folder, not the one where the script is located.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": "build",
"identifier": "build"
},
{
"label": "Copy files",
"type": "shell",
"command": "./scripts/copysrc.sh",
"windows": {
"command": ".\\scripts\\copysrc.cmd"
},
"group": "build",
"presentation": {
"reveal": "always"
},
"problemMatcher": [],
"dependsOn": "build"
},
{
"label": "Build and copy",
"dependsOn": [
"build",
"Copy files"
],
"group": "build",
"problemMatcher": []
}
]
}