Environment variables not updated when creating a new tab. #16121

Closed
opened 2026-01-31 04:58:07 +00:00 by claunia · 6 comments
Owner

Originally created by @alexmera88 on GitHub (Dec 14, 2021).

Windows Terminal version

1.11.2921.0

Windows build number

10.0.19042.1348

Other Software

No response

Steps to reproduce

  1. Open a Command Prompt tab (it also occurs with a Powershell tab).
  2. Type "SET" to display environment variables.
  3. Create or edit any Environment Variable
  4. Open a new Command Prompt tab.
  5. Type "SET" to display environment variables.

Expected Behavior

The environment variable value displayed in the tab opened AFTER creating/editing the variable is the current one.

Actual Behavior

The environment variable value displayed in the tab opened AFTER creating/editing the variable is NOT the current one.

Originally created by @alexmera88 on GitHub (Dec 14, 2021). ### Windows Terminal version 1.11.2921.0 ### Windows build number 10.0.19042.1348 ### Other Software _No response_ ### Steps to reproduce 1. Open a Command Prompt tab (it also occurs with a Powershell tab). 2. Type "SET" to display environment variables. 3. Create or edit any Environment Variable 4. Open a new Command Prompt tab. 5. Type "SET" to display environment variables. ### Expected Behavior The environment variable value displayed in the tab opened AFTER creating/editing the variable is the current one. ### Actual Behavior The environment variable value displayed in the tab opened AFTER creating/editing the variable is NOT the current one.
Author
Owner

@zadjii-msft commented on GitHub (Dec 14, 2021):

SET in cmd.exe only applies to the current session, always. What you're doing is basically the same as:

  1. Open conhost running cmd
  2. use set to set some variable
  3. open a new conhost running cmd via the start menu or run dialog

In this example, you'd expect the variables set in 1 wouldn't apply to the cmd spawned in 3. That's basically what's happening here in the Terminal.

SETX is the command you can use in cmd to update the environment not just for the current process, but the block that's used for future launches as well. However, that's sorta gonna run into a similar issue, because the Terminal doesn't (yet) reload environment variables while it's running. There's more discussion about this in #1125, an issue we're still sorting out the right way of handling. There's also #2785 tracking adding custom environment blocks to the profile. (that's related, but maybe not relevant). Does that make more sense now?

@zadjii-msft commented on GitHub (Dec 14, 2021): `SET` in cmd.exe only applies to the current session, always. What you're doing is basically the same as: 1. Open `conhost` running `cmd` 2. use `set` to set some variable 3. open a new `conhost` running `cmd` via the start menu or run dialog In this example, you'd expect the variables set in 1 wouldn't apply to the cmd spawned in 3. That's basically what's happening here in the Terminal. `SETX` is the command you can use in cmd to update the environment not just for the current process, but the block that's used for future launches as well. However, that's sorta gonna run into a similar issue, because the Terminal doesn't (yet) reload environment variables while it's running. There's more discussion about this in #1125, an issue we're still sorting out the right way of handling. There's also #2785 tracking adding custom environment blocks to the profile. (that's related, but maybe not relevant). Does that make more sense now?
Author
Owner

@eryksun commented on GitHub (Dec 14, 2021):

However, that's sorta gonna run into a similar issue, because the Terminal doesn't (yet) reload environment variables while it's running.

Terminal already calls CreateEnvironmentBlock() for new tabs. But problems have surfaced due to the order in which this API function loads the environment, which is inconsistent with the private RegenerateUserEnvironment() function that the shell uses. I think the private and public implementation should be made consistent by adding CreateEnvironmentBlock2() or RegenerateUserEnvironment() to the public API.


Any mention of setx.exe has to be accompanied by a disclaimer that it should never be used on its own to modify the system or per-user "PATH". It can be combined with reg.exe query to get the original system or user value of "PATH", but it's difficult to do if you're not experienced with batch scripting.

Using PowerShell is easier, but if "PATH" is a REG_EXPAND_SZ value that uses percent variables, use the registry provider to read the original value without expansion instead of using [System.Environment]::GetEnvironmentVariable(name, target). The latter should be avoided because it expands the percent variables based on the current environment.

@eryksun commented on GitHub (Dec 14, 2021): > However, that's sorta gonna run into a similar issue, because the Terminal doesn't (yet) reload environment variables while it's running. Terminal already calls `CreateEnvironmentBlock()` for new tabs. But problems have surfaced due to the order in which this API function loads the environment, which is inconsistent with the private `RegenerateUserEnvironment()` function that the shell uses. I think the private and public implementation should be made consistent by adding `CreateEnvironmentBlock2()` or `RegenerateUserEnvironment()` to the public API. --- Any mention of setx.exe has to be accompanied by a disclaimer that it should never be used *on its own* to modify the system or per-user "PATH". It can be combined with `reg.exe query` to get the original system or user value of "PATH", but it's difficult to do if you're not experienced with batch scripting. Using PowerShell is easier, but if "PATH" is a `REG_EXPAND_SZ` value that uses percent variables, use the registry provider to read the original value without expansion instead of using `[System.Environment]::GetEnvironmentVariable(name, target)`. The latter should be avoided because it expands the percent variables based on the current environment.
Author
Owner

@alexmera88 commented on GitHub (Dec 15, 2021):

Hi @zadjii-msft, I understood your nice explanation, that's correct. But I probably didn't explain the issue well enough: I'm using the SET command just to dump the environment variables in the terminal, not to modify actual variables.

The environment variables are modified in Windows's "Environment Variables" dialog. And once these variables are modified, I open a new command prompt tab.

I expect the new command prompt tab to inherit its environment variables from the current state, but changes that occurred since the terminal was started are ignored.

Anyway, you mentioned issue #1125, it looks exactly like my scenario. Good to know you have this under your radar, thanks a lot Mike!

@alexmera88 commented on GitHub (Dec 15, 2021): Hi @zadjii-msft, I understood your nice explanation, that's correct. But I probably didn't explain the issue well enough: I'm using the `SET` command just to dump the environment variables in the terminal, not to modify actual variables. The environment variables are modified in Windows's "Environment Variables" dialog. And once these variables are modified, I open a new command prompt tab. I expect the new command prompt tab to inherit its environment variables from the current state, but changes that occurred since the terminal was started are ignored. Anyway, you mentioned issue #1125, it looks exactly like my scenario. Good to know you have this under your radar, thanks a lot Mike!
Author
Owner

@eryksun commented on GitHub (Dec 15, 2021):

Anyway, you mentioned issue #1125, it looks exactly like my scenario. Good to know you have this under your radar

Calling CreateEnvironmentBlock() for new tabs has been implemented for a while now, but it's only available in Windows Terminal Preview.

@eryksun commented on GitHub (Dec 15, 2021): > Anyway, you mentioned issue #1125, it looks exactly like my scenario. Good to know you have this under your radar Calling `CreateEnvironmentBlock()` for new tabs has been implemented for a while now, but it's only available in Windows Terminal Preview.
Author
Owner

@zadjii-msft commented on GitHub (Dec 15, 2021):

Oh okay yea, that's my bad. That's definitely #1125 (and the myriad of pain that's followed in the wake of #7243). Or at least, that's the thread we've generally been using to track that. Thanks!

/dup #1125

@zadjii-msft commented on GitHub (Dec 15, 2021): Oh okay yea, that's my bad. That's definitely #1125 (and the myriad of pain that's followed in the wake of #7243). Or at least, that's the thread we've generally been using to track that. Thanks! /dup #1125
Author
Owner

@ghost commented on GitHub (Dec 15, 2021):

Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!

@ghost commented on GitHub (Dec 15, 2021): Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#16121