Skip to content

Commit

Permalink
Add WT_PROFILE_ID to the environment of the spawned process (#4852)
Browse files Browse the repository at this point in the history
This commit adds a `WT_PROFILE_ID` environment variable, which contains
the guid of the active profile.

It also teaches ConptyConnection to take an environment map on creation.

We had to do a little manual jiggery with the WSLENV environment
variable as passed by the creator.

* [x] CLA signed
* [ ] Tests added/passed
* [ ] Requires documentation to be updated
* [x] I've discussed this with core contributors already.

Ran terminal, validated vars and translated paths under windows and WSL. 

References #4566 (this PR originally introduced WT_SETTINGS/DEFAULTS)
Closes #3589
  • Loading branch information
oising authored Apr 17, 2020
1 parent bceeb45 commit 38e7cb0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/actions/spell-check/dictionary/apis.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
IMap
ICustom
IObject
LCID
Expand Down
30 changes: 23 additions & 7 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "DebugTapConnection.h"

using namespace winrt;
using namespace winrt::Windows::Foundation::Collections;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Core;
using namespace winrt::Windows::System;
Expand Down Expand Up @@ -572,7 +573,13 @@ namespace winrt::TerminalApp::implementation
// TODO GH#4661: Replace this with directly using the AzCon when our VT is better
std::filesystem::path azBridgePath{ wil::GetModuleFileNameW<std::wstring>(nullptr) };
azBridgePath.replace_filename(L"TerminalAzBridge.exe");
connection = TerminalConnection::ConptyConnection(azBridgePath.wstring(), L".", L"Azure", settings.InitialRows(), settings.InitialCols(), winrt::guid());
connection = TerminalConnection::ConptyConnection(azBridgePath.wstring(),
L".",
L"Azure",
nullptr,
settings.InitialRows(),
settings.InitialCols(),
winrt::guid());
}

else if (profile->HasConnectionType() &&
Expand All @@ -583,12 +590,21 @@ namespace winrt::TerminalApp::implementation

else
{
auto conhostConn = TerminalConnection::ConptyConnection(settings.Commandline(),
settings.StartingDirectory(),
settings.StartingTitle(),
settings.InitialRows(),
settings.InitialCols(),
winrt::guid());
std::wstring guidWString = Utils::GuidToString(profileGuid);

StringMap envMap{};
envMap.Insert(L"WT_PROFILE_ID", guidWString);
envMap.Insert(L"WSLENV", L"WT_PROFILE_ID");

auto conhostConn = TerminalConnection::ConptyConnection(
settings.Commandline(),
settings.StartingDirectory(),
settings.StartingTitle(),
envMap.GetView(),
settings.InitialRows(),
settings.InitialCols(),
winrt::guid());

sessionGuid = conhostConn.Guid();
connection = conhostConn;
}
Expand Down
26 changes: 24 additions & 2 deletions src/cascadia/TerminalConnection/ConptyConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "ConptyConnection.g.cpp"

#include "../../types/inc/Utils.hpp"
#include "../../types/inc/utils.hpp"
#include "../../types/inc/Environment.hpp"
#include "LibraryResources.h"

Expand Down Expand Up @@ -110,9 +110,29 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
// Ensure every connection has the unique identifier in the environment.
environment.insert_or_assign(L"WT_SESSION", guidSubStr.data());

auto wslEnv = environment[L"WSLENV"]; // We always want to load something, even if it's blank.
if (_environment)
{
// add additional WT env vars like WT_SETTINGS, WT_DEFAULTS and WT_PROFILE_ID
for (auto item : _environment)
{
auto key = item.Key();
auto value = item.Value();

// avoid clobbering WSLENV
if (std::wstring_view{ key } == L"WSLENV")
{
auto current = environment[L"WSLENV"];
value = current + L":" + value;
}

environment.insert_or_assign(key.c_str(), value.c_str());
}
}

// WSLENV is a colon-delimited list of environment variables (+flags) that should appear inside WSL
// https://devblogs.microsoft.com/commandline/share-environment-vars-between-wsl-and-windows/

auto wslEnv = environment[L"WSLENV"];
wslEnv = L"WT_SESSION:" + wslEnv; // prepend WT_SESSION to make sure it's visible inside WSL.
environment.insert_or_assign(L"WSLENV", wslEnv);
}
Expand Down Expand Up @@ -173,6 +193,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
ConptyConnection::ConptyConnection(const hstring& commandline,
const hstring& startingDirectory,
const hstring& startingTitle,
const Windows::Foundation::Collections::IMapView<hstring, hstring>& environment,
const uint32_t initialRows,
const uint32_t initialCols,
const guid& initialGuid) :
Expand All @@ -181,6 +202,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
_commandline{ commandline },
_startingDirectory{ startingDirectory },
_startingTitle{ startingTitle },
_environment{ environment },
_guid{ initialGuid },
_u8State{},
_u16Str{},
Expand Down
10 changes: 9 additions & 1 deletion src/cascadia/TerminalConnection/ConptyConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
{
struct ConptyConnection : ConptyConnectionT<ConptyConnection>, ConnectionStateHolder<ConptyConnection>
{
ConptyConnection(const hstring& cmdline, const hstring& startingDirectory, const hstring& startingTitle, const uint32_t rows, const uint32_t cols, const guid& guid);
ConptyConnection(
const hstring& cmdline,
const hstring& startingDirectory,
const hstring& startingTitle,
const Windows::Foundation::Collections::IMapView<hstring, hstring>& environment,
const uint32_t rows,
const uint32_t cols,
const guid& guid);

void Start();
void WriteInput(hstring const& data);
Expand All @@ -40,6 +47,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
hstring _commandline;
hstring _startingDirectory;
hstring _startingTitle;
Windows::Foundation::Collections::IMapView<hstring, hstring> _environment;
guid _guid{}; // A unique session identifier for connected client
hstring _clientName{}; // The name of the process hosted by this ConPTY connection (as of launch).

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalConnection/ConptyConnection.idl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.Terminal.TerminalConnection
[default_interface]
runtimeclass ConptyConnection : ITerminalConnection
{
ConptyConnection(String cmdline, String startingDirectory, String startingTitle, UInt32 rows, UInt32 columns, Guid guid);
ConptyConnection(String cmdline, String startingDirectory, String startingTitle, IMapView<String,String> environment, UInt32 rows, UInt32 columns, Guid guid);
Guid Guid { get; };
};

Expand Down

0 comments on commit 38e7cb0

Please sign in to comment.