I'm trying to run a task on window load in VSCode where a terminal opens and nvm use && yarn dev is run by default. However, running this shell tasks seems to not load my zsh profile.
The output I get from running my task is:
The terminal process "zsh '-c', 'nvm use && yarn dev'" terminated with exit code: 127.
Terminal will be reused by tasks, press any key to close it.
But if I then manually start a new terminal and run the same command (ie: by pressing plus, opening a new integrated terminal), it will work as intended.
Suspecting that VSCode isn't loading my profile for some reason, I tried adding the following to my task, it resulted in the error /bin/zsh: can't open input file: nvm use && yarn dev The terminal process "zsh '-l', 'nvm use && yarn dev'" terminated with exit code: 127..
// in dev task
"options": {
"shell": {
"executable": "zsh",
"args": ["-l"]
}
},
.vscode/tasks.json
{
"version": "2.0.0",
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "dedicated",
"showReuseMessage": true
},
"tasks": [
{
"label": "Create terminals",
"dependsOn": [
"Dev",
],
// Mark as the default build task so cmd/ctrl+shift+b will create them
"group": {
"kind": "build",
"isDefault": true
},
// Try start the task on folder open
"runOptions": {
"runOn": "folderOpen"
}
},
{
"label": "Dev",
"type": "shell",
"command":
["nvm use && yarn dev"],
"isBackground": true,
"problemMatcher": [],
"presentation": {
"group": "dev-group"
}
},
]
}
This worked for me-
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "/bin/zsh",
"args": ["-l", "-i"]
}
},
github.com/microsoft/vscode/issues/143061
try adding this to your settings.json
"terminal.integrated.profiles.osx": {
[...]
"zsh": {
"path": "/bin/zsh -l",
"args": [
"-l"
]
},
[...]
},
Note that the important part is
"path": "/bin/zsh -l",
I had the same problem and I found that for some reason VScode does not take into consideration the -l flag passed in args. So you can just include it with path.
If you do not have terminal.integrated.profiles.osx in your settings, you can copy it from the default settings (open the Command Palette and search for 'default settings').
I did not need to do this, but you can make sure that zsh is the default terminal profile for VScode by setting terminal.integrated.defaultProfile.osx to zsh
Try running echo $SHELL from VSCode's integrated terminal. If you're on a Mac or Linux machine, you can compare that output to the output from the terminal app (outside VSCode). It's possible your default shell in VSCode is set incorrectly or using a copy of zsh at another location. If so, set VSCode's default shell through the command palette (Terminal: Select Default Shell).
Also check out your shell's default profile (Terminal: Select Default Profile) from the command palette and make sure it's set to zsh -l... using the -c argument (non-login non-interactive) will prevent ~/.zshrc from being executed, which sounds like what's going on here given your error output.
Finally, confirm your profile is located correctly (at ~/.zshrc) and that both nvm and yarn PATHs are exported. Alternatively, if you're trying to reference yarn locally (if for some reason you only installed it locally), you'll need to run yarn via npx...
You may need to add an automation profile as well
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "/bin/zsh -l",
"args": ["-l"]
}
},
"terminal.integrated.automationProfile.osx": {
"path": "/bin/zsh"
}
macOS 12.6.1 | vscode 1.74.0
I did not manage to do any if it since none of this worked, so I have just removed warning...
"terminal.integrated.showExitAlert": false
Or via GUI
I hope that will not get minus points here...
Related
I have these settings in my workspace:
"terminal.integrated.defaultProfile.windows": "Ubuntu-20.04 (WSL)",
"terminal.integrated.profiles.windows": {
"Ubuntu-20.04 (WSL)": {
"path": [
"C:\\WINDOWS\\System32\\wsl.exe"
],
"args": [],
}
},
I want to either disable all others or set it wsl as default.
I still have all the rest in my list:
When I set wsl as default, its working only when I create a new terminal...
How can I remove all the others and stay only with wsl?
Thanks
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"
I'm attempting to debug a vscode extension in Windows using WSL. It appears as though the prelaunchtask is using the cmd.exe arguments, which causes the prelaunchtask to fail with bash.
Executing task: npm run watch <
/bin/bash: /d: 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.
Any thoughts how I might force the debug terminal to properly issue the bash arguments?
You can achieve this by manually specifying the shell executable and arguments used by the Extension launch configuration. Assuming you are starting with the example extension, you can edit the task for the "npm: watch" script in .vscode/tasks.json, forcing it to launch WSL with no additional arguments.
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
},
// Force this to use WSL with no additional arguments
"options": {
"shell": {
"executable": "C:\\WINDOWS\\System32\\wsl.exe"
},
"args": []
}
}
I press Ctrl+Shift+B to start a build in Visual Studio Code (it's configured to just run GNU Make), and the build tool output is written to the Terminal window.
However, it's appended to the output from the previous build, which is confusing.
How do I configure VS Code to clear the terminal window before starting a new build?
November 2018 Update
As of this commit (and a few subsequent follow-ups), you can now add a clear presentation option to your task to have it clear the terminal before each task run.
Working example (on fresh clone+build):
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "[gcc] Build",
"type": "shell",
"command": "g++",
"args": [
"source.h",
"-Wall",
"-o",
"a.out"
],
"presentation": {
"clear": true // <-- this line
}
}
]
}
(Note: the linked commit diff has the key being named clearBeforeExecuting but it's apparently since been changed to just clear).
Prior to this, I created a clear_g++ script on my path with just:
#!/bin/bash
clear
exec g++ $*
And changed my command from g++ to clear_g++.
Since I liked the idea of this approach but it didn't end up working out.
You can change from settings menu (at least from version 1.30.2 and above)...
On Mac, just hit Code > Preferences > Settings.
Then just search for "clear" and check Clear Previous Output.
I tried to find a solution but can't. Simple hack I tried is to open new build in new tab. Add this presentation key to your task in tasks.json
"presentation": {
"echo": true,
"reveal": "never",
"focus": false,
"panel": "new"
}
panel:new will open in new terminal.
Add this user setting to clear the OUTPUT tab on clicking run (▶)
"code-runner.clearPreviousOutput": true,
This is not the same as clearing the terminal but it might be what someone wants.
[Edit] This requires the Runner extension, which I'd recommend for testing/running scripts directly within VS Code.
New Visual Studio code 1.56. This works in windows.
You simply go to Preferences:Open Settings(UI), search "Clear" and check option as below:
This will make sure that terminal remain clear on every run, thus ensuring only 1 file run is visible at a time.
Update Visual Code 1.54 +
To clean terminal when click Run.
Install Code-runner extension.
Setting > search "clear" -> Check on "Clear Previous Output"
If you control the build task yourself, it's easy to prepend a clear command:
"tasks": [
{
"label": "build",
"type": "shell",
"command": "clear && make",
....
In Visual Studio Code version 1.52.1, the clearing by default of the terminal is achieved with the clear: true property (=Controls whether the terminal is cleared before executing the task.). Unfortunately it does not do the job, I still see the terminal with older messages. I have to manually enter "clear" in the terminal to clear it completely.
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
This is added in tasks.json which looks like this under OSX:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++11",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang++",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
}
]
}
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}"]
}