From e354313dc4fc627a9dc57f549f6fe4add7b53c02 Mon Sep 17 00:00:00 2001 From: "Dustin L. Howett" Date: Mon, 10 Aug 2020 14:48:27 -0700 Subject: [PATCH] Resolve the default profile during defaults load, don't crash on launch (#7237) The "default profile as name" feature in 1.1 broke the loading of default settings, as we would never get to the validation phase where the default profile string was transformed into a guid. I moved knowledge of the "unparsed default profile" optional to the consumer so that we could make sure we only attempted to deserialize it once (and only if it was present.) Fixes #7236. * [x] Closes #7236 (cherry picked from commit c03677b0c9f5b9a0a56c9767c7e65e0a0e723dd7) --- src/cascadia/TerminalApp/CascadiaSettings.cpp | 9 ++++++--- .../TerminalApp/CascadiaSettingsSerialization.cpp | 6 +++--- src/cascadia/TerminalApp/GlobalAppSettings.cpp | 4 ++-- src/cascadia/TerminalApp/GlobalAppSettings.h | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/cascadia/TerminalApp/CascadiaSettings.cpp b/src/cascadia/TerminalApp/CascadiaSettings.cpp index 9c8c7091726..db3bb58792d 100644 --- a/src/cascadia/TerminalApp/CascadiaSettings.cpp +++ b/src/cascadia/TerminalApp/CascadiaSettings.cpp @@ -226,9 +226,12 @@ void CascadiaSettings::_ValidateProfilesHaveGuid() void CascadiaSettings::_ResolveDefaultProfile() { const auto unparsedDefaultProfile{ GlobalSettings().UnparsedDefaultProfile() }; - auto maybeParsedDefaultProfile{ _GetProfileGuidByName(unparsedDefaultProfile) }; - auto defaultProfileGuid{ Utils::CoalesceOptionals(maybeParsedDefaultProfile, GUID{}) }; - GlobalSettings().DefaultProfile(defaultProfileGuid); + if (unparsedDefaultProfile) + { + auto maybeParsedDefaultProfile{ _GetProfileGuidByName(unparsedDefaultProfile) }; + auto defaultProfileGuid{ Utils::CoalesceOptionals(maybeParsedDefaultProfile, GUID{}) }; + GlobalSettings().DefaultProfile(defaultProfileGuid); + } } // Method Description: diff --git a/src/cascadia/TerminalApp/CascadiaSettingsSerialization.cpp b/src/cascadia/TerminalApp/CascadiaSettingsSerialization.cpp index 2ed23f8c770..b6b7f2a34c1 100644 --- a/src/cascadia/TerminalApp/CascadiaSettingsSerialization.cpp +++ b/src/cascadia/TerminalApp/CascadiaSettingsSerialization.cpp @@ -61,7 +61,7 @@ std::unique_ptr CascadiaSettings::LoadAll() // GH 3588, we need this below to know if the user chose something that wasn't our default. // Collect it up here in case it gets modified by any of the other layers between now and when // the user's preferences are loaded and layered. - const auto hardcodedDefaultGuid = resultPtr->GlobalSettings().UnparsedDefaultProfile(); + const auto hardcodedDefaultGuid = resultPtr->GlobalSettings().DefaultProfile(); std::optional fileData = _ReadUserSettings(); const bool foundFile = fileData.has_value(); @@ -141,12 +141,11 @@ std::unique_ptr CascadiaSettings::LoadAll() // is a lot of computation we can skip if no one cares. if (TraceLoggingProviderEnabled(g_hTerminalAppProvider, 0, MICROSOFT_KEYWORD_MEASURES)) { - const auto hardcodedDefaultGuidAsGuid = Utils::GuidFromString(hardcodedDefaultGuid); const auto guid = resultPtr->GlobalSettings().DefaultProfile(); // Compare to the defaults.json one that we set on install. // If it's different, log what the user chose. - if (hardcodedDefaultGuidAsGuid != guid) + if (hardcodedDefaultGuid != guid) { TraceLoggingWrite( g_hTerminalAppProvider, // handle to TerminalApp tracelogging provider @@ -229,6 +228,7 @@ std::unique_ptr CascadiaSettings::LoadDefaults() // them from a file (and the potential that could fail) resultPtr->_ParseJsonString(DefaultJson, true); resultPtr->LayerJson(resultPtr->_defaultSettings); + resultPtr->_ResolveDefaultProfile(); return resultPtr; } diff --git a/src/cascadia/TerminalApp/GlobalAppSettings.cpp b/src/cascadia/TerminalApp/GlobalAppSettings.cpp index d16ba285024..be372c1d06f 100644 --- a/src/cascadia/TerminalApp/GlobalAppSettings.cpp +++ b/src/cascadia/TerminalApp/GlobalAppSettings.cpp @@ -104,9 +104,9 @@ GUID GlobalAppSettings::DefaultProfile() const return _defaultProfile; } -std::wstring GlobalAppSettings::UnparsedDefaultProfile() const +std::optional GlobalAppSettings::UnparsedDefaultProfile() const { - return _unparsedDefaultProfile.value(); + return _unparsedDefaultProfile; } AppKeyBindings GlobalAppSettings::GetKeybindings() const noexcept diff --git a/src/cascadia/TerminalApp/GlobalAppSettings.h b/src/cascadia/TerminalApp/GlobalAppSettings.h index bec6376d4de..bfcc32a6fac 100644 --- a/src/cascadia/TerminalApp/GlobalAppSettings.h +++ b/src/cascadia/TerminalApp/GlobalAppSettings.h @@ -58,7 +58,7 @@ class TerminalApp::GlobalAppSettings final // by higher layers in the app. void DefaultProfile(const GUID defaultProfile) noexcept; GUID DefaultProfile() const; - std::wstring UnparsedDefaultProfile() const; + std::optional UnparsedDefaultProfile() const; GETSET_PROPERTY(int32_t, InitialRows); // default value set in constructor GETSET_PROPERTY(int32_t, InitialCols); // default value set in constructor