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

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

Closed
alexmera88 opened this issue Dec 14, 2021 · 6 comments
Closed

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

alexmera88 opened this issue Dec 14, 2021 · 6 comments
Labels
Issue-Question For questions or discussion Product-Terminal The new Windows Terminal. Resolution-Answered Related to questions that have been answered Resolution-Duplicate There's another issue on the tracker that's pretty much the same thing.

Comments

@alexmera88
Copy link

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.

@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 Dec 14, 2021
@zadjii-msft
Copy link
Member

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 zadjii-msft added Issue-Question For questions or discussion Product-Terminal The new Windows Terminal. Resolution-Answered Related to questions that have been answered labels Dec 14, 2021
@eryksun
Copy link

eryksun commented 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.

@alexmera88
Copy link
Author

alexmera88 commented 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!

@eryksun
Copy link

eryksun commented 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.

@zadjii-msft
Copy link
Member

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

@ghost
Copy link

ghost commented 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 ghost added Resolution-Duplicate There's another issue on the tracker that's pretty much the same thing. and removed 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 Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question For questions or discussion Product-Terminal The new Windows Terminal. Resolution-Answered Related to questions that have been answered Resolution-Duplicate There's another issue on the tracker that's pretty much the same thing.
Projects
None yet
Development

No branches or pull requests

3 participants