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

wt ignore env-vars set in the script launching it, due to new "auto reload system env vars with new window / tab" #15430

Closed
eZioPan opened this issue May 25, 2023 · 6 comments
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting

Comments

@eZioPan
Copy link

eZioPan commented May 25, 2023

Update:

for anyone encounter the same problem:

I suggest to set envrionment variables in Windows Terminal, and do not set env vars in script and let wt inherit it.

If you use powershell script, here is my solution:

$pre_cmd_string = {
    # do common pre run setup, set env vars, run some commands, etc...
    # DO NOT WRITE ANY COMMENTS IN THIS BLOCK, AND REMEMBER REMOVE US TOO
}.ToString()

<# prepare first pane...#>

$first_cmd_string = $pre_cmd_string + "`n" + { <#per pane setup code#> }.ToString()
$first_cmd_bytes = [System.Text.Encoding]::Unicode.GetBytes( $first_cmd_string )
$first_cmd_enc = [Convert]::ToBase64String( $first_cmd_bytes )

<# ... and #>
<# prepare rest of panes as above...#>

<# launching wt and launching powershell with EncodedCommand parm #>

wt pwsh.exe -ExecutionPolicy ByPass -NoExit -EncodedCommand $first_cmd_enc

Windows Terminal version

1.18.1421.0

Windows build number

10.0.19045.0

Other Software

PowerShell 7.4

Steps to reproduce

  1. open powershell open a standalone powershell commandline window (not in wt window)
  2. custom a env var $Env:MY_VAR="MY_VAL"
  3. launch a new Windows Terminal wt
  4. in new window (running powershell), type echo $Env:MY_VAR
  5. Nothing show up

after I disable compatibility.reloadEnvironmentVariables in global settings.json, new wt window inherit env var set in powershell

Expected Behavior

No response

Actual Behavior

see above

@eZioPan eZioPan added Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels May 25, 2023
@eZioPan
Copy link
Author

eZioPan commented May 25, 2023

I might not think it's really a issue, but this change might break some script. Should the new "auto-reload system environment variable" feature set to be "opt-in", rather than "default-on"?

related change #14999

@ianjoneill
Copy link
Contributor

after I disable compatibility.reloadEnvironmentVariables in global settings.json, new wt window inherit env var set in powershell

I can't reproduce this behaviour:

ReloadEnvironmentVariables.mp4

I don't think #14999 is at fault here - I think it's likely #5000. As of #5000 there is one overarching WindowsTerminal.exe process that manages all of the windows, whereas in terminal 1.17 and before each window had it's own WindowsTerminal.exe process.

As of version 1.18, when compatibility.reloadEnvironmentVariables is set to false, it's going to be that single long-running WindowsTerminal.exe that has it's environment block copied and given to the new shell process. It's environment won't contain the MY_VAR variable.

image

In version 1.17, the new WindowsTerminal.exe that's spawned by running wt is going to be the one that has it's environment block copied and given to the new shell process - it's environment will contain the MY_VAR variable.

image

(Apologies for the screenshots being from different PCs running different Windows versions - it was easier for logistical reasons.)

@eZioPan
Copy link
Author

eZioPan commented May 26, 2023

thanks for reply!

I can confirm that no matter how compatibility.reloadEnvironmentVariables sets, launching a new wt in a wt does not inherit env var.

maybe I wasn't clear, on the 1st step of my first post, I do means open a powershell commandline itself, other than open wt and there is a powershell running on it.

mybe we find 2 different situations that have similar behavior?

@eZioPan
Copy link
Author

eZioPan commented May 26, 2023

this could be a nasty task, that wt might need to ask parent process that launching it: which env var has been modified in parent process, which one is modified outside of parent process, then merge into the possible result.

@ianjoneill
Copy link
Contributor

Ah, opening a powershell window directly in the Windows console host, I can reproduce this behaviour.

If in the same powershell window you run a tiny program that calls the RegenerateUserEnvironment() function in shell32.dll (that the environment variable reloading code in Windows Terminal is based on), MY_VAR isn't printed either.

image

@eZioPan
Copy link
Author

eZioPan commented May 26, 2023

so, would you consider make compatibility.reloadEnvironmentVariables also a per profile settings / an env var / wt commandline argument, so that users can easily disable or enable it per profile / per session?

i used to write a powershell script, doing some env modification(mostly add msys2 into PATH), then launching wt, with 4 split panes, each runs a msys2 binary.

If I can disable auto-reload with a env var / wt commandline argument, then I can just add another line in my script before launching wt, that my old script would just run fine. and I also will be benefit from new auto-reload mechanism with a interactive use case.

@eZioPan eZioPan closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting
Projects
None yet
Development

No branches or pull requests

2 participants