Terminal preview does not accept environment variables from parent process #20025

Closed
opened 2026-01-31 07:01:05 +00:00 by claunia · 10 comments
Owner

Originally created by @ChrisKnue-MSFT on GitHub (Jun 1, 2023).

Windows Terminal version

1.18.1421.0

Windows build number

10.0.22631.0

Other Software

No response

Steps to reproduce

Create a batch file "test.bat" with contents

echo %TEST%
pause

open cmd terminal profile
run SET TEST=wow
run wt test.bat
run ./test.bat

Expected Behavior

New terminal window should open up with output (this is the behavior in 1.16)

>echo wow
wow
>pause
press any key to continue . . .

Then outputs in parent window

>echo wow
wow
>pause
press any key to continue . . .

Actual Behavior

New terminal window is opened with output

>echo
ECHO is on.
>pause
Press any key to continue . . .

Then outputs in parent window

>echo wow
wow
>pause
press any key to continue . . .
Originally created by @ChrisKnue-MSFT on GitHub (Jun 1, 2023). ### Windows Terminal version 1.18.1421.0 ### Windows build number 10.0.22631.0 ### Other Software _No response_ ### Steps to reproduce Create a batch file "test.bat" with contents ```bat echo %TEST% pause ``` open cmd terminal profile run `SET TEST=wow` run `wt test.bat` run `./test.bat` ### Expected Behavior New terminal window should open up with output (this is the behavior in 1.16) ``` >echo wow wow >pause press any key to continue . . . ``` Then outputs in parent window ``` >echo wow wow >pause press any key to continue . . . ``` ### Actual Behavior New terminal window is opened with output ``` >echo ECHO is on. >pause Press any key to continue . . . ``` Then outputs in parent window ``` >echo wow wow >pause press any key to continue . . . ```
Author
Owner

@DHowett commented on GitHub (Jun 1, 2023):

Ah, indeed!

With WT transitioning to a long-lived single process model like Explorer, we're evaluating a stance that all tabs and panes receive a completely new environment block on creation. That's the only way--apart from a risky three-way merge--to ensure that applications get up-to-date environment variables without requiring everything run through Terminal to actively reconstruct and merge its own envrionment. In short, inheritance is in tension with #1125 and #7239.

The move to that single-process model has made it somewhat difficult to maintain a chain of custody for the env. block in any way other than to construct a new one! There is probably something we could do with the wt remote control to send over environment variables that are "important," but that will require some doing.

@DHowett commented on GitHub (Jun 1, 2023): Ah, indeed! With WT transitioning to a long-lived single process model like Explorer, we're evaluating a stance that all tabs and panes receive a completely new environment block on creation. That's the only way--apart from a risky three-way merge--to ensure that applications get up-to-date environment variables without requiring everything run through Terminal to actively reconstruct and merge its own envrionment. In short, inheritance is in tension with #1125 and #7239. The move to that single-process model has made it somewhat difficult to maintain a chain of custody for the env. block in any way _other_ than to construct a new one! There is probably something we could do with the `wt` remote control to send over environment variables that are "important," but that will require some doing.
Author
Owner

@DHowett commented on GitHub (Jun 1, 2023):

This was already a bit "wacky" for window grouping, even in Preview 1.16 (and 1.15, 1.14, ...):

SET TEST=wow
wt -w 0 cmd

has different environment block behavior than...

SET TEST=wow
wt -w -1 cmd

Between the two, they are now at the very least consistent as of 1.18 😄

@DHowett commented on GitHub (Jun 1, 2023): This was already a bit "wacky" for window grouping, even in Preview 1.16 (and 1.15, 1.14, ...): ``` SET TEST=wow wt -w 0 cmd ``` has different environment block behavior than... ``` SET TEST=wow wt -w -1 cmd ``` Between the two, they are now at the very least _consistent_ as of 1.18 😄
Author
Owner

@carlos-zamora commented on GitHub (Jun 7, 2023):

@ChrisKnue-MSFT Thanks for filing! Going to close this based on Dustin's responses.

@carlos-zamora commented on GitHub (Jun 7, 2023): @ChrisKnue-MSFT Thanks for filing! Going to close this based on Dustin's responses.
Author
Owner

@timmydo commented on GitHub (Jun 7, 2023):

We have hundreds of developers relying on windows terminal for running about 6 tabs of applications to run our platform. Not propagating environment variables is a pretty large breaking change--I hope you will provide a workaround for passing environment variables to new tabs or at least give us some time to rearchitect things before the preview becomes the standard version. @DHowett

@timmydo commented on GitHub (Jun 7, 2023): We have hundreds of developers relying on windows terminal for running about 6 tabs of applications to run our platform. Not propagating environment variables is a pretty large breaking change--I hope you will provide a workaround for passing environment variables to new tabs or at least give us some time to rearchitect things before the preview becomes the standard version. @DHowett
Author
Owner

@DHowett commented on GitHub (Jun 16, 2023):

(I'm gonna reopen this and mark it for discussion among the team.)

There's roughly one quarter of a year until this ships broadly, which is the big win we get from having a preview channel 😄

@DHowett commented on GitHub (Jun 16, 2023): (I'm gonna reopen this and mark it for discussion among the team.) There's roughly one quarter of a year until this ships broadly, which is the big win we get from having a preview channel :smile:
Author
Owner

@zadjii-msft commented on GitHub (Jul 11, 2023):

quick notes

  • collect the env when wt is run. Package that up with the CommandlineArgs to send to the monarch
  • add a --flag to tell NewTab/SplitPane to use those env vars instead of the regenerated ones
  • don't just keep using those vars for future tabs/panes
@zadjii-msft commented on GitHub (Jul 11, 2023): quick notes * collect the env when `wt` is run. Package that up with the CommandlineArgs to send to the monarch * add a `--flag` to tell NewTab/SplitPane to use those env vars instead of the regenerated ones * don't just keep using those vars for future tabs/panes
Author
Owner

@DHowett commented on GitHub (Jul 11, 2023):

Maybe the --flag should be to disable inheritance

@DHowett commented on GitHub (Jul 11, 2023): Maybe the `--flag` should be to _disable_ inheritance
Author
Owner

@zadjii-msft commented on GitHub (Jul 13, 2023):

Argument for --flag disabling inheritance

  • this is how the wt CLI has always operated since inception in 0.9

Argument against:

  • Can we tell if wt was invoked for the commandline, vs just started by explorer.exe? There have been myriad issues in the past that our env vars are just plain wacky. I'd rather that a fresh "click the icon" launch not try to inherit, but idk if we can differentiate
@zadjii-msft commented on GitHub (Jul 13, 2023): Argument for `--flag` disabling inheritance * this is how the `wt` CLI has always operated since inception in 0.9 Argument against: * Can we tell if `wt` was invoked for the commandline, vs just _started by explorer.exe_? There have been myriad issues in the past that our env vars are just plain wacky. I'd rather that a fresh "click the icon" launch not try to inherit, but idk if we can differentiate
Author
Owner

@zadjii-msft commented on GitHub (Aug 24, 2023):

Notes from discussion:

We're gonna approach this as many bits:

  • wt -E foo=bar to manually pass env vars to the NewTerminalArgs. This'll let a caller manually pass env vars on the commandline. Great!
  • wt --inheritEnvironment
    • This one's more complicated.
    • On wt launch, bundle the entire environment that wt was spawned with, and put it into the Remoting.CommandlineArgs, and give them to the monarch (and ultimately, down to TerminalPage with the AppCommandlineArgs). DO THIS ALWAYS.
    • IF there was no overridden commandline, anywhere, in any subcommand, then we'll default to what we're doing in 1.18 Preview - don't inherit. Terminal act's like file explorer, and just starts up the profile with a fresh env block1 .
    • IF THERE WAS AN OVERRIDDEN commandline, then default this to "inherit from the caller of WT".
  • part the third: Convert the compatibility.reloadEnvironmentVariables setting to a per-profile one.

At the end of this:

  • wt, wt -p foo will still use a fresh env block, instead of the inherited one. Yay!
  • wt -- cmd, wt foo.bat, wt --inheritEnvironment will all use the inherited one. Just like in 1.17, yay.
  • set TEST=failure && wt -E TEST=wow cmd.exe /k echo such %TEST% will set TEST in the spawned cmd to wow, and that'll be a good boy.

working notes


image
image
image


Related:

  • #15487
    • TLDR: the virtual CWD is evaluated when the startupactions are evaluated, but not when the connection is created. So something like wt ./test.bat doesn't work, because CreateProcess("./test.bat") tries to load it from system32.

  1. Plus the environment setting, of course. ↩︎

@zadjii-msft commented on GitHub (Aug 24, 2023): Notes from discussion: We're gonna approach this as many bits: * `wt -E foo=bar` to manually pass env vars to the `NewTerminalArgs`. This'll let a caller _manually_ pass env vars on the commandline. Great! * `wt --inheritEnvironment` * This one's more complicated. * On `wt` launch, bundle the entire environment that `wt` was spawned with, and put it into the `Remoting.CommandlineArgs`, and give them to the monarch (and ultimately, down to `TerminalPage` with the `AppCommandlineArgs`). DO THIS ALWAYS. * IF there was _no_ overridden commandline, anywhere, in any subcommand, then we'll default to what we're doing in 1.18 Preview - _don't inherit_. Terminal act's like file explorer, and just starts up the profile with a fresh env block[^1]. * IF THERE WAS AN OVERRIDDEN commandline, then default this to "inherit from the caller of WT". * part the third: Convert the `compatibility.reloadEnvironmentVariables` setting to a per-profile one. At the end of this: * `wt`, `wt -p foo` will still use a fresh env block, instead of the inherited one. Yay! * `wt -- cmd`, `wt foo.bat`, `wt --inheritEnvironment` will all use the inherited one. Just like in 1.17, yay. * `set TEST=failure && wt -E TEST=wow cmd.exe /k echo such %TEST%` will set `TEST` in the spawned cmd to `wow`, and that'll be a good boy. --- _working notes_ * [`dev/migrie/f/--inherit-env-vars`](https://github.com/microsoft/terminal/compare/dev/migrie/f/--inherit-env-vars) * [ ] Don't forget that `_duplicateConnectionForRestart` should probably also use the same settings as the OG connection --- ![image](https://github.com/microsoft/terminal/assets/18356694/c4a28455-557a-409b-8d33-ec27777b5792) ![image](https://github.com/microsoft/terminal/assets/18356694/e045fe20-df04-445f-96bf-bdcf8460ffa6) ![image](https://github.com/microsoft/terminal/assets/18356694/8e2cf45b-1246-4062-8c91-8c381892d195) --- Related: * #15487 * TLDR: the virtual CWD is evaluated when the startupactions are evaluated, but not when the connection is created. So something like `wt ./test.bat` doesn't work, because `CreateProcess("./test.bat")` tries to load it from `system32`. [^1]: Plus the `environment` setting, of course.
Author
Owner

@zadjii-msft commented on GitHub (Sep 20, 2023):

Actually, y'know what. I'm gonna mark this as closed by #15897, and then fork off the -E request to it's own issue.

@zadjii-msft commented on GitHub (Sep 20, 2023): Actually, y'know what. I'm gonna mark this as closed by #15897, and then fork off the `-E` request to it's own issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20025