wt.exe ignores environment of parent (calling) process #15026

Closed
opened 2026-01-31 04:26:24 +00:00 by claunia · 8 comments
Owner

Originally created by @rashil2000 on GitHub (Aug 31, 2021).

Windows Terminal version (or Windows build number)

10.0.19043.1165 1.10.1933.0

Other Software

No response

Steps to reproduce

  1. Open up cmd.exe (in conhost)
  2. Do set SOME_VAR=some_value
  3. Type conhost. In the newly launched window, check for SOME_VAR variable. It'll be set. Close this new window.
  4. In the original cmd.exe shell, type wt. In the newly launched window, check for SOME_VAR variable. It'll be absent.

Expected Behavior

I expect any new processes launched from the original shell to inherit the parent's environment.

Actual Behavior

The wt.exe executable is probably the first ever process I have seen (in my limited experience) that ignores the parent's environment. All other processes (including conhost) inherit the environment.

Note that I searched for similar issues, and found numerous ones pertaining to refreshing the environment variables automatically for new tabs/panes. This is not that.

There is actually an issue - #4259 - which slightly mentions this sort of thing, but again the issue title there is regarding panes and not the process in general.

Originally created by @rashil2000 on GitHub (Aug 31, 2021). ### Windows Terminal version (or Windows build number) 10.0.19043.1165 1.10.1933.0 ### Other Software _No response_ ### Steps to reproduce 1. Open up cmd.exe (in conhost) 2. Do `set SOME_VAR=some_value` 3. Type `conhost`. In the newly launched window, check for **SOME_VAR** variable. It'll be set. Close this new window. 4. In the original cmd.exe shell, type `wt`. In the newly launched window, check for **SOME_VAR** variable. It'll be absent. ### Expected Behavior I expect any new processes launched from the original shell to inherit the parent's environment. ### Actual Behavior The `wt.exe` executable is probably the first ever process I have seen (in my limited experience) that ignores the parent's environment. All other processes (including conhost) inherit the environment. Note that I searched for similar issues, and found numerous ones pertaining to refreshing the environment variables automatically for new tabs/panes. This is not that. There is actually an issue - #4259 - which slightly mentions this sort of thing, but again the issue title there is regarding panes and not the process in general.
claunia added the Needs-TriageNeeds-Tag-Fix labels 2026-01-31 04:26:25 +00:00
Author
Owner

@rashil2000 commented on GitHub (Sep 1, 2021):

On second thought, I now realize why this behaviour is by design. Each new window/tab/pane fetches the environment block from the system itself (the registry, perhaps?). If it were to inherit it from the parent, it won't have up to date values.

Is this reasoning correct? Please close this if it is 😅.

@rashil2000 commented on GitHub (Sep 1, 2021): On second thought, I now realize why this behaviour is _by design_. Each new window/tab/pane fetches the environment block from the system itself (the registry, perhaps?). If it were to inherit it from the parent, it won't have up to date values. Is this reasoning correct? Please close this if it is 😅.
Author
Owner

@zadjii-msft commented on GitHub (Sep 1, 2021):

Each new window/tab/pane fetches the environment block from the system

I believe it only does this on main - I don't think we actually shipped that code, because it broke... pwsh or scoop or something like that. #9741? #7418 maybe? @DHowett You remember that one better than me.

@zadjii-msft commented on GitHub (Sep 1, 2021): > Each new window/tab/pane fetches the environment block from the system I believe it only does this on `main` - I don't think we actually shipped that code, because it broke... pwsh or scoop or something like that. #9741? #7418 maybe? @DHowett You remember that one better than me.
Author
Owner

@DHowett commented on GitHub (Sep 1, 2021):

It's... complicated. @rashil2000, you're right that it keeps the environment up to date. However, it should try to merge the environment if possible. 😄

@DHowett commented on GitHub (Sep 1, 2021): It's... complicated. @rashil2000, you're right that it keeps the environment up to date. However, it should try to _merge_ the environment if possible. :smile:
Author
Owner

@rashil2000 commented on GitHub (Sep 1, 2021):

I'm curious to know what it'll do in case of "merge conflicts" 😅

Like if I set SOME_VAR in both the calling process and in the Start Menu->Environment Variables dialog.

@rashil2000 commented on GitHub (Sep 1, 2021): I'm curious to know what it'll do in case of "merge conflicts" 😅 Like if I set SOME_VAR in both the calling process and in the Start Menu->Environment Variables dialog.
Author
Owner

@eryksun commented on GitHub (Sep 2, 2021):

However, it should try to merge the environment if possible.

I'd be worried that automated merging could create an inconsistent state, with conflicting variables or values. I see no reason to deviate from Explorer's behavior in this regard. To that end, Terminal would listen for WM_SETTTINGCHANGE "Environment" window messages, for which it would reload the environment block that's used for new tabs. Maybe it could also support an OSC sequence that contains a serialized environment block, which sets a per-tab or per-pane environment for use when duplicating or splitting a tab.

@eryksun commented on GitHub (Sep 2, 2021): > However, it should try to merge the environment if possible. I'd be worried that automated merging could create an inconsistent state, with conflicting variables or values. I see no reason to deviate from Explorer's behavior in this regard. To that end, Terminal would listen for `WM_SETTTINGCHANGE` "Environment" window messages, for which it would reload the environment block that's used for new tabs. Maybe it could also support an OSC sequence that contains a serialized environment block, which sets a per-tab or per-pane environment for use when duplicating or splitting a tab.
Author
Owner

@jporkka commented on GitHub (Nov 17, 2021):

I second that thought -- it should not try to merge environment variables.
Each new tab just gets a fresh copy of the system environment after a WM_SETTINGCHANGE -- Just like Explorer does.

Merging will likely end badly - as there are complex scenarios, and it is unlikely that users would foresee this and will get surprised.

Maybe a cmdline option to control if it should listen for WM_SETTINGCHANGE? So if users really want the terminal to only inherit their env vars from the parent process they can get it.

@jporkka commented on GitHub (Nov 17, 2021): I second that thought -- it should not try to merge environment variables. Each new tab just gets a fresh copy of the system environment after a WM_SETTINGCHANGE -- Just like Explorer does. Merging will likely end badly - as there are complex scenarios, and it is unlikely that users would foresee this and will get surprised. Maybe a cmdline option to control if it should listen for WM_SETTINGCHANGE? So if users really want the terminal to only inherit their env vars from the parent process they can get it.
Author
Owner

@DHowett commented on GitHub (Jan 11, 2022):

Hey all! Thanks for the discussion here. We've decided to make the call:

We aren't going to merge environment variables, and applications spawned inside a Terminal will always have an "up to date" view of the user's environment. How we achieve this is up in the air (whether we can rely on RegenerateUserEnvironment or if we have to "bless" our own version of it moving forward), but given that Terminal (1) is multiprocess and (2) comes with a "remote" control frontend that can open new tabs in existing windows the ownership and the provenance of the environment was never a sure thing.

The best thing we can do is always give you a consistent new environment -- trying to guess which one you wanted and what you wanted from that one will result in us accidentally being wrong, rather than being consistent.

Thanks!

@DHowett commented on GitHub (Jan 11, 2022): Hey all! Thanks for the discussion here. We've decided to make the call: We aren't going to merge environment variables, and applications spawned inside a Terminal will always have an "up to date" view of the user's environment. How we achieve this is up in the air (whether we can rely on `RegenerateUserEnvironment` or if we have to "bless" our own version of it moving forward), but given that Terminal (1) is multiprocess and (2) comes with a "remote" control frontend that can _open new tabs in existing windows_ the ownership and the provenance of the environment was never a sure thing. The best thing we can do is always give you a consistent new environment -- trying to guess which one you wanted and what you wanted _from_ that one will result in us accidentally being wrong, rather than being consistent. Thanks!
Author
Owner

@DHowett commented on GitHub (Jan 11, 2022):

Please follow #1125 for future updates about this!

@DHowett commented on GitHub (Jan 11, 2022): Please follow #1125 for future updates about this!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#15026