Skip to content

Support using a "clean" environment for the terminal #70248

Closed

Description

@praenubilus
  • VSCode Version: 1.30, 1.31, 1.33
  • OS Version: Ubuntu 18.04/ MacOS 10.14 Mojave

Steps to Reproduce:

  1. 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
  1. Open a vscode instance, in the integrated terminal type echo $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

  1. added
    terminal
    on Mar 11, 2019
  2. praenubilus commented on Mar 11, 2019

    @praenubilus
    Author

    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?

  3. DonJayamanne commented on Mar 13, 2019

    @DonJayamanne
    Contributor

    Current workaround is:

    "terminal.integrated.env.osx": {
    		"PATH": ""
    }
  4. Tyriar commented on Mar 13, 2019

    @Tyriar
    Contributor

    The reason this happens is because on mac and Linux there are two ways to launch VS Code:

    • From a terminal (ie. the environment has already been setup by running .bashrc/.bash_profile
    • From the "dash" in which case VS Code runs $SHELL -ilc to 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

    • Ubuntu (log in sources ~/.bash_profile)
      • Terminal (sources ~/.bashrc)
        • VS Code main process
          • VS Code renderer process*
            • VS Code terminal (sources ~/.bashrc)
            • VS Code task*
            • VS Code debug session*

    Ubuntu launching from dash

    • Ubuntu (log in sources ~/.bash_profile)
      • VS Code main process (sources ~/.bash_profile because VSCODE_CLI is not set)
        • VS Code renderer process*
          • VS Code terminal (sources ~/.bashrc)
          • VS Code task*
          • VS Code debug session*

    macOS launching from terminal

    • macOS
      • Terminal (sources ~/.bash_profile)
        • VS Code main process
          • VS Code renderer process**
            • VS Code terminal (sources ~/.bash_profile)
            • VS Code task**
            • VS Code debug session**

    macOS launching from dock

    • macOS
      • VS Code main process (sources ~/.bash_profile because VSCODE_CLI is not set)
        • VS Code renderer process**
          • VS Code terminal (sources ~/.bash_profile)
          • VS Code task**
          • VS Code debug session**

    * needs ~/.bashrc already sourced
    ** needs ~/.bash_profile already sourced

    The underlying problem is:

    When a terminal is launched, it needs to run its launch script again; .bashrc on Linux and .bash_profile on macOS (because terminal.integrated.osx.shellArgs defaults to [ "-l" ]). This will shuffle the environment based on what the user's scripts does, and can mess things up if their script is not idempotent.

    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 code executable which will probably be systemd on Linux or the root process on macOS, we can do this on Linux by running and parsing cat /proc/<pid>/environ on Linux, and ps 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?

  5. added
    feature-request
    on Mar 13, 2019
  6. added this to the Backlog milestone on Mar 13, 2019
  7. changed the title [-]Inconsistent Environment Variable Values in Integrated Terminal[/-] [+]Support using a "clean" environment for the terminal[/+] on Mar 13, 2019
  8. Tyriar commented on Mar 13, 2019

    @Tyriar
    Contributor

    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.env here:

    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.

  9. added
    help wanted
    on Mar 13, 2019
  10. praenubilus commented on Mar 13, 2019

    @praenubilus
    Author

    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?

  11. auchenberg commented on Mar 14, 2019

    @auchenberg
    Contributor
  12. DonJayamanne commented on Mar 18, 2019

    @DonJayamanne
  13. bandrei commented on Mar 19, 2019

    @bandrei

    Not sure if this helps with your assessment Daniel Imms (@Tyriar) however, my bash_profile sources my bashrc file. If VSCode is to load bash_profile in either scenarios (dock vs terminal), why is the VSCODE_CLI variable not being set affect the execution order of the sourcing?

  14. 22 remaining items

  15. ueoo commented on Jun 14, 2019

    @ueoo

    After set

    "terminal.integrated.env.osx": {
    		"PATH": ""
    }
    

    I can not build cpp file:
    Screen Shot 2019-06-15 at 00 29 29

  16. Tyriar commented on Jun 14, 2019

    @Tyriar
    Contributor

    Gerry (@hologerry) try setting terminal.integrated.inheritEnv to false using the Insiders build and let me know if that works.

  17. ueoo commented on Jun 14, 2019

    @ueoo

    Daniel Imms (@Tyriar) With the Insiders build and setting terminal.integrated.inheritEnv to false.
    It works! Thanks!

  18. Tyriar commented on Jun 17, 2019

    @Tyriar
    Contributor

    While inheritEnv false 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 like sh won't be available (unless the login shell adds them).

  19. Tyriar commented on Jun 17, 2019

    @Tyriar
    Contributor

    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 $PATH in from /etc/paths via /etc/profile.

  20. lwabish commented on Jun 19, 2019

    @lwabish

    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 works

  21. ieipi commented on Jul 5, 2019

    @ieipi

    using vscode on mac and set "terminal.integrated.inheritEnv": false,,this works;

  22. ladyrick commented on Jul 16, 2019

    @ladyrick

    using vscode on mac and set "terminal.integrated.inheritEnv": false,,this works;

    But sometimes I really need to inheritEnv.
    inheritEnv: false will make something goes wrong.

  23. DonJayamanne commented on Jul 18, 2019

    @DonJayamanne
    Contributor

    inheritEnv: false will make something goes wrong.

    Please could you provide more details on what this something is.

  24. ladyrick commented on Jul 19, 2019

    @ladyrick

    Please could you provide more details on what this something is.

    In remote development, "inheritEnv: false" will make "$PATH" wrong.
    Don't know why.
    Maybe it's a bug of remote dev?

  25. Tyriar commented on Jul 19, 2019

    @Tyriar
    Contributor

    ladyrick (@ladyrick) what's your OS/version?

  26. praenubilus commented on Jul 23, 2019

    @praenubilus
    Author

    Please could you provide more details on what this something is.

    In remote development, "inheritEnv: false" will make "$PATH" wrong.
    Don't know why.
    Maybe it's a bug of remote dev?

    Can you add the following line at the end of your zshrc or bashrc?

    echo $PATH

    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.

  27. locked and limited conversation to collaborators on Jul 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature-requesthelp wantedlinuxmacoson-testplanterminal

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions