WT_SESSION is not set immediately #20705

Closed
opened 2026-01-31 07:21:44 +00:00 by claunia · 2 comments
Owner

Originally created by @kaneryu on GitHub (Oct 21, 2023).

Windows Terminal version

1.18.1421.0

Windows build number

10.0.19042

Other Software

The code that is mentioned in the steps:

choice /M "Would you like to start WindowsTerminal?"
echo %WT_SESSION%
IF "%WT_SESSION%"=="%WT_SESSION%" (
IF %ERRORLEVEL% EQU 1 start %userprofile%\Downloads\terminal-1.18.1421.0\WindowsTerminal.exe && exit
)

cls && cls

Steps to reproduce

Note: This code is being run through the AutoRun reg key. It runs as soon as any conhost is opened.

Run the above code in WT
%WT_SESSION% Is not set

Expected Behavior

%WT_SESSION% Is set

Actual Behavior

%WT_SESSION% Is not set

I would assume that the code runs before anything WT related even starts up, because after selecting N and checking the value of %WT_SESSION%, it has been set.

I have seen other threads mentioning that %WT_SESSION% should not be used to check if you can use certain capabilities of the terminal, however, I just want to check if it's running, I don't care about the capabilities of the terminal. Is there another way to check this?

Originally created by @kaneryu on GitHub (Oct 21, 2023). ### Windows Terminal version 1.18.1421.0 ### Windows build number 10.0.19042 ### Other Software The code that is mentioned in the steps: choice /M "Would you like to start WindowsTerminal?" echo %WT_SESSION% IF "%WT_SESSION%"=="%WT_SESSION%" ( IF %ERRORLEVEL% EQU 1 start %userprofile%\Downloads\terminal-1.18.1421.0\WindowsTerminal.exe && exit ) cls && cls ### Steps to reproduce Note: This code is being run through the AutoRun reg key. It runs as soon as any conhost is opened. Run the above code in WT %WT_SESSION% Is not set ### Expected Behavior %WT_SESSION% Is set ### Actual Behavior %WT_SESSION% Is not set I would assume that the code runs _before_ anything WT related even starts up, because after selecting N and checking the value of %WT_SESSION%, it has been set. I have seen other threads mentioning that %WT_SESSION% should not be used to check if you can use certain capabilities of the terminal, however, I just want to check if it's running, I don't care about the capabilities of the terminal. Is there another way to check this?
claunia added the Needs-TriageIssue-BugNeeds-Attention labels 2026-01-31 07:21:44 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Oct 23, 2023):

I'm guessing this is something like #13006. You're starting some console process (cmd.exe, your batch script, whatever) directly, and then that's getting handed-off to the Windows Terminal (the configured default terminal) rather than conhost. Does that sound right/?

Terminal never has a chance to set WT_SESSION in that scenario - cmd.exe spawned and got its env vars before it knew it would be attached to a Terminal.

@zadjii-msft commented on GitHub (Oct 23, 2023): I'm guessing this is something like #13006. You're starting _some console process_ (cmd.exe, your batch script, whatever) directly, and then that's getting handed-off to the Windows Terminal (the configured default terminal) rather than conhost. Does that sound right/? Terminal never has a chance to set `WT_SESSION` in that scenario - `cmd.exe` spawned and got its env vars before it knew it would be attached to a Terminal.
Author
Owner

@kaneryu commented on GitHub (Oct 24, 2023):

Yes (however the handoff isn't automatic). The reason why i would want to be able to read WT session immediatly is to see if the current console is open in WT or not.
However I understand how the autorun would run it before the WT_SESSION was able to be set.
I'm going to close this issue, becauase I don't think i will end up doing it this way (i'll probably find another non-admin way to make WT the default terminal application) because the current method messes up with cmd windows that other applications open. Thank you for responding too this

@kaneryu commented on GitHub (Oct 24, 2023): Yes (however the handoff isn't automatic). The reason why i would want to be able to read WT session immediatly is to see if the current console is open in WT or not. However I understand how the autorun would run it before the WT_SESSION was able to be set. I'm going to close this issue, becauase I don't think i will end up doing it this way (i'll probably find another non-admin way to make WT the default terminal application) because the current method messes up with cmd windows that other applications open. Thank you for responding too this
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20705