Set global $PATH environment variable in VS Code

后端 未结 8 1862
温柔的废话
温柔的废话 2020-12-14 02:28

I\'m defining a custom $PATH environment variable in my ~/.bash_profile (on a Mac), like so:

PATH=\"$HOME/.cargo/bin:$PATH:$HOME/bi         


        
相关标签:
8条回答
  • 2020-12-14 02:44

    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.

    0 讨论(0)
  • 2020-12-14 02:47

    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 )

    0 讨论(0)
  • 2020-12-14 02:50

    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}"
    },
    
    0 讨论(0)
  • 2020-12-14 02:59

    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:

    1. 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"
      
    2. In VS Code, press ⇧⌘P and type install 'code' command if you haven't done so before.

    3. Quit VS Code.

    4. 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.

    5. 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.

    0 讨论(0)
  • 2020-12-14 03:03

    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.

    0 讨论(0)
  • 2020-12-14 03:06

    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:-}
    
    0 讨论(0)
提交回复
热议问题