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

Terminal not reading updated environment variables #8862

Closed
rashil2000 opened this issue Jan 23, 2021 · 8 comments
Closed

Terminal not reading updated environment variables #8862

rashil2000 opened this issue Jan 23, 2021 · 8 comments
Labels
Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Resolution-External For issues that are outside this codebase

Comments

@rashil2000
Copy link
Contributor

Environment

Windows build number: 10.0.19042.0
Windows Terminal version (if applicable): 1.4.3243.0

Steps to reproduce

Go to Start menu and Edit/Add any environment variables.

Expected behavior

Any shells launched through WT should have the updated environment variables.

Actual behavior

Nothing happens. Even if I restart all WT windows. I have to restart my PC for changes to reflect. I have tried both the Store versions and the GitHub releases.

Opening PowerShell or CMD from anywhere else works as expected, they have the updated variables.

@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Jan 23, 2021
@zadjii-msft
Copy link
Member

Well this is just the dickens, I can't seem to get this to repro:

8862-env-vars-001

@DHowett any ideas here? I'm at a loss

@rashil2000
Copy link
Contributor Author

HI @zadjii-msft!

I would like to apologize for taking up your time.
I found the culprit - it was the SteelSeries Engine application.
I'll write the cause here in case (although very unlikely) encounters this.

You see, SteelSeries is a company that makes gaming peripherals. I have a keyboard with some extra hotkeys - that I can assign to launch any apps with a single key. The SteelSeries Engine is used to manage/run the keyboard. I have assigned various keys to run different Windows Terminal profiles (CMD, PowerShell, WSL etc.). Since the processes launched by this Engine are children of the Engine itself, the processes won't have updated environment variables until and unless the Engine itself is restarted (so that the Engine itself runs with up to date variables in environment).

I'll close this now.

@zadjii-msft
Copy link
Member

Interesting! Thanks for following up, I haven't heard of that before so it'll be good to keep my eye out for something like this in the future.

@zadjii-msft zadjii-msft added the Resolution-External For issues that are outside this codebase label Jan 25, 2021
@LeoDupont
Copy link

Thanks for the details @rashil2000, it helped me!
I have the same problem with Autohotkey: when opening WT with a hotkey from a forever-running AHK script, the environment variables are not reloaded.

@rashil2000
Copy link
Contributor Author

Welcome @LeoDupont! Glad to see another member of AutoHotKey gang. I switched to shortly thereafter (I have Windows key + Enter set to launch wt.exe) as it removed dependence on external keyboard.

@totkeks
Copy link

totkeks commented Jul 12, 2021

Very interesting! This helped me too. I use the Microsoft Mouse and Keyboard center to configure the launch hotkeys (1, 2 and 3) and had the same issue, when running windows terminal through it.

@bestK
Copy link

bestK commented Oct 28, 2022


#`::
RefreshEnvironment()
Run C:\Users\WIN10\AppData\Local\Microsoft\WindowsApps\wt.exe

 RefreshEnvironment()
{
	Path := ""
	PathExt := ""
	RegKeys := "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment,HKCU\Environment"
	Loop, Parse, RegKeys, CSV
	{
		Loop, Reg, %A_LoopField%, V
		{
			RegRead, Value
			If (A_LoopRegType == "REG_EXPAND_SZ" && !ExpandEnvironmentStrings(Value))
				Continue
			If (A_LoopRegName = "PATH")
				Path .= Value . ";" 
			Else If (A_LoopRegName = "PATHEXT")
				PathExt .= Value . ";"
			Else 
				EnvSet, %A_LoopRegName%, %Value%
		}
	}
	EnvSet, PATH, %Path%
	EnvSet, PATHEXT, %PathExt%
}


ExpandEnvironmentStrings(ByRef vInputString)
{
	; get the required size for the expanded string
	vSizeNeeded := DllCall("ExpandEnvironmentStrings", "Str", vInputString, "Int", 0, "Int", 0)
	If (vSizeNeeded == "" || vSizeNeeded <= 0)
		return False ; unable to get the size for the expanded string for some reason
	
	vByteSize := vSizeNeeded + 1
	If (A_PtrSize == 8) { ; Only 64-Bit builds of AHK_L will return 8, all others will be 4 or blank
		vByteSize *= 2 ; need to expand to wide character sizes
	}
	VarSetCapacity(vTempValue, vByteSize, 0)
	
	; attempt to expand the environment string
	If (!DllCall("ExpandEnvironmentStrings", "Str", vInputString, "Str", vTempValue, "Int", vSizeNeeded))
		return False ; unable to expand the environment string
	vInputString := vTempValue
	
	; return success
	Return True
}

from https://www.autohotkey.com/board/topic/63858-function-to-refresh-environment-variables/

@i-zanis
Copy link

i-zanis commented Jan 4, 2023

Thanks for the details @rashil2000, it helped me! I have the same problem with Autohotkey: when opening WT with a hotkey from a forever-running AHK script, the environment variables are not reloaded.

Thanks for providing this answer.
How does this even happen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Resolution-External For issues that are outside this codebase
Projects
None yet
Development

No branches or pull requests

6 participants