Why aren't my Visual Studio Code task executing? - windows

I am struggling with getting my Visual Studios code task to work. The problem is that it seems like the task/.bat file is not being executed.
VS Code task configuration:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "My Label",
"type": "shell",
"windows": {
"command": "'c:\\Program Files (x86)\\path\\to\\the\\file.bat${file}'"
},
"presentation": {
"reveal": "always"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
For testing purposes the file.bat contains:
echo "------"
echo %1
echo "------"
The output in Visual Studio Code terminal is:
> Executing task in folder User: 'c:\\Program Files (x86)\\path\\to\\the\\file.bat c:\project\file.abc' <
c:\\Program Files (x86)\\path\\to\\the\\file.bat c:\project\file.abc
Terminal will be reused by tasks, press any key to close it.
I expected that the ${file} argument/value to printed in the console. The problem is that nothing is printed. It doesn't matter if I intentionally make syntax errors in the bat file. From VS Code it seems like the .bat file is not executed at all.
The shell being used is PowerShell
Configuration:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
Thank you in advance!
T

You just have a slight syntax error using double quotes and single quotes together.
Change to:
...
"windows": {
"command": "c:\\Program Files (x86)\\path\\to\\the\\file.bat ${file}"
}
...

Related

Custom buildscript for vscode extension

I am trying to build an extension for vscode for a specific file format.
Now I want to run a custom build script (a batch file that is placed inside the custom extension) that gets exectued when the user presses ctrl+shift+B
I successfully created such a behaviour in a sublime3 extension where the sublime-build script looks like that:
{
"working_dir": "${project_path:${folder:${file_path}}}",
"shell": true,
"osx":
{
"cmd": ["cd '$packages/myextension/buildscript/' && ./mybuildscript.sh $file"]
},
"windows":
{
"shell_cmd": "cd '$packages/myextension/buildscript/' && ./mybuildscript.bat $file",
},
"linux":
{
"cmd": ["cd '$packages/myextension/buildscript/' && ./mybuildscript.sh $file"]
}
}
Can you provide an example how something similar can be done in the vscode extension?

Running vscode task that includes which fails

I have a task created that runs some unittests via bash_unit.
The bash unit script seems to fail based on its use of which.
If I replace:
CAT="$(which cat)"
and other which referances in bash unit to point to my local commands all runs great.
If I run bash)unit directly all good, but if i run it as a vscode task it fails.
I have simplified the task below to the minimum failure:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "run which",
"type": "shell",
"command": "/usr/bin/which which",
"problemMatcher": [],
"group": "test",
}
]
}
This produces the following:
The terminal process "/bin/bash '-c', '/usr/bin/which which'" failed to launch (exit code: 1).
Any ideas what is happening?

How to create a directory in Visual Studio Code tasks.json in windows?

I'm trying to write a task to create a directory using Visual studio Code tasks (in tasks.json) for windows users using the mkdir command, it's working well except when the folder already exists.
tasks.json
{
"label": "(release) create build directory",
"type": "shell",
"linux": {
"command": "mkdir -p ./build/release"
},
"windows": {
"command": "mkdir .\\build\\release", // Not working when folder already exists !
}
},
What I tried:
"command": "IF NOT EXIST .\\build\\release mkdir .\\build\\release"
But then I get the error:
At line:1 char:3
+ IF NOT EXIST .\build\release mkdir .\build\release
+ ~
Missing '(' after 'IF' in if statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingOpenParenthesisInIfStatement
If it is not possible to do it this way, is it possible to run this task by ignoring the exit code ? ( so that the tasks continue to build my project)
Environment:
Visual Studio code 1.40.2
Windows 10 Pro x64
I found a way of doing it by using cmd.exe with /C option (Run Command and then terminate)
{
"label": "(release) create build directory",
"type": "shell",
"linux": {
"command": "mkdir -p ./build/release"
},
"windows": {
"command": "cmd",
"args": ["/C", "if not exist .\\build\\release mkdir .\\build\\release"]
}
},

Debug powershell in Visual studio code with parameters from command line

Using Powershell ISE I often set breakpoints in a script and start the script from command line. ISE then stops at the breakpoint and lets me debug from there. How do I do the same from Terminal in Visual Studio Code? Below is a script just to show you what I mean. Starting from a terminal I would write:
.\hello.ps1 -firstName "firstName" -lastName "theLastName"
But doing that from terminal it just starts a new window.
param(
[string]$firstName,
[string]$lastName
)
Write-Host "Starting test script"
try
{
Write-Host "Hello $firstName $lastName"
}
catch
{
Write-Error "Error. Exception: $_"
}
write-host "Script done"
To make the info from Eickhel Mendoza's link explicit - the solution is to set up a Run and Debug config file. This is created in .vscode/launch.json. The content for this case would be:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Hello Script",
"type": "PowerShell",
"request": "launch",
"script": "${workspaceFolder}\\hello.ps1",
"cwd": "${workspaceFolder}",
"args": ["-firstName \"firstName\" -lastName \"theLastName\""]
}
]
}
Then just open the "Run and Debug" sidebar pane on VSCode (the play button with the bug or Ctrl+Shift+D), where you can run your launch script and any breakpoints you set will be hit as expected.
Maybe this might help... it involves modifying the launch.json file in your workspace.
https://github.com/PowerShell/vscode-powershell/tree/master/examples#passing-arguments-to-the-script

Visual Studio Code Task Argument

I'm trying to create some tasks in Visual Studio Code to run all the tests in my go project.
I usually execute the tests on the command line using:
go test ./...
In Visual Studio Code my tasks.json looks like this:
{
"version": "0.1.0",
"command": "go",
"tasks": [
{
"taskName": "build",
"isBuildCommand": true
},
{
"taskName": "test",
"isTestCommand": true,
"args": ["./..."]
}
]
}
So Build works fine (CTRL + SHIFT + B)
But when I try to run the tests (CTRL + SHIFT + T) the following error occurs:
go: unknown subcommand "./..."
It seems to be omitting the "test" param, but when I comment out the args it runs go test fine.
Any ideas?
THIS MAY BE A BUG
VSCode Reverse Args and Task as of v0.8.0
This may be a bug that still persists in the newer versions. As of v0.9.1 I have not had a chance to test. Prior to 0.9.1 at least one hack worked by reversing the task and it's arg as in the following example:
{
"version": "0.1.0",
"command": "go",
"tasks": [
{
"taskName": "build",
"isBuildCommand": true
},
{
"taskName": "./...",
"isTestCommand": true,
"args": ["test"]
}
]
}
It's hard to believe that this has still persisted until v0.8.0 so there may be a preferred solution that I have not discovered.
Here is a link to a prior post that deals with a similar issue:
Define multiple tasks in VSCode
Scroll down to my answer for more explanation.

Resources