Repository navigation
Description
- VSCode Version: 1.30, 1.31, 1.33
- OS Version: Ubuntu 18.04/ MacOS 10.14 Mojave
Steps to Reproduce:
- From OS terminal, type
echo $PATH, you may see values similar to following:
/Users/xxx/anaconda3/bin:/Users/xxx/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
- Open a
vscodeinstance, in the integrated terminal typeecho $PATH, now the value may look like following:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/xxx/anaconda3/bin:/Users/xxx/anaconda3/condabin
The CONTENT in two $PATH values are the same, BUT in different order. Problems it may cause: some system configuration depends on the first executable value they can find in the $PATH. Different $PATH value order will have different default program being used. In previous example, the value in step 1 will use python3 and pip3 from anaconda. But in vscode integrated terminal, the $PATH value in step 2 will use the python2 and pip2 in /usr/local/bin. Even you activate a virtualenv in integrated terminal, it will still use the wrong virtual env. More details and a proposed workaround can be seen in vscode-python #4434.
This has been observed on both MacOS and Ubuntu. I don't have windows computer, but I guess that may also have the same issue. Some related issues can be found in this issue vscode-python #4668.
Now I'm trying to fix this issue with a pull request. But I need some help to indicate me to the correct location in the code base. I also addressed this question on StackOverflow:
Through my own observation, I found the root cause may be: after some steps, somewhere during the create terminal process, the actual environment variable values(specific to PATH) has been reordered/changed. Maybe it is being processed for remove duplication.
I download the vscode codebase(1.33) and follow the debugging until method createProcess in source file src/vs/workbench/contrib/terminal/electron-browser/terminalProcessManager.ts. What I have found is: even after the terminal instance has been created, the env.PATH in the main process has NOT been changed. For ptyProcess, the constructor takes options(which contains env) as input, but I cannot find any env related property in created ptyProcess. And after the terminal instance created, the debugging ends, integrated terminal appears in the workbench. I get lost after terminal instance created, can someone from the core team or used to working on integrated terminal module indicate me to the right place in the codebase so I can try to solve the issue? There is not enough documents on internet or comments in the source code can help me find/locate the environments changing during/after the terminal instance creation.
Activity
praenubilus commentedon Mar 11, 2019
Also, if I want to debug vscode platform source code, where should I expect a User Settings or Workspace Settings so I can add some customized stuff in it?
DonJayamanne commentedon Mar 13, 2019
Current workaround is:
Tyriar commentedon Mar 13, 2019
The reason this happens is because on mac and Linux there are two ways to launch VS Code:
$SHELL -ilcto source the "development environment"This sets up the main process environment which is inherited by each VS Code window, its tasks, debug sessions and terminals. Below explain this in detail:
Ubuntu launching from terminal
~/.bash_profile)~/.bashrc)~/.bashrc)Ubuntu launching from dash
~/.bash_profile)~/.bash_profilebecauseVSCODE_CLIis not set)~/.bashrc)macOS launching from terminal
~/.bash_profile)~/.bash_profile)macOS launching from dock
~/.bash_profilebecauseVSCODE_CLIis not set)~/.bash_profile)* needs
~/.bashrcalready sourced** needs
~/.bash_profilealready sourcedThe underlying problem is:
So as for fixing this issue once and for all this will be a little tricky, unfortunately it's not just as easy as holding onto the original environment and using that for the terminal instead of the one generated by
getShellEnvironment, that will only work when launching VS Code from the dock/dash but not when run through the terminal.I propose we fetch the environment of the parent process of the
codeexecutable which will probably be systemd on Linux or the root process on macOS, we can do this on Linux by running and parsingcat /proc/<pid>/environon Linux, andps eww <pid>on macOS.It's worth noting that this could break some people's set up and potentially cause some other unexpected issues as before it was an assumption that the terminal process uses the environment of VS Code's window. Maybe this should even be an option in settings, something like
"terminal.integrated.useWindowEnvironment": false?[-]Inconsistent Environment Variable Values in Integrated Terminal[/-][+]Support using a "clean" environment for the terminal[/+]Tyriar commentedon Mar 13, 2019
YH (@praenubilus) if you wanted to put together a PR for this I would stick the fetching of the environment code insider electron-browser/terminalService and grab that inside TerminalProcessManager instead of basing it on
process.envhere:https://github.com/Microsoft/vscode/blob/d3443fd68d625c980325e1571066850e943dfbef/src/vs/workbench/contrib/terminal/browser/terminalProcessManager.ts#L128
I think for Windows we always want the old behavior.
praenubilus commentedon Mar 13, 2019
Daniel Imms (@Tyriar) Thanks for the information. Can you answer my another question: if I want to debug with customized settings, (for example "terminal.integrated.env.osx": { "PATH": ""} ) where should I expect a User Settings or Workspace Settings file I can play with?
Tyriar commentedon Mar 14, 2019
YH (@praenubilus) I haven't touched any debugging stuff with extensions, this might be relevant?
https://github.com/Microsoft/vscode/blob/1615a36eb45182de5e34c53cfdd5b3bce8cddbf3/src/vs/vscode.d.ts#L8484-L8500
auchenberg commentedon Mar 14, 2019
Ping Don Jayamanne (@DonJayamanne)
DonJayamanne commentedon Mar 18, 2019
bandrei commentedon Mar 19, 2019
Not sure if this helps with your assessment Daniel Imms (@Tyriar) however, my
bash_profilesources mybashrcfile. If VSCode is to load bash_profile in either scenarios (dock vs terminal), why is theVSCODE_CLIvariable not being set affect the execution order of the sourcing?22 remaining items
Get mac launching with mostly root env
ueoo commentedon Jun 14, 2019
After set
I can not build cpp file:

Tyriar commentedon Jun 14, 2019
Gerry (@hologerry) try setting
terminal.integrated.inheritEnvtofalseusing the Insiders build and let me know if that works.ueoo commentedon Jun 14, 2019
Daniel Imms (@Tyriar) With the Insiders build and setting
terminal.integrated.inheritEnvtofalse.It works! Thanks!
Tyriar commentedon Jun 17, 2019
While
inheritEnvfalse on macOS is better than the current workaround (setting PATH to""), I suspect that it probably isn't good enough of a fix to switch the default over as the initial PATH will be completely empty, so things likeshwon't be available (unless the login shell adds them).Tyriar commentedon Jun 17, 2019
Actually scratch that, I think that's how it's meant to be and it's the reason why the default shell always has the absolute path (
/bin/sh) instead of the shell name (sh). path_helper pulls the$PATHin from/etc/pathsvia/etc/profile.lwabish commentedon Jun 19, 2019
At first I used bash+vscode+conda,this problem appears and I fixed it by setting
"terminal.integrated.env.osx": {"PATH": ""}in vscode and it works.But this time I just change my main shell to zsh(also in vscode),this method has failed : vscode prompt something like "zsh -l can't start".
Daniel Imms (@Tyriar) I tried using vscode insider edition and set
"terminal.integrated.inheritEnv": false,,this worksieipi commentedon Jul 5, 2019
using vscode on mac and set "terminal.integrated.inheritEnv": false,,this works;
ladyrick commentedon Jul 16, 2019
But sometimes I really need to inheritEnv.
inheritEnv: false will make something goes wrong.
DonJayamanne commentedon Jul 18, 2019
Please could you provide more details on what this
somethingis.ladyrick commentedon Jul 19, 2019
In remote development, "inheritEnv: false" will make "$PATH" wrong.
Don't know why.
Maybe it's a bug of remote dev?
Tyriar commentedon Jul 19, 2019
ladyrick (@ladyrick) what's your OS/version?
praenubilus commentedon Jul 23, 2019
Can you add the following line at the end of your zshrc or bashrc?
with this line, every time integrated terminal will display PATH value if the script being sourced/run, if the terminal has been printed more than once, and it may help you to locate the problem.