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

set identifying environment variable for new connections #897

Merged
merged 5 commits into from
May 22, 2019
Merged

set identifying environment variable for new connections #897

merged 5 commits into from
May 22, 2019

Conversation

binarycrusader
Copy link
Member

@binarycrusader binarycrusader commented May 19, 2019

Summary of the Pull Request

Set a WT_SESSION environment variable to a unique guid on every new connection to allow shell consumers to detect Windows Terminal and uniquely identify the session.

References

Unknown.

PR Checklist

Detailed Description of the Pull Request / Additional comments

Set a new 'WT_SESSION' environment variable when creating new terminal connections to allow shells to detect a unique Windows Terminal session. The value of the variable is a stringified GUID as returned by
CoCreateGuid.

How verified:

  • "razzle" & vs debug build
  • runut
  • opencon
  • testcon
  • manual inspection

Uncertain about what tests to add and where; all of the existing ones passed.

I did some basic research to try to ensure that "WT_SESSION" is a "unique", non-conflicting environment variable and so shouldn't cause any problems with any existing programs. I'm certainly open to suggestions about the name though.

Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks really good

src/cascadia/TerminalConnection/ConhostConnection.cpp Outdated Show resolved Hide resolved
src/inc/conpty-universal.h Outdated Show resolved Hide resolved
src/inc/conpty-universal.h Outdated Show resolved Hide resolved
@ghost ghost added Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels May 20, 2019
Copy link
Contributor

@DHowett-MSFT DHowett-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really cool. Thanks for doing it!
I'm not 100% comfortable signing off, as I'd like @zadjii-msft and @adiviness to give it a twice-over, but I'm also not going to block.

src/cascadia/TerminalConnection/ConhostConnection.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalConnection/ConhostConnection.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalConnection/ConhostConnection.h Outdated Show resolved Hide resolved
src/inc/conpty-universal.h Outdated Show resolved Hide resolved
src/inc/conpty-universal.h Show resolved Hide resolved
src/inc/conpty-universal.h Show resolved Hide resolved
src/inc/conpty-universal.h Outdated Show resolved Hide resolved
src/inc/conpty-universal.h Outdated Show resolved Hide resolved
src/cascadia/TerminalConnection/ConhostConnection.h Outdated Show resolved Hide resolved
src/cascadia/TerminalConnection/ConhostConnection.h Outdated Show resolved Hide resolved
src/cascadia/TerminalConnection/ConhostConnection.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalConnection/ConhostConnection.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalConnection/ConhostConnection.cpp Outdated Show resolved Hide resolved
src/inc/conpty-universal.h Show resolved Hide resolved
src/inc/conpty-universal.h Outdated Show resolved Hide resolved
src/inc/conpty-universal.h Outdated Show resolved Hide resolved
src/inc/conpty-universal.h Show resolved Hide resolved
src/inc/conpty-universal.h Show resolved Hide resolved
@ghost ghost added Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels May 21, 2019
Set a new 'WT_SESSION' environment variable when creating new terminal
connections to allow shells to detect a unique Windows Terminal session.
The value of the variable is a stringified GUID as returned by
CoCreateGuid.

How verified:
- "razzle" & vs debug build
- runut
- manual inspection
* use Utils::GuidToString for guid stringification
* expose guid parameter in ITerminalConnection idl
- misc. review fixes
- throw if CreateConPty fails in ConhostConnection::Start
- apply [[nodiscard]] and noexcept in various places
@@ -57,27 +62,41 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation

void ConhostConnection::Start()
{
std::wstring cmdline = _commandline.c_str();
std::wstring cmdline{ _commandline.c_str() };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer std::wstring cmdline{ _commandline.begin(), _commandline.end() }; because then we get guarantees about the bounds o the string we're using to initialize and we don't need to dip into the behind-the-scenes data members of std::string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string has an explicit constructor from a T that has a data and size; maybe it can be used here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(std::wstring cmdline{_commandline})

@DHowett-MSFT
Copy link
Contributor

I am willing to push this with the nits outstanding. @adiviness, would you block on #897 (comment)?

@adiviness
Copy link
Contributor

naw, it's probably ok. We can always fix it later as well.

@adiviness adiviness merged commit 1d9cdb3 into microsoft:master May 22, 2019
@DHowett-MSFT
Copy link
Contributor

@binarycrusader thanks so much for doing this. 👍

@axelfontaine
Copy link

axelfontaine commented Feb 17, 2022

WT_SESSION also doesn't work reliably in Win11 x64 with Windows Terminal set as default.

To reproduce, press the Windows key to open start, then type cmd + enter

This now opens a Windows Terminal cmd.exe tab via %USER_PROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\System Tools\Command Prompt.lnk where WT_SESSION is not set.

HorlogeSkynet pushed a commit to HorlogeSkynet/archey4 that referenced this pull request Sep 18, 2022
Since v0.1.1431.0, Windows Terminal sets `WT_SESSION` environment
variable that we may use to detect it.

> See <microsoft/terminal#897>

`TERM_PROGRAM` is not supported (yet ?).

> See <microsoft/terminal#1828>
@sschuberth
Copy link

WT_SESSION also doesn't work reliably in Win11 x64 with Windows Terminal set as default.

Right 😢 Also see #13006.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider setting environment variable when Terminal hosts cmd / powershell
6 participants