nvm, node etc. paths missing from integrated terminal - visual-studio-code

I am using the insiders edition and zsh with ohmyzsh inside iTerm2 on OSX High Sierra.
If I launch the integrated command line in VSCode echo $path returns:
/Users/username/.yarn/bin:/Users/username/.cargo/bin:/usr/bin:/usr/bin/lldb:
/Users/username/bin:/usr/local/bin:/Users/username/.scripts:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
Obviously, the node path, nvm path etc. are missing so the terminal is not much use to me.
But typing echo $path in my terminal outside of VSCode returns:
/Users/username/.nvm/versions/node/v9.0.0/bin /Users/username/.yarn/bin
/Users/username/.cargo/bin /usr/bin /usr/bin/lldb /Users/username/bin
/usr/local/bin /Users/username/.scripts /usr/local/bin /usr/bin /bin /usr/sbin
/sbin /opt/X11/bin
Which has all the paths I need for my development activities.
I have this alias in my ~/.zshrc:
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCodeInsiders" --args $* ;}
If I launch code using the alias from an external terminal the node paths are correct.
As far as I can see this is because loading code from the Dock shortcut doesn't load the terminal using my user profile so it loads the profile from /etc/zprofile instead, which sets the path using /usr/libexec/path_helper so it misses all of my important paths that I need for development.
I suspect that most people would prefer the settings from their user profiles to be used. This would mean all of our dev apps would work from the integrated terminal and also the end of the infamous nvm is not compatible with the npm config "prefix" option error caused by the improper setting of paths.
How can I get VSCode to use the environment of the terminal in my user profile when launched from the Dock?

Go to settings and edit integrated terminal
// Object with environment variables that will be added to the VS Code process to be used by the terminal on OS X
"terminal.integrated.env.osx": {},
there you can pass in your variables

The vscode docs also offer a solution for the warning nvm is not compatible with the npm config "prefix" option: currently set to "/usr/local":
https://github.com/Microsoft/vscode-docs/blob/master/docs/editor/integrated-terminal.md#why-is-nvm-complaining-about-a-prefix-option-when-the-integrated-terminal-is-launched
From the docs:
This is mostly a macOS problem and does not happen in external
terminals. The typical reasons for this are the following:
npm was globally installed using another instance of node which is
somewhere in your path (such as /usr/local/bin/npm). In order to get
the development tools on the $PATH, VS Code will launch a bash login
shell on start up. This means that your ~/.bash_profile has already
run and when an Integrated Terminal launches, it will run another
login shell, reordering the $PATH potentially in unexpected ways. To
resolve this issue, you need to track down where the old npm is
installed and remove both it and its out of date node_modules. You can
do this by finding the nvm initialization script and running which npm
before it runs, which should print the path when you launch a new
terminal.
Once you have the path to npm, you can find the old node_modules by
resolving the symlink by running a command something like this:
ls -la /usr/local/bin | grep npm
This will give you the resolved path at the end:
... npm -> ../lib/node_modules/npm/bin/npm-cli.js
From there, removing the files and relaunching VS Code should fix the issue:
rm -R /usr/local/bin/npm /usr/local/lib/node_modules/npm/bin/npm-cli.js

Related

"Unable to resolve your shell environment"

On macOS Big Sur, I installed MacPorts and installed bash using it. I set my default shell to the MacPorts version of bash /opt/local/bin/bash by using chsh.
All was well.
I decided I would rather use Homebrew. I uninstalled MacPorts, installed Homebrew, installed bash via homebrew, and set my default shell to /opt/homebrew/bin/bash via chsh.
All was well, except when I am first starting vscode (from Finder) which reports:
Unable to resolve your shell environment: A system error occurred (spawn /opt/local/bin/bash ENOENT)
This is a totally reasonable complaint, since that binary no longer exists. It makes this complaint when launched from Finder, but not when launched from the terminal. There is no reference to this prior version of bash in my user settings.json file, nor is it in my .bashrc.
Where the heck could vscode be getting this path to bash from?!
Edit (2021-10-11): there is something strange going on here. If I open an integrated terminal in vscode:
$ echo $SHELL
/opt/local/bin/bash
$ ls -lh $SHELL
ls: /opt/local/bin/bash: No such file or directory
$ ps -p $$
PID TTY TIME CMD
45388 ttys001 0:00.12 /bin/bash -l
(there are no such problems in the regular macOS Terminal app)
Edit again (2021-10-12):
I tried resetting vscode (via rm -rf ~/Library/Application\ Support/Code).
I tried resetting vscode's cache (via rm -rf ~/Library/Caches/com.microsoft.*),
I downloaded iTerm2 and ran it; it loaded the correct shell (/opt/homebrew/bin/bash) without issue (tried this in hopes of determining if there was some other setting in macOS pointing at the old macports version of bash).
Had the same issue and was able to resolve this by changing the shell for the logged in user (using chsh -s, cf. the hint at the bottom of https://github.com/microsoft/vscode/issues/139867). As this change is not being picked up by VSCode in your currently logged in session, don't forget to explicitly log out and log back in again after performing this step.

powershell and conda: conda activate env returns command not found

I have pip installed powerline-shell in my base conda env. Switching envs yields the following error:
conda activate <env_name>
-bash: powerline-shell: command not found
I also tried running conda init powershell but it took no actions.
I have miniconda3, with conda 4.7, installed on MacOS Mojave.
I don't know a simple solution to this. I'm thinking you either need to install it in every env (which I don't recommend because it's best to avoid using pip in Conda) or you create a link to the powerline-shell binary in another location that you can keep on PATH to avoid adding the entire miniconda3/bin/ directory to PATH. I've done something like this in the past, but never with a Python entry point before.
I'd try something like
mkdir -p ~/.local/bin
ln -s /your/path/to/miniconda3/bin/powerline-shell ~/.local/bin/powerline-shell
Then add .local/bin to PATH in your .bashrc, probably toward the beginning (e.g., before the Conda section). The path here (~/.local/bin) is totally arbitrary, so adjust to your preferences. Main point is to minimize what you are exposing globally in a shell session.
Note: conda init powershell is for Windows PowerShell users.

ionic install fails on fsevents on Windows

I have tried to install ionic on Windows but installation has failed. What should I do?
I have tried more than 20 times but still I got the same problem.
Here I have attached the screenshot for this
Entered commands:
npm install -g cordova
npm install -g ionic
Note: this one windows server machine
My guess is that the NPM global path has not been added to your windows path.
These are the steps to try:
First verify that the command just installed can run. The output contains the full path to the command installed, just copy and paste it to the command line. In your case it might be something like this %APPDATA%\Roaming\npm\iconic -v
Lets assume that works. That means that the command was successfully installed and will run, now we need to add it to your PATH so you can run it from the command line.
Next, Press Windows key and type "path" and select "Edit environment variables for your account". From here. Add or append the path %APPDATA%\Roaming\npm to your PATH variable, and save the results.
Once it is on your PATH you can run it from any command shell window.
Next, start a new command shell (e.g., Windows+R, cmd, Enter) and then type the command iconic -v. Viola, it should work without requiring the full path to the script.
I'm doing most of this from memory so hopefully it's correct. But if it needs some tweeks, let me know and I'll update the answer.
For completness, this install didn't actually fail. The lines with fsevents are warnings. The fsevents package is only designed to work on Mac Unix so these warnings can be safely ignored.
As Suraj Rao mentioned, also see Nodejs cannot find installed module on Windows?.

Could not install typings files... in VS Code, with Bash on Ubuntu on Windows

I'm trying to use Visual Studio Code with Bash on Ubuntu on Windows. I have VS Code installed on Windows, but otherwise use Bash exclusively for my JavaScript development workflow.
I have node and npm installed on Bash (via apt-get), however, I do not have node and npm also on Windows to avoid duplication. In my VS Code settings, I have configured the integrated shell to use Bash:
"terminal.integrated.shell.windows": "C:\\windows\\Sysnative\\bash.exe"
The integrated shell works fine, and node and npm are both accessible. However, when I launch VS Code on my project, I get the warning:
Could not install typings files for JavaScript langauge features. Please ensure that NPM is installed or configure 'typescript.npm' in your user settings
I have tried both the following settings, neither of which work:
"typescript.npm": "C:\\windows\\Sysnative\\bash.exe"
"typescript.npm": "C:\\windows\\Sysnative\\bash.exe -c \"npm\""
Is it possible to get VS Code to install typings files via the npm that is installed on Bash?
Try setting “externalTerminal.windowsExec”: “bash” in your settings file to set it as the terminal executable and then restart VS Code

Set global $PATH environment variable in VS Code

I'm defining a custom $PATH environment variable in my ~/.bash_profile (on a Mac), like so:
PATH="$HOME/.cargo/bin:$PATH:$HOME/bin"
However, VS Code of course does not run my .bash_profile, so it does not have my custom paths. In fact, if I Toggle Developer Tools and check process.env.PATH, it doesn't even seem to have /usr/local/bin.
How do I globally set the $PATH environment variable in VS Code?
(I want to set it globally, not per project or per task, since I'm maintaining a lot of small packages.)
If you only need the $PATH to be set in the integrated terminal, you can use VS Code's terminal.integrated.env.<platform> variable (added in version 1.15). Press Cmd+Shift+P (or Ctrl+Shift+P) and search for "Preferences: Open Settings (JSON)". Then add the following entry to the settings file:
"terminal.integrated.env.osx": {
"PATH": "...:/usr/bin:/bin:..."
}
(Replace .osx with .linux or .windows as needed.)
To see your system's $PATH, type echo "$PATH" in Terminal.app, and copy and paste it into the settings snippet above.
As for having the $PATH available everwhere in VS Code, so that it will
be used by extensions that call binaries, the only workaround I've found so far is this:
Configure your shell (bash by default) to have the $PATH you want. For example, my ~/.bash_profile has the following line:
PATH="$PATH:$HOME/bin"
In VS Code, press ⇧⌘P and type install 'code' command if you haven't done so before.
Quit VS Code.
Launch VS Code not by clicking the icon in the dock or in Launchpad, but by opening Terminal.app and typing code. Your newly set path will be active in VS Code until you quit it.
If VS Code restarts, for example due to an upgrade, the $PATH will reset to the system default. In that case, quit VS Code and re-launch it by typing code.
Update: VS Code on Mac and Linux now apparently tries to automatically resolve the shell environment when it is started by clicking the icon (rather than via code). It does this by temporarily starting a shell and reading the environment variables. I haven't tested this though.
In:
> Preferences: Open Settings (JSON)
add to the JSON file:
"terminal.integrated.env.windows": {
"PATH": "${env:PATH}"
},
-> terminal.integrated.env should end with .osx, .linux or .windows depending on your OS.
In order to check if it works execute in your VS Code Terminal:
# For PowerShell
echo $env:PATH
# For bash
echo "$PATH"
I am using vscode on macos for C/C++ development in conjunction with CMake.
The vscode extension CMake Tools allows to manipulate environment variables via the configuration properties cmake.configureEnvironment, cmake.buildEnvironment and cmake.environment (acting respectively on the CMake configuration phase, the build phase and both - see docs).
Then you can extend your system PATH with custom paths by adding the following snippet to your user or project settings.json:
"cmake.environment": {
"PATH": "~/.myTool/bin:${env:PATH}"
},
Visual Studio Code is the problem.
No matter how you set your PATH variable in the shell, there are cases where Visual Studio Code will not inherit your PATH setting. If you're using an application launcher like LaunchBar to start Visual Studio Code, your PATH variable will not be inherited.
Here is a system-wide fix:
In the /etc/paths.d directory, create a file with your Unix username. In that file, place the additional paths that Visual Studio Code needs to work. In my case, this is the contents of my /etc/paths.d file:
/usr/ucb
/opt/local/bin
/opt/local/sbin
~/go/bin
Note: Your /etc/paths.d file will be processed system-wide. Since most systems are single-user, this shouldn't be a problem for most developers.
Since this is the top Google search result for variants of "VS Code path", I will add my answer here.
I'm running Linux and my problem was that VS Code couldn't find some executable needed to build my project. I was running VS Code from the quick launcher (ALT+F2), and not from a Terminal. I tried modifying the PATH variable in many different places, but I couldn't seem to get it right.
In the end, placing the right PATH inside of ~/.zshenv is what worked. It's because .zshenv is the only file that gets sourced for non-interactive shell command execution like from inside of VS Code (more detailed explanation here https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout )
This was even easier to fix than the above answers suggested.
Open VSCode Settings (Ctrl + ,) and search for terminal.defaultProfile.
I updated my Terminal > Integrated > Default Profile: Windows.
It was set to null by default. As soon as I changed it to PowerShell and restarted the terminal, it picked up my system's path variables!
What did the trick in my case (Linux Mint 19.3 Cinnamon, VS code installed via snap) was to put my appended PATH in ~/.profile . Since this file is read at the beginning of a user session, don't forget to logout/login or reboot after editing this file.
I'm working with ubuntu 18.04. I had a similar problem, my enviroment variables were defined and the terminal knows the $PATH but when I tried to debug with golang, go libraries were not found in $PATH variable.
So, to solve it I uninstall the default version from ubuntu software and install manually using the following instructions:
https://code.visualstudio.com/docs/setup/linux
It works for me.
As of VS Code v1.63.2, you can proceed with Ctrl + Shift + P and then type Open Settings (JSON), and simply add the following line.
"terminal.integrated.inheritEnv": true
In my case the code was already there, but set to false. After changing it, everything was fine.
Getting Code to load your existing ~/.bash_profile would be best. I think the docs here are the relevant reference:
https://code.visualstudio.com/docs/editor/integrated-terminal#_linux-os-x
Typically $SHELL is your primary shell on Unix-like systems so you
probably won't want to change the shell. You can pass arguments to the
shell when it is launched.
For example, to enable running bash as a login shell (which runs
.bash_profile), pass in the -l argument (with double quotes):
// Linux "terminal.integrated.shellArgs.linux": ["-l"]
// OS X "terminal.integrated.shellArgs.osx": ["-l"]
Although, it looks like that setting is the default on my current VS Code (OS X) setup. Integrated terminal is running my ~/.bash_profile without any changes to the configuration. Perhaps try adding echo Executing .bash_profile... to test if it's running when a new terminal is opened in Code.
Add the following to your ~/.bash_profile:
launchctl setenv PATH $HOME/.cargo/bin:$PATH:$HOME/bin
Or run a Bash script when needed, e.g.:
#!/bin/bash
set -Eeuxo pipefail
proj_path=$( cd $( dirname ${BASH_SOURCE[0]} ) && pwd )
launchctl setenv PATH $proj_path/bin:${PATH:-}
For me it's resolved by editing the .desktop file.
Originally I have
Exec=/usr/bin/code-oss --unity-launch %F
. Just changed to
Exec=zsh -c "source ~/.zshrc && /usr/bin/code-oss --unity-launch %F"
since I use zsh, instead of bash. But if you have the same problem with bash, simply replace zsh with bash. And shortcuts from your desktop environment should be fixed.