(tasks.json) I can't figure out how to use escaping characters using WSL as a shell - visual-studio-code

I'm running VSCode 1.54.3 on Windows10 along with Ubuntu in WSL.
This is the task I'm trying to build.
{
"label": "Verilog: Compile iVerilog File ",
"command": "iverilog",
"type": "shell",
"args": [
"-t vvp",
"-o ${fileBasename}.vvp",
"-l /opt/Xilinx/14.7/ISE_DS/ISE/DCM_SP.v",
"-I /opt/Xilinx/14.7/ISE_DS/ISE/verilog/src/unisims",
"-I /opt/Xilinx/14.7/ISE_DS/ISE/verilog/src/XilinxCoreLib/",
"wslpath ${workspaceFolder}${pathSeparator}${relativeFileDirname}${pathSeparator}${fileBasenameNoExtension}.v"
],
"problemMatcher": [
"$tsc"
],
"presentation": {
"reveal": "always"
},
"group": "build"
}
Clearly I'm here because it doesn't work so I'm here to throw myself at the feet of smarter people.
This is what it actually runs
> Executing task in folder xilinx_projects: iverilog '-t vvp' '-o pulse2.v.vvp' '-l /opt/Xilinx/14.7/ISE_DS/ISE/DCM_SP.v' '-I /opt/Xilinx/14.7/ISE_DS/ISE/verilog/src/unisims' '-I /opt/Xilinx/14.7/ISE_DS/ISE/verilog/src/XilinxCoreLib/' 'wslpath C:\demand\xilinx_projects\pulse2\pulse2.v
I've been to the following pages for help:
Single quotes not escaped in debug commands #91578
how-do-i-use-bash-on-ubuntu-on-windows-wsl-for-my-vs-code-terminal
Regression: WSL Shell Task command containing spaces fails
Paths separators in build config being escaped/stripped out prior to build command being run #35593
All double quotes removed from command in tasks.json in powershell #72039
Variable Reference
And of course: Integrate with External Tools via Tasks
I see other people struggling with it. I can't tell if that is current. These things find a way dead ending when someone finally gets it or the problem goes away from some other feature that I must not know about.
I'm sure the answer is somewhere in those links. I just can't find one that works for me. I've tried all kinds of variations of escape characters and none work for me.
You'll also see that the final argument is obscene compared to what someone that knows what they are doing would use. That same command works in "command":, but not as an arg.
I try to avoid asking questions but this is killing me. I fill very close to being able to use tasks to do more but I find the documentation incomplete and without examples of what the shell sees.
You can probably see what I'm trying to accomplish. Can you offer any advice on how to do this as painlessly as possible?

From the "containing spaces fails" issue that you linked to, I wasn't able to get the original version of the sample "My Task" to work, so it almost does seem like the fixed regression has regressed again. Either that, or the new 2.0.0 task system doesn't parse it the same way.
That example used one long "command", but specified the "executable" as wsl.exe. Something else may have changed here, because reading the ${env:windir} as specified there doesn't work for me either when launching vscode from WSL. But no matter, I'm just going to leave off the path for now.
One alternative presented there was to specify each element as a separate arg (i.e. "args": [ "ls", "/", "&&", "echo", "OK", "#", "comment" ]). That does work for me, and your iverilog arguments seem to work when parsed that way as well. At least, it comes out as an unquoted command line. To be honest, the way the "Quoting" section of the Tasks doc reads, it sounds like that is the expected way to do it -- Have each element be a separate "arg". From that page:
If a command and arguments are provided, the task system will use single quotes if the command or arguments contain spaces.
That's exactly what we are seeing, of course.
But there's another alternative I found, as well. A comment further down that thread mentioned passing -c to the (old) ubuntu1804.exe executable. That led me to try something similar for the wsl.exe command, and it worked to pass in args of "-e", "sh", and "-c", along with the full commandline in the "command", like so:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Verilog: Compile iVerilog File",
"options": {
"shell": {
"executable": "wsl.exe",
"args": [
"-e"
"sh",
"-c"
]
}
},
"type": "shell",
"command": "iverilog -t vvp -o ${fileBasename}.vvp -l /opt/Xilinx/14.7/ISE_DS/ISE/DCM_SP.v -I /opt/Xilinx/14.7/ISE_DS/ISE/verilog/src/unisims -I /opt/Xilinx/14.7/ISE_DS/ISE/verilog/src/XilinxCoreLib/ wslpath ${workspaceFolder}${pathSeparator}${relativeFileDirname}${pathSeparator}${fileBasenameNoExtension}.v",
"problemMatcher": [
"$tsc"
],
"presentation": {
"reveal": "always"
},
"group": "build"
},
{
"label": "My Task",
"options": {
"shell": {
"executable": "wsl.exe",
"args": [
"-e",
"sh",
"-c"
]
}
},
"type": "shell",
"command": "ls / && echo OK # comment",
"problemMatcher": [],
"presentation": {
"reveal": "always"
},
"group": "build"
}
]
}
I believe that's your consolidated full command-line for iverilog. I've also included the "My Task" example, which is generic enough that it should work on any WSL system.

Can you replace corresponding setion of tasks.json with this ?
"type": "shell",
"command": "wsl",
"args": ["bash", "-c", "iverilog\
-t vvp -o ${fileBasename}.vvp -l /opt/Xilinx/14.7/ISE_DS/ISE/DCM_SP.v\
-I /opt/Xilinx/14.7/ISE_DS/ISE/verilog/src/unisims\
-I /opt/Xilinx/14.7/ISE_DS/ISE/verilog/src/XilinxCoreLib/\
$(wslpath '${workspaceFolder}${pathSeparator}${relativeFileDirname}${pathSeparator}${fileBasenameNoExtension}.v')"]

Related

Problem of using args in VS Code custom tasks.json

So, Inside of the tasks array in the .vscode/tasks.json, I have this one task:
{
"label": "buildReleaseIos",
"type": "shell",
"command": "flutter build ios -t lib/main_prod.dart --dart-define=DART_DEFINE_APP_NAME=App Name --dart-define=DART_DEFINE_APP_SUFFIX=.myapp"
}
This task is valid and I'm able to execute it properly.
The problem is, if I move the --dart-define from command to the args array (which I think is the better practice), like this:
{
"label": "buildReleaseIos",
"type": "shell",
"command": "flutter build ios -t lib/main_prod.dart",
"args": [
"--dart-define=BCP_DART_DEFINE_APP_NAME=App Name",
"--dart-define=BCP_DART_DEFINE_APP_SUFFIX=.myapp",
]
}
There's this error:
> Executing task: 'flutter build ios -t lib/main_prod.dart' '--dart-define=DART_DEFINE_APP_NAME=App Name' --dart-define=DART_DEFINE_APP_SUFFIX=.myapp <
/bin/bash: flutter build ios -t lib/main_prod.dart: No such file or directory
The terminal process "/bin/bash '-c', ''flutter build ios -t lib/main_prod.dart' '--dart-define=DART_DEFINE_APP_NAME=App Name' --dart-define=DART_DEFINE_APP_SUFFIX=.myapp'" terminated with exit code: 127.
But, I also have similar args usage in the launch.json which works fine:
{
"name": "Flutter Debug Prod",
"request": "launch",
"type": "dart",
"program": "lib/main_prod.dart",
"args": [
"--dart-define=DART_DEFINE_APP_NAME=App Name",
],
}
Maybe I'm mistaken on how args in tasks.json works? Can you show me how to properly use args?
Thanks
That translation in bash should look like
"/bin/bash '-c', 'flutter' 'build' 'ios' '-t' 'lib/main_prod.dart' '--dart-define=DART_DEFINE_APP_NAME=App Name' '--dart-define=DART_DEFINE_APP_SUFFIX=.myapp'
which means you need flutter in program and everything else as separate elements of args, or you can just put everything in program, but then whitespace quoting can be a bit wonky.

Configuring multiple commands to run in parallel in VS Code tasks (to compile and autoprefix Sass)

I had previously been using Koala to compile my Sass with autoprefixing and minifying (on Windows), but have come to find that Koala is no longer maintained. I'm therefore trying to figure out how people usually compile Sass, autoprefix it, and minify it automatically on save.
I'm not super experienced with command line tools like Gulp but have used NPM enough to get to the point of being able to install Dart Sass, PostCSS, etc., and since I use VS Code, have decided that its internal Tasks feature seems like the easiest way to go.
Currently if I do this in the VS Code terminal:
sass --watch sass:. --style compressed
It works, i.e., it successfully watches for changes in the sass directory and outputs a minified .css file in the parent directory.
If I stop that and do this instead:
postcss style-raw.css --output style.css --use autoprefixer --watch
It also works. I had to rename the original .scss file because apparently postcss doesn't allow --replace and --watch at the same time.
So right now, I have style-raw.scss which compiles to style-raw.css when I run the sass command, and style-raw.css gets autoprefixed and output to style.css when I run the postcss command.
Where I'm stuck is getting both commands to run at the same time via a Task. In my tasks.json file I have:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Sass Compile",
"type": "shell",
"command": "sass --watch sass:. --style compressed | postcss style-raw.css --output style.css --use autoprefixer --watch",
"problemMatcher": [
"$node-sass"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
This is connected to the Build task, which has a keyboard shortcut of ctrl+shift+B, so my ultimate goal thus far has been to be able to just hit ctrl+shift+B to start everything up getting watched and compiled and autoprefixed, etc.
Currently though, only the Sass command runs when I use the keyboard shortcut. I found another post somewhere that said the pipe should work for multiple commands, but it doesn't seem to, or perhaps you can't --watch two things at the same time, I have no idea. I tried an array for command: but that either doesn't work or I didn't have the right format.
Or perhaps there's an entirely different and better way to go about all this, but if anyone can help with using these two commands together, that'd be much appreciated.
UPDATE: SOLUTION --------------------------------------------------------
With the kind help of #soulshined below, I got the multiple commands working (the dependsOn option was the trick), but evidently it won't run two commands with the --watch parameter in the same terminal. For my use case this wouldn't work and I eventually found this extremely helpful article that explains how to run multiple tasks in a split terminal by grouping them.
If anyone else runs across this with the same use case, here is the working tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Compile CSS",
"dependsOn": [
"Sass Compile",
"Prefix Output",
],
"group": {
"kind": "build",
"isDefault": true
},
},
{
"label": "Prefix Output",
"type": "shell",
"command": "postcss style-raw.css --output style.css --use autoprefixer --watch",
"presentation": {
"group": "cssCompile"
}
},
{
"label": "Sass Compile",
"type": "shell",
"command": "sass --watch sass:. --style compressed",
"problemMatcher": [
"$node-sass"
],
"presentation": {
"group": "cssCompile"
}
}
]
}
UPDATE 2: GULP --------------------------------------------------------
Randomly ran across my own post and thought I would add that I now use Gulp. I don't remember why but the VS Code tasks turned into a hassle later on, and Gulp turned out to be not that hard to learn.
Where I'm stuck is getting both commands to run at the same time via a Task
Running concurrently can be tricky to accomplish; consider taking advantage of the dependsOn property. Here is a brief example of running commands tasks consecutively:
"version": "2.0.0",
"tasks": [
{
"label": "Echo All",
"type": "shell",
"command": "echo Done",
"dependsOn": [
"Last"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Last",
"type": "shell",
"command": "echo 'Did it last'",
"dependsOn": "First",
},
{
"label": "First",
"type": "shell",
"command": "echo 'Doing it first'",
}
]
That's a language [shell] agnostic solution. If you would like to run multiple commands you can try adding a semi colon after each statement:
"command": "sass --watch sass:. --style compressed; postcss style-raw.css --output style.css --use autoprefixer --watch"

Visual Studio Code Task is Prepending the working directory to the command

I'm trying to setup visual studio code for rust to do debugging. In my launch.json file I have
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/target/debug/vscode-rust-debug-example.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "localcargobuildtask"
}
]
}
and in my tasks.json I have
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "localcargobuildtask",
"command": "echo hi",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Where "echo hi" is in my tasks I eventually want to have something like "cargo build" or "cargo test" or whatever. But this is just a starting step.
But when I press F5 the output I get is
> Executing task: C:\programs\VSCodeWorkspaces\RustProjects\vscode-rust-debug-example-debug-config-included\echo hi <
The terminal process terminated with exit code: 2
Terminal will be reused by tasks, press any key to close it.
Rather than running "echo" from where ever the terminal finds it (working directory first, then check global path variable like you would expect) it is actually looking for a program named "echo" in my root folder of the workspace.
How do I tell visual studio code "No I don't want you to run workspace/echo, I want you to run echo in workspace" ? Or is there another more direct way to tell visual studio code "compile this before you debug it" ?
The answer to this question How to debug Rust unit tests on Windows?
suggests that changing
"command": "cargo build",
into
"command": "cargo",
"args": [
"build"
],
in the tasks.json file works and somehow it does. Maybe the editor is configured to search the %path% for single word commands, or maybe some plugin I installed overwrote the "cargo" command. Maybe the reason echo wasn't found is because it is a terminal command, not a program. Maybe the error message was a lie and it was only reporting that it couldn't find workspacefolder\command despite checking %path%? Or maybe none of that. I have no idea. It is some pretty idiosyncratic behavior.

Can't get simple task to run in vscode

I am ultimately trying to set up vscode to build typescript, but I first just wanted to get a simple task to run, and I can't seem to get that to work. I now want to just run the "Hello world" task from https://code.visualstudio.com/docs/editor/tasks, which is to simply echo a string to the output window.
My tasks.json is in the .vscode folder, and its content is:
{
"version": "0.1.0",
"command": "echo",
"isShellCommand": true,
"args": ["Hello World"],
"showOutput": "always"
}
When I try to run the task from the command palette and choosing "Tasks: Run Task," I see "no tasks found" when I expect to see this echo task. I don't know why I don't see this task in a task list.
What am I doing wrong?
FWIW, my vscode version is 1.11.1.
This works on current vscode:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "MyHelloTask",
"type": "shell",
"command": "echo",
"args": ["Hello\ World"],
"presentation": {
"echo": true,
"reveal": "always",
"panel": "shared"
}
}
]
}
What was wrong?
The property showOutput is deprecated. Use the reveal
property inside the presentation property instead.
See also the 1.14 release notes.
Also isShellCommand now became type, etc...
Also note the escaped space in the argument. (triggers explict complaint about it otherwise. Yes, despite the quotes around it.)

Multiple commands/tasks with Visual Studio Code

I have a local folder that I use as a scratch pad for multiple little sample and toy pieces of code. I store a host of python, C++, shell scripts etc. in this directory.
I'm using Visual Studio Code (on OS X) and am looking into its tasks to run/compile the code snippets without having to switch to a terminal.
For example, I found this following task will run python on the currently open file.
// A task runner that runs a python program
{
"version": "0.1.0",
"command": "/usr/bin/python",
"args": ["${file}"]
}
This task will use python as the task runner irrespective of the type of file I'm currently editing.
How do I implement a task to run a command based on the file type (or select between multiple commands)? I.e. if I'm editing a C++ file, it will run clang++.
If I can't do it based on file type; are there any alternatives to this?
An alternative would be; are multiple commands supported?
You can always use bash as your task runner and then assign arbitrary terminal commands as your tasks.
{
"version": "0.1.0",
"command": "bash",
"isShellCommand": true,
"showOutput": "always",
"args": [
"-c"
],
"tasks": [
{
"taskName": "My First Command",
"suppressTaskName": true,
"isBuildCommand": true,
"args": ["echo cmd1"]
},
{
"taskName": "My Command Requiring .bash_profile",
"suppressTaskName": true,
"args": ["source ~/.bash_profile && echo cmd2"]
},
{
"taskName": "My Python task",
"suppressTaskName": true,
"args": ["/usr/bin/python ${file}"]
}
]
}
A few notes on what is happening here:
Using bash -c for all tasks by putting it in args list of the command so that we can run arbitrary commands. The echo statements are just examples but could be anything executable from your bash terminal.
The args array will contain a single string to be passed to bash -c (separate items would be treated as multiple arguments to the bash command and not the command associated with the -c arg).
suppressTaskName is being used to keep the taskName out of the mix
The second command shows how you can load your ~/.bash_profile if you need anything that it provides such as aliases, env variables, whatever
Third command shows how you could use your Python command you mentioned
This will not give you any sort of file extension detection, but you can at least use cmd+p then type "task " to get a list of your tasks. You can always mark your 2 most common commands with isBuildCommand and isTestCommand to run them via cmd+shift+b or cmd+shift+t respectively.
This answer has some helpful information that might be useful to you as well.
The simplest way would be to add them separated by ; (or &&) in a shell:
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"command": "cd ~/dev/xxxx;source ~/dev/yyyy;ls",
}
]
}
Recent changes to the tasks.json seem to have made a command available for each of the tasks listed. See https://code.visualstudio.com/docs/editor/tasks which makes a lot of this moot.
This answer was originally aimed at a more complex solution, but the simple shell runner task format as presented in the accepted answer proved more useful. See below for what that looks like now.
The limitation here is that VS Code is limited to a single high level build task/command for a given workspace. Multiple sub-tasks are allowed, but they are limited to using the top level "command" but can provide different "arguments". This would be well suited to an environment that uses a build system akin to make, ant or msbuild. E.g.;
{
"version": "0.1.0",
"command": "make", // command must appear here
"tasks" : [
{
"taskName": "clean",
"suppressTaskName": false, // false by default
//"command": "somethingelse", // not valid here
"args": ["${file}"] // if required
},
{
"taskName": "install"
// ...
}
]
}
Two alternatives are available;
Have a custom script attempt to run the compile/execution solely given the arguments in task.json.
-- the shell file would be a simple
"$#" # run what I get
-- the tasks.json
"args": ["clang++", "-std=c++14", "-O2", "${file}"]
Getting the exectuable to run (./a.out) was more effort. Simply adding it as an argument didn't work, the shell script was required to execute it if it was there.
Shell out the switching and the execution of the output to a custom script, given the file extension and filename. This proved easier to implement and offered more control in the shell script.
{
"version": "0.1.0",
"isShellCommand": true,
"taskName": "generic build",
"showOutput": "always",
"args": ["${fileExtname}", "${file}"]
"command": "./.vscode/compileme.sh", // expected in the "local settings" folder
//"command": "~/compileme.sh", // if in HOME folder
}
And the shell script, compileme.sh;
#!/bin/sh
# basic error checking not shown...
echo "compilation being executed with the arguments;"
echo "$#"
filetype=$1
file=$2
if [ $filetype = ".cpp" -o $filetype = ".cxx" ] ; then
clang++ -std=c++14 -Wall -Wextra -pedantic -pthread $file && ./a.out
elif [ $filetype = ".c" ]
then
clang -std=c11 -Wall -Wextra -pedantic -pthread $file && ./a.out
elif [ $filetype = ".sh" ]
then
$file
elif [ $filetype = ".py" ]
then
python $file
else
echo "file type not supported..."
exit 1
fi
Given the options listed above, the second option is preferable. This implementation works on OS X, but it could be easily ported to Linux and Windows as needed. I'll keep on eye on this and try track changes to the VS Code build tasks, file based builds or support for multiple commands could be a welcome addition.
My tasks.json supports a few runners, and a default for the build that prints message as a reminder. It uses the shell as the runner and now looks like...
{
"version": "0.1.0",
"isShellCommand": true,
"taskName": "GenericBuild",
"showOutput": "always",
"command": "sh",
"suppressTaskName": false,
"args": ["-c"],
"tasks": [
{
"taskName": "no build",
"suppressTaskName": true,
"isBuildCommand": true,
"args": [
"echo There is no default build task, run a task by name..."
]
},
{
"taskName": "cpp",
"suppressTaskName": true,
"args": [
"clang++ -std=c++14 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"
]
},
{
"taskName": "shell",
"suppressTaskName": true,
"args": [
"\"${file}\""
]
},
{
"taskName": "python",
"suppressTaskName": true,
"args": [
"python \"${file}\""
]
},
{
"taskName": "c",
"suppressTaskName": true,
"args": [
"clang -std=c11 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"
]
}
]
}
You can use compound tasks to run multiple commands
https://code.visualstudio.com/docs/editor/tasks#_compound-tasks
You could write and run a custom script file instead of python etc. directly. In the script file you would extract the file extension in order to call python, clang or whatever the compiler/translator needed may be.
So your task file would look like this;
// A task runner that runs a program
{
"version": "0.1.0",
"command": "${workspaceRoot}\\runProgram.sh",
"args": ["${file}"]
}
I made this script.
It requires that you install python IDLE in your environment.
This will open the IDLE and run your python file each time you run your task (CTRL+Shift+B).
{
"version": "0.1.0",
"command": "/usr/bin/idle",
"isShellCommand": false,
"showOutput": "never",
"args": ["-r","${file}"]
}