I have had a setup for a while to run C++ programs in VS Code. It was basically a tasks.json which looked like this
"tasks": [
{
"label": "Compile and run",
"type": "shell",
"command": "",
"args": [
"g++",
"${fileBasename}",
"-o",
"test",
"&&",
...
}
I had also kept a settings.json to define the terminal to be used which was command Prompt,
For some reason in the latest update for VS Code my setup got messed and it does not work anymore.
I would like to know the right syntax to define cmd as the terminal for running the tasks since things like && < > dont work in powershell
After Luuk's comment I did more digging and found the followig to work for my vs code tasks problem. I only had to change && to ; and for the input out redirection I used this answer. Now my tasks.json looks like this
"tasks": [
{
"label": "Compile and run",
"type": "shell",
"command": "",
"args": [
"g++",
"${fileBasename}",
"-o",
"test",
";",
"cmd",
"/c",
"'test.exe < input.txt > output.txt'",
";",
"del",
"*exe",
";",
"del",
"${fileBasename}"
}
]
Related
I had a custom build system in sublime for C++ which takes input from one file and redirects the output to another.
My sublime build system is as follows :
{
"cmd": ["g++.exe","-std=c++14", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<inputf.in>outputf.in"],
"selector":"source.cpp",
"shell":true,
"working_dir":"$file_path"
}
this helped in taking input from "inputf.in" file and print the output in "outputf.in" whenever i ran my program.
I want similar functionality in vs code but dont know how to configure build-task.
Here is a build task for a Node addon, you have to put it in .vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "npx node-pre-gyp build -j max",
"problemMatcher": {
"base": "$gcc",
"fileLocation": [
"relative",
"${workspaceFolder}/build"
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
You can press Ctrl-Shift-P to bring the command palette and select configure build task.
The problem matcher allows VSCode to highlight the compiler errors in your editor.
The default build task is the one you launch with a hotkey.
Trying to avoid the XY problem, my problem at the very high level: I work in a VS Code workspace with R and Python code files, and I often need to debug one or the other. Currently I have different debug launch configurations saved, and I need to switch between them manually - that is a pain. I would like to use F5 to
launch the Python debugger when the editor is active in a .py file
launch the R debugger when the editor is active in a .R file
I see many technical ways of doing that, but all have their roadblocks (some of which may just poor documentation):
Create my own extension with a dynamic debug configuration that determines the type of the active editor and starts the correct debug configuration. (A lot of effort required.)
Use a "compound" launch configuration, starting both R and Python launch configurations, and stopping all but one. This can be done using a "prelaunchTask" for each, but non-zero return codes create error message that I don't like.
Use editor-dependent key mappings ("when": "editorLangId == 'python'"), but which command for debugging to start and how to pass the launch configuration? There is vscode.startDebug which takes arguments (https://github.com/microsoft/vscode/issues/4615), but I cannot keybind to that. And then there is workbench.action.debug.start which seems to ignore arguments. And then there is vscode.commands.executeCommand to which I cannot keybind, either.
Use a multi-command extension to bind a key to something like "set debug configuration, then press F5". But how to do that?
Etc. etc.
After much fiddling, here is one solution following idea 2:
settings.json (launch configs could be put into launch.json):
{
"debug.onTaskErrors": "abort",
"launch": {
"compounds": [
{
"name": "Python/R: Current File",
"configurations": [
"Python: Current File",
"R: Current File",
],
},
],
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"preLaunchTask": "check_ext py",
"request": "launch",
// ...
},
{
"name": "R: Current File",
"type": "R-Debugger",
"preLaunchTask": "check_ext R",
"request": "launch",
// ...
}
],
},
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "check_ext py",
"type": "shell",
"command": "echo ${fileExtname} | grep -i ^.py$",
},
{
"label": "check_ext R",
"type": "shell",
"command": "echo ${fileExtname} | grep -i '^.R$'",
},
],
}
I am trying to define a VSCode task in tasks.json that would adapt to the specific architecture where VSCode runs. To do this, I want to get the architecture as uname --m (e.g. "aarch64" or "amd64"). My goal is to interpolate the output of uname into an environment variable like so
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "cmake",
"args": [
"-DMYLIB_INCLUDE_DIR=$MYLIB/include",
"-DMYLIB_LIBRARY=$MYLIB/lib"
],
"options": {
"env": {
"MYLIB": "${workspaceFolder}/mylib/${command:get_arch}"
}
},
}
]
In my case, I will have architecture-specific versions of mylib under mylib/aarch64, mylib/amd64, etc.
My attempt so far as been to define a second get_arch task used in the environment definition of MYLIB, that simply runs uname.
{
"label": "get_arch",
"type": "shell",
"command": "uname --m"
}
Of course, this task is not a proper command and so it isn't detected by VSCode and my build task fails. I checked out the documentation on variable substition, but they don't mention if it's possible to substitute a shell command. I guess this would be possible from within an extension, but I want to keep things as simple as possible.
This extension provides a way to launch arbitrary shell commands as a VS Code command:
"tasks": [
{
"label": "test_arch",
"type": "shell",
"command": "echo",
"args": [
"${MYARCH}"
],
"options": {
"env": {
"MYARCH": "${input:get_arch}"
}
},
"problemMatcher": []
},
],
"inputs": [
{
"id": "get_arch",
"type": "command",
"command": "shellCommand.execute",
"args": {
"command": "uname -m"
}
}
]
One disadvantage I discovered is that you have to press Enter once more when the result of the command is prompted in picker. Aside of this, this is the straightest way to implement what you want, and yet it can be utilized in many similar situations.
Alternatively, you can add pickString input with arch picker or create an extension that adds only single command GetArch.
If you don't want to press enter every time, you can add the option useFirstResult: true in the args section.
I would like to build my program using Git Bash or MinGW so I have this build task:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "gcc",
"args": ["-g", "${relativeFile}", "-o", "${fileBasenameNoExtension}"],
"options": {
"shell": {
"executable": "C:\\Program Files\\Git\\bin\\bash.exe",
"args": ["-l"]
},
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
},
]
}
I have two issues:
The cwd command does not work because ${fileDirname} is in the Windows form C:\Foo\Bar and since I am on bash I would like the POSIX form /c/Foo/Bar.
My task is not executed in the current terminal, but in a weird Window that display no output:
/usr/bin/bash: gcc -g .vscode\tasks.json -o tasks: No such file or directory
The terminal process terminated with exit code: 127
Terminal will be reused by tasks, press any key to close it.
With the extension Command Variable (v0.2.0) you can add Posix versions of the directory variables.
"cwd": "${command:extension.commandvariable.file.fileDirnamePosix}"
What I want to achieve is using zsh in "output" pane whenever I run a Task in VSCode, but it keep using /bin/sh instead.
The "Terminal" pane is using zsh correctly though.
Here's my configurations:
➜ ~ echo $SHELL
/bin/zsh
➜ ~ which zsh
/bin/zsh
tasks.json
{
"version": "0.1.0",
"command": "echo Im $0",
"suppressTaskName": true,
"isShellCommand": {
"executable": "/bin/zsh"
},
"showOutput": "always",
"tasks": [
{
"taskName": "Test",
"args": ["test"]
}
]
}
And the output when I run the Task is:
Im /bin/sh
You could change the settings from menu File -> Preferences -> Settings. Then put the options to use zsh shell.
... another lines
... another lines
"terminal.integrated.shell.linux": "zsh"
The recommended way to do this now is the following:
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "zsh",
"args": []
}
},
"terminal.integrated.defaultProfile.linux": "zsh"
As the other answer presents the deprecated way to do it.
Note that you can customize this profile with arguments if need be :)