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

Fix winget after a call to winget settings export #2767

Merged
merged 8 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 5 additions & 3 deletions src/AppInstallerCLICore/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ namespace AppInstaller::CLI
std::string_view parent,
Command::Visibility visibility,
Settings::ExperimentalFeature::Feature feature,
Settings::TogglePolicy::Policy groupPolicy) :
m_name(name), m_aliases(std::move(aliases)), m_visibility(visibility), m_feature(feature), m_groupPolicy(groupPolicy)
Settings::TogglePolicy::Policy groupPolicy,
CommandOutputFlags outputFlags) :
m_name(name), m_aliases(std::move(aliases)), m_visibility(visibility), m_feature(feature), m_groupPolicy(groupPolicy), m_outputFlags(outputFlags)
{
if (!parent.empty())
{
Expand Down Expand Up @@ -933,7 +934,8 @@ namespace AppInstaller::CLI
{
try
{
if (!Settings::User().GetWarnings().empty())
if (!Settings::User().GetWarnings().empty() &&
!WI_IsFlagSet(command->GetOutputFlags(), CommandOutputFlags::IgnoreSettingsWarnings))
Copy link
Member

Choose a reason for hiding this comment

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

As discussed, I would think this would be better done using the output channels mechanism, but this is acceptable.

The nice thing about that mechanism being built out is that any error/exception could be output in a JSON format as well. But we have only so much time.

{
context.Reporter.Warn() << Resource::String::SettingsWarnings << std::endl;
}
Expand Down
37 changes: 29 additions & 8 deletions src/AppInstallerCLICore/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ namespace AppInstaller::CLI
std::vector<Utility::LocIndString> m_params;
};

// Flags to control the behavior of the command output.
enum class CommandOutputFlags : int
{
None = 0x0,
IgnoreSettingsWarnings = 0x1,
};

DEFINE_ENUM_FLAG_OPERATORS(CommandOutputFlags);

struct Command
{
// Controls the visibility of the field.
Expand All @@ -57,17 +66,27 @@ namespace AppInstaller::CLI

Command(std::string_view name, std::string_view parent) :
Command(name, {}, parent) {}
Command(std::string_view name,std::vector<std::string_view> aliases, std::string_view parent) :
Command(std::string_view name, std::vector<std::string_view> aliases, std::string_view parent) :
Command(name, aliases, parent, Settings::ExperimentalFeature::Feature::None) {}
Command(std::string_view name,std::vector<std::string_view> aliases, std::string_view parent, Command::Visibility visibility) :
Command(std::string_view name, std::string_view parent, CommandOutputFlags outputFlags) :
Command(name, {}, parent, Command::Visibility::Show, Settings::ExperimentalFeature::Feature::None, Settings::TogglePolicy::Policy::None, outputFlags) {}
Command(std::string_view name, std::vector<std::string_view> aliases, std::string_view parent, Command::Visibility visibility) :
Command(name, aliases, parent, visibility, Settings::ExperimentalFeature::Feature::None) {}
Command(std::string_view name,std::vector<std::string_view> aliases, std::string_view parent, Settings::ExperimentalFeature::Feature feature) :
Command(std::string_view name, std::vector<std::string_view> aliases, std::string_view parent, Settings::ExperimentalFeature::Feature feature) :
Command(name, aliases, parent, Command::Visibility::Show, feature) {}
Command(std::string_view name,std::vector<std::string_view> aliases, std::string_view parent, Settings::TogglePolicy::Policy groupPolicy) :
Command(name, aliases, parent, Command::Visibility::Show, Settings::ExperimentalFeature::Feature::None, groupPolicy) {}
Command(std::string_view name,std::vector<std::string_view> aliases, std::string_view parent, Command::Visibility visibility, Settings::ExperimentalFeature::Feature feature) :
Command(name, aliases, parent, visibility, feature, Settings::TogglePolicy::Policy::None) {}
Command(std::string_view name,std::vector<std::string_view> aliases, std::string_view parent, Command::Visibility visibility, Settings::ExperimentalFeature::Feature feature, Settings::TogglePolicy::Policy groupPolicy);
Command(std::string_view name, std::vector<std::string_view> aliases, std::string_view parent, Settings::TogglePolicy::Policy groupPolicy) :
Command(name, aliases, parent, Command::Visibility::Show, Settings::ExperimentalFeature::Feature::None, groupPolicy, CommandOutputFlags::None) {}
Command(std::string_view name, std::vector<std::string_view> aliases, std::string_view parent, Command::Visibility visibility, Settings::ExperimentalFeature::Feature feature) :
Command(name, aliases, parent, visibility, feature, Settings::TogglePolicy::Policy::None, CommandOutputFlags::None) {}

Command(std::string_view name,
std::vector<std::string_view> aliases,
std::string_view parent,
Command::Visibility visibility,
Settings::ExperimentalFeature::Feature feature,
Settings::TogglePolicy::Policy groupPolicy,
CommandOutputFlags outputFlags);

virtual ~Command() = default;

Command(const Command&) = default;
Expand All @@ -85,6 +104,7 @@ namespace AppInstaller::CLI
Command::Visibility GetVisibility() const;
Settings::ExperimentalFeature::Feature Feature() const { return m_feature; }
Settings::TogglePolicy::Policy GroupPolicy() const { return m_groupPolicy; }
CommandOutputFlags GetOutputFlags() const { return m_outputFlags; }

virtual std::vector<std::unique_ptr<Command>> GetCommands() const { return {}; }
virtual std::vector<Argument> GetArguments() const { return {}; }
Expand Down Expand Up @@ -118,6 +138,7 @@ namespace AppInstaller::CLI
Command::Visibility m_visibility;
Settings::ExperimentalFeature::Feature m_feature;
Settings::TogglePolicy::Policy m_groupPolicy;
CommandOutputFlags m_outputFlags;
};

template <typename Container>
Expand Down
2 changes: 1 addition & 1 deletion src/AppInstallerCLICore/Commands/SettingsCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace AppInstaller::CLI

struct SettingsExportCommand final : public Command
{
SettingsExportCommand(std::string_view parent) : Command("export", parent) {}
SettingsExportCommand(std::string_view parent) : Command("export", parent, CommandOutputFlags::IgnoreSettingsWarnings) {}

Resource::LocString ShortDescription() const override;
Resource::LocString LongDescription() const override;
Expand Down
2 changes: 1 addition & 1 deletion src/AppInstallerCLICore/Commands/SourceCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace AppInstaller::CLI

struct SourceExportCommand final : public Command
{
SourceExportCommand(std::string_view parent) : Command("export", parent) {}
SourceExportCommand(std::string_view parent) : Command("export", parent, CommandOutputFlags::IgnoreSettingsWarnings) {}

std::vector<Argument> GetArguments() const override;

Expand Down
3 changes: 3 additions & 0 deletions src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw
Original file line number Diff line number Diff line change
Expand Up @@ -1541,4 +1541,7 @@ Please specify one of them using the --source option to proceed.</value>
<data name="UserSettings" xml:space="preserve">
<value>User Settings</value>
</data>
<data name="SettingsWarningUsingDefault" xml:space="preserve">
<value>Settings file couldn't load. Using default values.</value>
</data>
</root>
16 changes: 12 additions & 4 deletions src/AppInstallerCLITests/UserSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,16 @@ TEST_CASE("SettingsPortablePackageUserRoot", "[settings]")
SECTION("Relative path")
{
DeleteUserSettingsFiles();
std::string_view json = R"({ "installBehavior": { "portablePackageUserRoot": %LOCALAPPDATA%/Portable/Root } })";
std::string_view json = R"({ "installBehavior": { "portablePackageUserRoot": "%LOCALAPPDATA%/Portable/Root" } })";
SetSetting(Stream::PrimaryUserSettings, json);
UserSettingsTest userSettingTest;

REQUIRE(userSettingTest.Get<Setting::PortablePackageUserRoot>().empty());
REQUIRE(userSettingTest.GetWarnings().size() == 1);

auto warnings = userSettingTest.GetWarnings();
REQUIRE(warnings.size() == 1);
REQUIRE(warnings[0].Message == AppInstaller::StringResource::String::SettingsWarningInvalidFieldValue);
REQUIRE(warnings[0].Path == ".installBehavior.portablePackageUserRoot");
}
SECTION("Valid path")
{
Expand All @@ -443,12 +447,16 @@ TEST_CASE("SettingsPortablePackageMachineRoot", "[settings]")
SECTION("Relative path")
{
DeleteUserSettingsFiles();
std::string_view json = R"({ "installBehavior": { "portablePackageMachineRoot": %LOCALAPPDATA%/Portable/Root } })";
std::string_view json = R"({ "installBehavior": { "portablePackageMachineRoot": "%LOCALAPPDATA%/Portable/Root" } })";
SetSetting(Stream::PrimaryUserSettings, json);
UserSettingsTest userSettingTest;

REQUIRE(userSettingTest.Get<Setting::PortablePackageMachineRoot>().empty());
REQUIRE(userSettingTest.GetWarnings().size() == 1);

auto warnings = userSettingTest.GetWarnings();
REQUIRE(warnings.size() == 1);
REQUIRE(warnings[0].Message == AppInstaller::StringResource::String::SettingsWarningInvalidFieldValue);
REQUIRE(warnings[0].Path == ".installBehavior.portablePackageMachineRoot");
}
SECTION("Valid path")
{
Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCommonCore/Public/winget/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace AppInstaller
WINGET_DEFINE_RESOURCE_STRINGID(SettingsWarningInvalidValueFromPolicy);
WINGET_DEFINE_RESOURCE_STRINGID(SettingsWarningLoadedBackupSettings);
WINGET_DEFINE_RESOURCE_STRINGID(SettingsWarningParseError);
WINGET_DEFINE_RESOURCE_STRINGID(SettingsWarningUsingDefault);
};
}

Expand Down
12 changes: 9 additions & 3 deletions src/AppInstallerCommonCore/UserSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ namespace AppInstaller::Settings
}
catch (const std::exception& e)
Copy link
Member

Choose a reason for hiding this comment

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

This exposes the scenario where both the normal and backup settings fail, which can result in no output to the user about settings load failure? I think it just means an else for when the backup settings fails to load (~line 520 now) where we put in a different warning about not being able to load any settings file.

{
AICLI_LOG(Core, Error, << "Failed to read " << setting.Name << "Reason: " << e.what());
AICLI_LOG(Core, Error, << "Failed to read " << setting.Name << "; Reason: " << e.what());
}
catch (...)
{
AICLI_LOG(Core, Error, << "Failed to read " << setting.Name << " Reason unknown.");
AICLI_LOG(Core, Error, << "Failed to read " << setting.Name << "; Reason unknown.");
}

return {};
Expand Down Expand Up @@ -520,7 +520,13 @@ namespace AppInstaller::Settings
}
else
{
AICLI_LOG(Core, Warning, << "Failed loading settings files.");
// Settings and back up didn't parse or exist. If they exist then warn the user.
auto settingsPath = Stream{ Stream::PrimaryUserSettings }.GetPath();
auto backupPath = Stream{ Stream::BackupUserSettings }.GetPath();
if (std::filesystem::exists(settingsPath) || std::filesystem::exists(backupPath))
{
m_warnings.emplace_back(StringResource::String::SettingsWarningUsingDefault);
}
}
}
}
Expand Down