Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fresh environment block on new terminals in Windows #47816

Open
eslachance opened this issue Apr 13, 2018 · 52 comments
Open

Use fresh environment block on new terminals in Windows #47816

eslachance opened this issue Apr 13, 2018 · 52 comments
Assignees
Labels
feature-request Request for new features or functionality terminal-process Problems launching processes, managing ptys, exiting, process leaks, etc. windows VS Code on Windows issues
Milestone

Comments

@eslachance
Copy link

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.

@vscodebot vscodebot bot added the terminal Integrated terminal issues label Apr 13, 2018
@MirzaSikander
Copy link

MirzaSikander commented Apr 19, 2018

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.

@MirzaSikander
Copy link

Actually it is stuck in a weird state. Two instances have different values for the same variable. One is updated while the other is not. Not sure how to reproduce it. I think it is somehow tied to different workspaces. It would be great if somebody could shine a light on how vscode powershell integrated terminal fetches the environment variable.

@MirzaSikander
Copy link

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.

@Tyriar
Copy link
Member

Tyriar commented May 15, 2018

npm i -g <something>

@eslachance are you on Windows? Isn't the global node_modules bin directory added to PATH, I would have expected this to work?

@Tyriar Tyriar added feature-request Request for new features or functionality info-needed Issue requires more information from poster labels May 15, 2018
@eslachance
Copy link
Author

I believe what I added was a global module such as eslint or it might have been windows-built-tools which of course does more than just copy javascript to the node modules. However, this does not simply affect node. This affects any and all things that you can do that adds something to the path, or does anything to modify what's accessible from the console. So even though my request stems from using npm i -g, the actual issue is broader and the feature request would definitely enhance the user experience for a lot of people (in my humble opinions).

@Tyriar Tyriar removed the info-needed Issue requires more information from poster label May 15, 2018
@Tyriar Tyriar added this to the Backlog milestone May 15, 2018
@claudeleveille
Copy link

claudeleveille commented Jun 5, 2018

Git also seems to be affected by this. Specifically, the ssh-agent included in Git for Windows. This program relies on environment variables to work, so when VS Code is started before the ssh-agent program, VS Code's Git integration can never find out about the ssh-agent process and make use of the SSH keys stored in there. VS Code as a whole should periodically refresh it's environment variables to fix all of the issues mentioned here, and possibly also as a response to certain events. A rejected Git push/pull based on a failed publickey authentication attempt comes to mind as a good idea for the first event.

@jonfelske
Copy link

jonfelske commented Jun 19, 2018

One thing that worked for me ( on mac ) was to add the environment variables to the bash profile. That way whenever you restart vscode or open a new terminal instance anything in the profile is loaded.

jonfelske$ vi ~/.bash_profile
# Environment variable needed
export YOUR_ENV_VAR_NAME="some value here"

You can then either restart the terminal or use "source ~/.bash_profile" and the environment variable will be picked up. Hope that helps! :)

@claudeleveille
Copy link

What I ended up doing is installing PuTTY and running setx.exe GIT_SSH "C:\Program Files\PuTTY\plink.exe". Then I could load my keys into pagent and never had any other problems (except for not being able to accept host key fingerprints with Git). To trust host keys: echo 'y' | plink.exe <hostname-or-ip> does the trick in a script (though that's not super secure, I know).

If only we could just converge on a single SSH suite inside of Windows, whether that's the WSL one, or the native port. I think that even PowerShell has some SSH functionality built-into sessions now.

@nishant-labs
Copy link

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.

@ps2goat
Copy link

ps2goat commented Oct 5, 2018

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.

@Tanja-4732
Copy link

Is there any plan as of right now to add this functionality?

@IAmHopp
Copy link

IAmHopp commented Mar 12, 2019

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

Same here, it's been bugging me for a while as I exclusively open via the taskbar. Nicely spotted, @MirzaSikander. This issue is more critical imo, as it's hard to even zero in on what's happening and it doesn't seem like it's by design, as opposed to having to close/reopen.

@edwin-hendriks
Copy link

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.

@Tyriar
Copy link
Member

Tyriar commented Mar 23, 2019

This could probably be fixed by #70248 (comment)

I'll take a note on that calling out this specific issue.

@woutervugt
Copy link

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!

@eslachance
Copy link
Author

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

@woutervugt
Copy link

Thanks for your reply. I understand env vars are loaded at the Windows process level.
One thing that I am then really not understanding is how this keeps on happening even when I do close all windows. That is exactly the first thing that I do! (haven't checked process explorer). Of course hitting the cross to close a bunch of windows does not necessarily mean the process terminates. Is there anything in that area that you can think of that might cause a process to stay around for a bit in a headless fashion?
Also, how does terminating processes interact with recent projects? Would not expect there to be a relation between those.

Gonna test a bit and see what is happening!

@woutervugt
Copy link

Ok, this is just crazy
I have an environment variable with current value in VSCode being 'Ralph' (testing deployment for him,anyhoo)
So, while VSCode remains open with 1 window, I update the env var via the System properties of Windows. Changing the value from 'Ralph' to 'Ralph1'
Then, close VSCode
open vscode via Taskbar --> Recent Projects
open the POSH integrated terminal, and see what value it has.

And it was not Ralph, nor Ralph1 (but Wouter, which it was somewhere before I did deployments for eh Ralph).

So, now VSCode has a env var value that is not equal to what it was before I refreshed the process / window, and is also not equal to the actual value in Windows.

Must be me but this is just really unexpected behavior. It seems that somewhere the env vars are persisted. Might be the POSH plugin, which truth be told has its fair share of weirdness)

@woutervugt
Copy link

Made a screen recording file. If you want I can drop that somewhere.

Must be a dev if you find it funny that I actually have a screen recording tool I didn't know and then see that it creates MHT files. Want me to send the MHT file :) I swear it won't cause harm!! lol

@IAmHopp
Copy link

IAmHopp commented Apr 17, 2019

This issue is more critical imo, as it's hard to even zero in on what's happening and it doesn't seem like it's by design, as opposed to having to close/reopen.

Just reiterating here, as I think these last comments are added evidence, that the issue with opening via the taskbar should be prioritized over the need to close/reopen all terminal instances.

Regardless of one's thoughts in regards to closing/reopening, at least it makes some sort of sense. Projects opened via the taskbar not picking up new values is just confusing.

I probably wouldn't have figured that one out to this day if it wasn't for @MirzaSikander.

@Lunar2kPS
Copy link

None of the solutions here regarding restarting VS code, killing VS code, nor using File -> Close Folder / Open Recent help me.

I have to fully restart my computer, and it's a workflow killer :/
I would love this feature.

@cedric-booxi
Copy link

None of the solutions here regarding restarting VS code, killing VS code, nor using File -> Close Folder / Open Recent help me.

I have to fully restart my computer, and it's a workflow killer :/ I would love this feature.

One possible solution is to execute:

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

it at least helps you reload your path which is often the most important to make you powershell terminal workable

@Lunar2kPS
Copy link

@cedric-booxi Ah thanks, but I use the Git Bash terminal, so that PowerShell syntax doesn't work.
Is there a bash-equivalent? 😅

@Tyriar
Copy link
Member

Tyriar commented May 13, 2022

@2kCarlos the problem is likely because you didn't restart your terminal, if VS Code is completely closed it will inherit the environment of the parent process (git bash). Re-exporting $Path is an alternative to restarting the terminal completely.

@Lunar2kPS
Copy link

@Tyriar Oh strange, do you mean clicking the garbage bin icon next to my terminal and then creating a new one with the + button? (If so, I did that too)
image

@Tyriar
Copy link
Member

Tyriar commented May 13, 2022

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

@Lunar2kPS
Copy link

@Tyriar Ahhh that makes sense, thanks a lot for clarifying! :D That should probably work in the meantime.

@eslachance
Copy link
Author

eslachance commented Jan 25, 2023

Man you'd think someone would have figured this out, almost 5 years later...

@Tyriar
Copy link
Member

Tyriar commented Jan 25, 2023

@eslachance

image

@eslachance
Copy link
Author

eslachance commented Jan 25, 2023

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?

@Tyriar
Copy link
Member

Tyriar commented Jan 26, 2023

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

@tkntkn
Copy link

tkntkn commented Mar 4, 2023

@cedric-booxi Ah thanks, but I use the Git Bash terminal, so that PowerShell syntax doesn't work. Is there a bash-equivalent? 😅

You can call PowerShell command from git-bash like this.

export PATH=$PATH:$(powershell.exe invoke-command -scr "{[System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\") + \";\" + [System.Environment]::GetEnvironmentVariable(\"Path\", \"User\")}" | sed -e 's/\\/\//g'  -e 's/C:/\/c/g' -e 's/;/:/g')

@Chaoses-Ib
Copy link

There is now also a PowerShell package for refreshing environment variables: https://github.com/asheroto/Refresh-EnvironmentVariables

Install-Script Refresh-EnvironmentVariables -Force

Refresh-EnvironmentVariables

Patarimi added a commit to Patarimi/hades that referenced this issue Jul 7, 2024
@aarowman
Copy link

aarowman commented Jul 24, 2024

Are there any updates on this issue? It's been open for 6 years now. My issue is with ELECTRON_* variables. Any time I want to run a command in the terminal that needs one, it won't work because the variable is empty.

This is the real issue: #101575 (comment)

@user4302
Copy link

Are there any updates on this issue? It's been open for 6 years now. My issue is with ELECTRON_* variables. Any time I want to run a command in the terminal that needs one, it won't work because the variable is empty.

This is the real issue: #101575 (comment)

i used the solution mentioned in the comment right before yours, not sure if its applicable for you tho, seems like youre facing a different issue.

i use this after installing a specific package in a script that im using

Install-Script Refresh-EnvironmentVariables -Force -Scope CurrentUser
Refresh-EnvironmentVariables

@vtvy
Copy link

vtvy commented Aug 1, 2024

“Today, I encountered a situation where the VS Code terminal couldn’t read system environment variables because the vscode/bin path was set in my account environment. However, after changing it to the system environment path, everything worked fine.” 😊

@e1399579
Copy link

e1399579 commented Aug 5, 2024

2024.8.5 The problem remains
i've added variables to user and system, and restart vscode

@fariazz
Copy link

fariazz commented Aug 28, 2024

“Today, I encountered a situation where the VS Code terminal couldn’t read system environment variables because the vscode/bin

Where do you set this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality terminal-process Problems launching processes, managing ptys, exiting, process leaks, etc. windows VS Code on Windows issues
Projects
None yet
Development

No branches or pull requests