vscode: Use fresh environment block on new terminals in Windows

At the moment, if any environment variables change, or new “apps” are installed while VS Code is running, in order for those changes to be recognized VS Code needs to be closed and re-open.

For instance, when running npm i -g <something> or choco install <package> in another prompt or powershell window, those new commands are not recognized until the app is rebooted.

It would be great if adding a new terminal instance with + would load these new variables/apps/notsurewhatterminologytouse.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 99
  • Comments: 47 (10 by maintainers)

Most upvoted comments

Okay so I think I found two different issues.

  • You do need to kill all instances to refresh environment variables. This is easy to repro.
  1. Start 2 vscode instances pointing to different directories.
  2. Add a new env variable through System Properties > Environement variables called TEST
  3. Check $env:TEST in both instances. Result: Not updated.
  4. Restart one instance by killing it and then using Open folder to open the directory. Check again. Result: Not updated.
  5. Restart both instances by killing both and using Open folder. Check again. Result: Updated.
  • The second issue is that if you open your workspaces using links under Recent, you will find that even after restarting VSCode, the environment variables are not updated.
  1. Start one instance.
  2. Update env variable.
  3. Kill instance.
  4. Open VScode by right clicking on Task bar and selecting the folder from Recent Workspaces
  5. Check env variable. Result: Not updated.

Worse yet, if you have multiple instances of vscode running, all of them need to be killed and restarted for the environment variables to be updated for the sake of one window where you needed it.

I am a teacher on the Radboud University in the Netherlands and my students have a lot of problems with this issue. Most work on Windows 10 and for them the easiest work around is to restart their computers. Because this is counter intuitive, it often takes them a while to figure out how to solve it. So fixing this issue would be a great help to me and my students.

Since this is still open here is a trick to reload the path in powershell (integrated or not):

$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")

What worked for me was exiting and then relaunching the cmd/git bash to fetch the latest env vars and then run

code . 

so the process starts with these new env vars

Having to shut down and relaunch the editor is exactly what we don’t want to have to do, and the reason this feature request exists. I feel like you didn’t read, or misunderstood, this entire thread…

@woutervugt this isn’t about caching and it’s not a Vscode specific issue. The same thing happens if you have a command prompt or bash open, and change your environment variables - you have to close and reopen the prompt to see those changes. However, in the world of Vscode, all the terminals need to be restarted, which means all of the vscode instances, and that’s why a solution has to be provided by the team to reload them without a full restart.

So to be clear: the vscode team didn’t cause this issue, it’s inherent to all terminals, but we would welcome a solution from them with arms wide open.

What worked for me was exiting and then relaunching the cmd/git bash to fetch the latest env vars and then run

code . 

so the process starts with these new env vars

Can the in my opinion super weird behavior of caching environment variables but only when working with recent projects be disabled somehow? I have exactly 0 understanding of the need for doing this, but hey, lets say it is useful for ‘some scenarios’.

Being able to not have this super annoying behavior would be quite beneficial!

I have same issue on mac for .bash_profile changes to take effect, Either will have to restart whole vscode IDE or execute “source ~/.bash_profile”.

Apart from that I have issue with duplicated paths in PATH variable. Below code is in .bash_profile:-

MAVEN_HOME=/Users/<user>/apache-maven-3.5.2 TEST=/Users/<user>/test_1_0 PATH=$PATH:$MAVEN_HOME/bin:$TEST

When I echo in mac terminal I get <other paths>:/Users/<user>/apache-maven-3.5.2/bin:/Users/<user>/test_1_0

where as vscode integrated terminal outputs as <other paths>:/Users/<user>/apache-maven-3.5.2/bin:/Users/<user>/test_1_0:/Users/<user>/apache-maven-3.5.2/bin:/Users/<user>/test_1_0

Can you please resolve this.

Man you’d think someone would have figured this out, almost 5 years later…

What worked for me was exiting and then relaunching the cmd/git bash to fetch the latest env vars and then run

code . 

so the process starts with these new env vars

Thank you, this method worked for me.

My VSCode on Windows still doesn’t refresh the environment variables even after closing all the related windows.

Executing the command Code in PowerShell, the environment variables get refreshed, but after that, if you don’t run it that way, then it still doesn’t work, which is really frustrating.

However, this is a good way to avoid restarting the computer in many cases.

I’m still having this issue too. Driky’s workaround for powershell still works + anyone with Chocolatey installed can run refreshenv in command prompt (which calls this script)

Thanks, @MirzaSikander! I couldn’t figure out why the environment variable was hanging around. I was always re-opening the project via the recent options (either via taskbar or within vs code’s start page).

Opening the folder by using the ‘open folder’ option did indeed load the new environment variable values! This is important when switching your AWS_PROFILE, which is needed for IAM authentication to create a token for kubernetes.

@eslachance the issue with this one is I think we’ll need to write a new native node module that pulls the fresh environment block, and that’s not really my area of expertise (C++, Rust or native Windows programming). Of course it’s doable, but it would take a disproportionate amount of time to action relative to other issues. If we can do it reliably by shelling out to fetch the env block or via built-in node APIs this would be a lot easier to tackle.

Yes, I commend Microsoft on solving 143k issues, and being the top editor for developers. You ousted Atom, and I absolutely adore VSCode. Still, after 5 years, this issue is position 175-ish most upvoted open issues and top 100-ish most open commented, so I don’t feel it’s unreasonable for it to get some attention and love?

@2kCarlos I mean if you launch VS Code’s first window from git bash, git bash’s environment will be used as the base environment. And if you haven’t relaunched git bash after changing your environment it will be using a stale environment.

As a side-note (since I opened this feature request in the first place): if you think it’s not possible to modify env variables without restarting the entire app, please explain how CMDER does it (https://github.com/cmderdev/cmder). And also suggest a reasoning on why VSCode couldn’t just do the same. (edit: that sounded super condescending and angry - IT WASN’T MEANT TO! I was just preventively answering anyone that thought it wasn’t possible!)

I personally would be pretty happy with something like code --restart that would:

  1. Close the existing instance of vscode, but remember what windows were open
  2. Start a new instance
  3. Open those windows again

Failing that, it’d be nice to have code --quit or something like that, which would ask the existing instance to close, taking care to follow the principle of least surprise with respect to sequences such as the following:

code --quit
code .

That is, such sequences should first wait for the existing instance to quit, then fire up a new one. Note, however, that I’m not certain which of the above commands should actually do the waiting; the main point is that we really don’t want to open a window or editor in the instance that just started quitting.