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

Allow inheriting env vars from wt again and other env var changes too #15897

Merged
merged 17 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2280,11 +2280,6 @@
"description": "When set to true, the background image for the currently focused profile is expanded to encompass the entire window, beneath other panes.",
"type": "boolean"
},
"compatibility.reloadEnvironmentVariables": {
"default": true,
"description": "When set to true, when opening a new tab or pane it will get reloaded environment variables.",
"type": "boolean"
},
"initialCols": {
"default": 120,
"description": "The number of columns displayed in the window upon first load. If \"launchMode\" is set to \"maximized\" (or \"maximizedFocus\"), this property is ignored.",
Expand Down Expand Up @@ -2533,6 +2528,11 @@
"null"
]
},
"compatibility.reloadEnvironmentVariables": {
"default": true,
"description": "When set to true, when opening a new tab or pane it will get reloaded environment variables.",
"type": "boolean"
},
"unfocusedAppearance": {
"$ref": "#/$defs/AppearanceConfig",
"description": "Sets the appearance of the terminal when it is unfocused.",
Expand Down
38 changes: 38 additions & 0 deletions src/cascadia/LocalTests_SettingsModel/DeserializationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ namespace SettingsModelLocalTests
TEST_METHOD(TestInheritedCommand);
TEST_METHOD(LoadFragmentsWithMultipleUpdates);

TEST_METHOD(MigrateReloadEnvVars);

private:
static winrt::com_ptr<implementation::CascadiaSettings> createSettings(const std::string_view& userJSON)
{
Expand Down Expand Up @@ -2020,4 +2022,40 @@ namespace SettingsModelLocalTests
// GH#12520: Fragments should be able to override the name of builtin profiles.
VERIFY_ARE_EQUAL(L"NewName", loader.userSettings.profiles[0]->Name());
}

void DeserializationTests::MigrateReloadEnvVars()
{
static constexpr std::string_view settings1Json{ R"(
{
"defaultProfile": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"compatibility.reloadEnvironmentVariables": false,
"profiles": [
{
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"historySize": 1,
"commandline": "cmd.exe"
}
],
"actions": [
{
"name": "foo",
"command": "closePane",
"keys": "ctrl+shift+w"
}
]
})" };

implementation::SettingsLoader loader{ settings1Json, DefaultJson };
loader.MergeInboxIntoUserSettings();
loader.FinalizeLayering();

VERIFY_IS_TRUE(loader.FixupUserSettings(), L"Validate that this will indicate we need to write them back to disk");

const auto settings = winrt::make_self<implementation::CascadiaSettings>(std::move(loader));

Log::Comment(L"Ensure that the profile defaults have the new setting added");
VERIFY_IS_TRUE(settings->ProfileDefaults().HasReloadEnvironmentVariables());
VERIFY_IS_FALSE(settings->ProfileDefaults().ReloadEnvironmentVariables());
}
}
113 changes: 113 additions & 0 deletions src/cascadia/LocalTests_SettingsModel/SerializationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ namespace SettingsModelLocalTests
TEST_METHOD(CascadiaSettings);
TEST_METHOD(LegacyFontSettings);

TEST_METHOD(RoundtripReloadEnvVars);
TEST_METHOD(DontRoundtripNoReloadEnvVars);

private:
// Method Description:
// - deserializes and reserializes a json string representing a settings object model of type T
Expand Down Expand Up @@ -513,4 +516,114 @@ namespace SettingsModelLocalTests

VERIFY_ARE_EQUAL(toString(jsonOutput), toString(result));
}

void SerializationTests::RoundtripReloadEnvVars()
{
static constexpr std::string_view oldSettingsJson{ R"(
{
"defaultProfile": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"compatibility.reloadEnvironmentVariables": false,
"profiles": [
{
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"historySize": 1,
"commandline": "cmd.exe"
}
],
"actions": [
{
"name": "foo",
"command": "closePane",
"keys": "ctrl+shift+w"
}
]
})" };

static constexpr std::string_view newSettingsJson{ R"(
{
"defaultProfile": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"profiles":
{
"defaults":
{
"compatibility.reloadEnvironmentVariables": false
},
"list":
[
{
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"historySize": 1,
"commandline": "cmd.exe"
}
]
},
"actions": [
{
"name": "foo",
"command": "closePane",
"keys": "ctrl+shift+w"
}
]
})" };

implementation::SettingsLoader oldLoader{ oldSettingsJson, DefaultJson };
oldLoader.MergeInboxIntoUserSettings();
oldLoader.FinalizeLayering();
VERIFY_IS_TRUE(oldLoader.FixupUserSettings(), L"Validate that this will indicate we need to write them back to disk");
const auto oldSettings = winrt::make_self<implementation::CascadiaSettings>(std::move(oldLoader));
const auto oldResult{ oldSettings->ToJson() };

implementation::SettingsLoader newLoader{ newSettingsJson, DefaultJson };
newLoader.MergeInboxIntoUserSettings();
newLoader.FinalizeLayering();
newLoader.FixupUserSettings();
const auto newSettings = winrt::make_self<implementation::CascadiaSettings>(std::move(newLoader));
const auto newResult{ newSettings->ToJson() };

VERIFY_ARE_EQUAL(toString(newResult), toString(oldResult));
}

void SerializationTests::DontRoundtripNoReloadEnvVars()
{
// Kinda like the above test, but confirming that _nothing_ happens if
// we don't have a setting to migrate.

static constexpr std::string_view oldSettingsJson{ R"(
{
"defaultProfile": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"profiles": [
{
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"historySize": 1,
"commandline": "cmd.exe"
}
],
"actions": [
{
"name": "foo",
"command": "closePane",
"keys": "ctrl+shift+w"
}
]
})" };

implementation::SettingsLoader oldLoader{ oldSettingsJson, DefaultJson };
oldLoader.MergeInboxIntoUserSettings();
oldLoader.FinalizeLayering();
oldLoader.FixupUserSettings();
const auto oldSettings = winrt::make_self<implementation::CascadiaSettings>(std::move(oldLoader));
const auto oldResult{ oldSettings->ToJson() };

Log::Comment(L"Now, create a _new_ settings object from the re-serialization of the first");
implementation::SettingsLoader newLoader{ toString(oldResult), DefaultJson };
newLoader.MergeInboxIntoUserSettings();
newLoader.FinalizeLayering();
newLoader.FixupUserSettings();
const auto newSettings = winrt::make_self<implementation::CascadiaSettings>(std::move(newLoader));
VERIFY_IS_FALSE(newSettings->ProfileDefaults().HasReloadEnvironmentVariables(),
L"Ensure that the new settings object didn't find a reloadEnvironmentVariables");
}
}
1 change: 1 addition & 0 deletions src/cascadia/LocalTests_SettingsModel/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Author(s):

// Manually include til after we include Windows.Foundation to give it winrt superpowers
#include "til.h"
#include <til/winrt.h>

// Common includes for most tests:
#include "../../inc/conattrs.hpp"
Expand Down
8 changes: 6 additions & 2 deletions src/cascadia/Remoting/CommandlineArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation

CommandlineArgs(const winrt::array_view<const winrt::hstring>& args,
winrt::hstring currentDirectory,
const uint32_t showWindowCommand) :
const uint32_t showWindowCommand,
winrt::hstring envString) :
_args{ args.begin(), args.end() },
_cwd{ currentDirectory },
_ShowWindowCommand{ showWindowCommand }
_ShowWindowCommand{ showWindowCommand },
CurrentEnvironment{ envString }
{
}

Expand All @@ -27,6 +29,8 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
void Commandline(const winrt::array_view<const winrt::hstring>& value);
winrt::com_array<winrt::hstring> Commandline();

til::property<winrt::hstring> CurrentEnvironment;

WINRT_PROPERTY(uint32_t, ShowWindowCommand, SW_NORMAL); // SW_NORMAL is 1, 0 is SW_HIDE

private:
Expand Down
4 changes: 3 additions & 1 deletion src/cascadia/Remoting/Monarch.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
_WindowName{ windowInfo.WindowName() },
_args{ command.Commandline() },
_CurrentDirectory{ command.CurrentDirectory() },
_ShowWindowCommand{ command.ShowWindowCommand() } {};
_ShowWindowCommand{ command.ShowWindowCommand() },
_CurrentEnvironment{ command.CurrentEnvironment() } {};

WindowRequestedArgs(const winrt::hstring& window, const winrt::hstring& content, const Windows::Foundation::IReference<Windows::Foundation::Rect>& bounds) :
_Id{ 0u },
Expand All @@ -68,6 +69,7 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
WINRT_PROPERTY(winrt::hstring, CurrentDirectory);
WINRT_PROPERTY(winrt::hstring, Content);
WINRT_PROPERTY(uint32_t, ShowWindowCommand, SW_NORMAL);
WINRT_PROPERTY(winrt::hstring, CurrentEnvironment);
WINRT_PROPERTY(Windows::Foundation::IReference<Windows::Foundation::Rect>, InitialBounds);

private:
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/Remoting/Monarch.idl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Microsoft.Terminal.Remoting
String[] Commandline { get; };
String CurrentDirectory { get; };
UInt32 ShowWindowCommand { get; };
String CurrentEnvironment { get; };

String Content { get; };
Windows.Foundation.IReference<Windows.Foundation.Rect> InitialBounds { get; };
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/Remoting/Peasant.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ namespace Microsoft.Terminal.Remoting
runtimeclass CommandlineArgs
{
CommandlineArgs();
CommandlineArgs(String[] args, String cwd, UInt32 showWindowCommand);
CommandlineArgs(String[] args, String cwd, UInt32 showWindowCommand, String env);

String[] Commandline { get; set; };
String CurrentDirectory { get; };
UInt32 ShowWindowCommand { get; };
String CurrentEnvironment { get; };
};

runtimeclass RenameRequestArgs
Expand Down
5 changes: 4 additions & 1 deletion src/cascadia/Remoting/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,10 @@ namespace winrt::Microsoft::Terminal::Remoting::implementation
// If the name wasn't specified, this will be an empty string.
p->WindowName(args.WindowName());

p->ExecuteCommandline(*winrt::make_self<CommandlineArgs>(args.Commandline(), args.CurrentDirectory(), args.ShowWindowCommand()));
p->ExecuteCommandline(*winrt::make_self<CommandlineArgs>(args.Commandline(),
args.CurrentDirectory(),
args.ShowWindowCommand(),
args.CurrentEnvironment()));

_monarch.AddPeasant(*p);

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/Remoting/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ TRACELOGGING_DECLARE_PROVIDER(g_hRemotingProvider);
#include "til.h"

#include <cppwinrt_utils.h>
#include <til/winrt.h>
15 changes: 14 additions & 1 deletion src/cascadia/TerminalApp/AppCommandlineArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,11 @@ void AppCommandlineArgs::_addNewTerminalArgs(AppCommandlineArgs::NewTerminalSubc

subcommand.appendCommandLineOption = subcommand.subcommand->add_flag("--appendCommandLine", _appendCommandLineOption, RS_A(L"CmdAppendCommandLineDesc"));

subcommand.inheritEnvOption = subcommand.subcommand->add_flag(
"--inheritEnvironment,!--reloadEnvironment",
_inheritEnvironment,
RS_A(L"CmdInheritEnvDesc"));

// Using positionals_at_end allows us to support "wt new-tab -d wsl -d Ubuntu"
// without CLI11 thinking that we've specified -d twice.
// There's an alternate construction where we make all subcommands "prefix commands",
Expand All @@ -592,7 +597,8 @@ NewTerminalArgs AppCommandlineArgs::_getNewTerminalArgs(AppCommandlineArgs::NewT
{
NewTerminalArgs args{};

if (!_commandline.empty())
const auto hasCommandline{ !_commandline.empty() };
if (hasCommandline)
{
std::ostringstream cmdlineBuffer;

Expand Down Expand Up @@ -662,6 +668,13 @@ NewTerminalArgs AppCommandlineArgs::_getNewTerminalArgs(AppCommandlineArgs::NewT
args.AppendCommandLine(_appendCommandLineOption);
}

bool inheritEnv = hasCommandline;
Copy link
Member Author

Choose a reason for hiding this comment

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

of interest: default to inherit if we do have a commandline. If the user passed the arg in manually, then use the arg.

if (*subcommand.inheritEnvOption)
{
inheritEnv = _inheritEnvironment;
}
args.ReloadEnvironmentVariables(!inheritEnv);

return args;
}

Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/AppCommandlineArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class TerminalApp::AppCommandlineArgs final
CLI::Option* suppressApplicationTitleOption;
CLI::Option* colorSchemeOption;
CLI::Option* appendCommandLineOption;
CLI::Option* inheritEnvOption;
};

struct NewPaneSubcommand : public NewTerminalSubcommand
Expand Down Expand Up @@ -100,6 +101,7 @@ class TerminalApp::AppCommandlineArgs final
std::string _startingTabColor;
std::string _startingColorScheme;
bool _suppressApplicationTitle{ false };
bool _inheritEnvironment{ false };

winrt::Microsoft::Terminal::Settings::Model::FocusDirection _moveFocusDirection{ winrt::Microsoft::Terminal::Settings::Model::FocusDirection::None };
winrt::Microsoft::Terminal::Settings::Model::FocusDirection _swapPaneDirection{ winrt::Microsoft::Terminal::Settings::Model::FocusDirection::None };
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@
<value>Open the tab with tabTitle overriding default title and suppressing title change messages from the application</value>
<comment>{Locked="\"tabTitle\""}</comment>
</data>
<data name="CmdInheritEnvDesc" xml:space="preserve">
<value>Inherit the terminal's own environment variables when creating the new tab or pane, rather than creating a fresh environment block. This defaults to set when a "command" is passed. </value>
<comment>{Locked="\"command\""}</comment>
</data>
<data name="CmdColorSchemeArgDesc" xml:space="preserve">
<value>Open the tab with the specified color scheme, instead of the profile's set "colorScheme"</value>
<comment>{Locked="\"colorScheme\""}</comment>
Expand Down
Loading