Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

### Minor Bug Fixes

* Fixed `Assert-WinGetPackageManager` and `Repair-WinGetPackageManager` running a `winget.exe` from the current directory. They now only run `winget.exe` from the installed App Installer package, while still checking PATH and app-execution alias availability.
* Fixed PowerShell CLI-backed commands to consistently use the installed, Store-signed WinGet executable instead of searching the current directory or PATH. `Assert-WinGetPackageManager` and `Repair-WinGetPackageManager` still check PATH and app-execution alias availability.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void ResetAllSources()

private WinGetCLICommandResult Run(WinGetCLICommandBuilder builder, int timeOut = 60000)
{
var wingetCliWrapper = new WingetCLIWrapper(WingetCLIWrapper.WinGetFullPath);
var wingetCliWrapper = new WingetCLIWrapper();
var result = wingetCliWrapper.RunCommand(this, builder, timeOut);
result.VerifyExitCode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public UserSettingsCommand(PSCmdlet psCmdlet)
// Here would be "The specified method is not supported."
if (winGetSettingsFilePath == null)
{
var wingetCliWrapper = new WingetCLIWrapper(WingetCLIWrapper.WinGetFullPath);
var wingetCliWrapper = new WingetCLIWrapper();
var settingsResult = wingetCliWrapper.RunCommand(this, new WinGetCLICommandBuilder("settings").AppendSubCommand("export"));

// Read the user settings file property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace Microsoft.WinGet.Client.Engine.Common
/// </summary>
internal static class WinGetIntegrity
{
private const int ErrorFileNotFound = 2;

/// <summary>
/// Verifies winget runs correctly. If it doesn't, tries to find the reason why it failed.
/// </summary>
Expand All @@ -47,8 +45,8 @@ public static void AssertWinGet(PowerShellCmdlet pwshCmdlet, string expectedVers
try
{
// Use the installed package's winget instead of searching CWD or PATH.
wingetPath = GetWinGetPath(pwshCmdlet) ?? throw new Win32Exception(ErrorFileNotFound);
versionResult = WinGetVersion.RunWinGetVersionFromCLI(pwshCmdlet, wingetPath);
wingetPath = WingetCLIWrapper.WinGetFullPath;
versionResult = WinGetVersion.RunWinGetVersionFromCLI(pwshCmdlet);
versionResult.VerifyExitCode();
}
catch (Win32Exception e)
Expand Down Expand Up @@ -155,7 +153,7 @@ private static IntegrityCategory GetReason(PowerShellCmdlet pwshCmdlet, string?
}

// First lets check if the file is there, which means it is installed or someone is taking our place.
if (File.Exists(WingetCLIWrapper.WinGetFullPath))
if (File.Exists(WingetCLIWrapper.WinGetAliasPath))
{
var environmentCategory = GetEnvironmentIntegrityCategory(
Utilities.LocalDataWindowsAppPath,
Expand All @@ -177,11 +175,7 @@ private static IntegrityCategory GetReason(PowerShellCmdlet pwshCmdlet, string?
// It could be that AppInstaller package is old or the package is not
// registered at this point. To know that, call Get-AppxPackage.
var appxModule = new AppxModuleHelper(pwshCmdlet);
#if USE_PROD_CLSIDS
string? version = appxModule.GetStoreSignedAppInstallerPropertyValue("Version");
#else
string? version = appxModule.GetAppInstallerPropertyValue("Version");
#endif
if (version is null)
{
// This can happen in Windows Sandbox.
Expand All @@ -200,17 +194,6 @@ private static IntegrityCategory GetReason(PowerShellCmdlet pwshCmdlet, string?
return IntegrityCategory.AppInstallerNotRegistered;
}

private static string? GetWinGetPath(PowerShellCmdlet pwshCmdlet)
{
#if USE_PROD_CLSIDS
string? location = new AppxModuleHelper(pwshCmdlet).GetStoreSignedAppInstallerPropertyValue("InstallLocation");
return string.IsNullOrEmpty(location) ? null : Path.Combine(location, Constants.WinGetExe);
#else
// Development packages are not Store signed.
return WingetCLIWrapper.WinGetFullPath;
#endif
}

private static bool IsSupportedOSVersion()
{
// Windows version has to be equal or newer than 10.0.17763.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ internal class AppxModuleHelper
private const string AppxManifest = "AppxManifest.xml";
private const string PackageFullName = "PackageFullName";
private const string Version = "Version";
private const string PublisherId = "PublisherId";
private const string SignatureKind = "SignatureKind";
private const string MicrosoftPublisherId = "8wekyb3d8bbwe";
private const string StoreSignatureKind = "Store";

private const string DependencyArchitectureEnvironmentVariable = "WINGET_PACKAGE_MANAGER_REPAIR_DEPENDENCY_ARCHITECTURES";

Expand Down Expand Up @@ -125,7 +121,7 @@ public AppxModuleHelper(PowerShellCmdlet pwshCmdlet)
/// <returns>Result of Get-AppxPackage.</returns>
public PSObject? GetAppInstallerObject(bool allUsers = false)
{
return this.GetAppxObjects(AppInstallerName, allUsers).FirstOrDefault();
return this.GetAppxObject(AppInstallerName, allUsers);
}

/// <summary>
Expand All @@ -134,7 +130,7 @@ public AppxModuleHelper(PowerShellCmdlet pwshCmdlet)
/// <returns>Result of Get-AppxPackage.</returns>
public PSObject? GetWinGetSourceObject()
{
return this.GetAppxObjects(WinGetSourceName).FirstOrDefault();
return this.GetAppxObject(WinGetSourceName);
}

/// <summary>
Expand All @@ -159,18 +155,6 @@ public AppxModuleHelper(PowerShellCmdlet pwshCmdlet)
return result;
}

/// <summary>
/// Gets a string property from the Microsoft-published, Store-signed AppInstaller package.
/// </summary>
/// <param name="propertyName">Property name.</param>
/// <returns>The property value, or null if the package or property is missing.</returns>
public string? GetStoreSignedAppInstallerPropertyValue(string propertyName)
{
var packageObj = this.GetAppxObjects(AppInstallerName)
.FirstOrDefault(p => IsStoreSignedMicrosoftPackage(p.Properties[PublisherId]?.Value, p.Properties[SignatureKind]?.Value));
return packageObj?.Properties[propertyName]?.Value as string;
}

/// <summary>
/// Checks if winget source is installed.
/// </summary>
Expand Down Expand Up @@ -261,18 +245,6 @@ public async Task InstallWinGetSourceAsync()
await this.DownloadPackageAndAddAsync(WinGetSourceUrl, WinGetSourceMsixName, options: null);
}

/// <summary>
/// Checks if a package is published by Microsoft and signed by the Store.
/// </summary>
/// <param name="publisherId">The PublisherId of the package.</param>
/// <param name="signatureKind">The SignatureKind of the package.</param>
/// <returns>True if the package is published by Microsoft and signed by the Store.</returns>
internal static bool IsStoreSignedMicrosoftPackage(object? publisherId, object? signatureKind)
{
// SignatureKind is an enum, or a string when the Appx module is loaded with -UseWindowsPowerShell.
return publisherId?.ToString() == MicrosoftPublisherId && signatureKind?.ToString() == StoreSignatureKind;
}

/// <summary>
/// Gets the Xaml dependency package name and release tag based on the provided WinGet release tag.
/// </summary>
Expand Down Expand Up @@ -366,7 +338,7 @@ private async Task AddAppInstallerBundleAsync(string releaseTag, bool downgrade,
}
}

private Collection<PSObject> GetAppxObjects(string packageName, bool allUsers = false)
private PSObject? GetAppxObject(string packageName, bool allUsers = false)
{
var options = new List<string>();
if (allUsers)
Expand All @@ -380,7 +352,8 @@ private Collection<PSObject> GetAppxObjects(string packageName, bool allUsers =
{
{ Name, packageName },
},
options);
options)
.FirstOrDefault();
}

private async Task InstallDependenciesAsync(string releaseTag)
Expand Down Expand Up @@ -691,7 +664,7 @@ private async Task InstallUiXamlAsync(string releaseTag)
string xamlAssetX86 = string.Format("{0}.x86.appx", xamlPackageName);
string xamlAssetArm64 = string.Format("{0}.arm64.appx", xamlPackageName);

var uiXamlObjs = this.GetAppxObjects(xamlPackageName).FirstOrDefault();
var uiXamlObjs = this.GetAppxObject(xamlPackageName);
if (uiXamlObjs is null)
{
var githubRelease = new GitHubClient(RepositoryOwner.Microsoft, RepositoryName.UiXaml, this.pwshCmdlet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ public WinGetVersion(string version)
/// Runs the winget version command.
/// </summary>
/// <param name="pwshCmdlet">PowerShell cmdlet.</param>
/// <param name="wingetPath">The full path to the WinGet executable.</param>
/// <returns>The command result.</returns>
public static WinGetCLICommandResult RunWinGetVersionFromCLI(PowerShellCmdlet pwshCmdlet, string wingetPath)
public static WinGetCLICommandResult RunWinGetVersionFromCLI(PowerShellCmdlet pwshCmdlet)
{
var wingetCliWrapper = new WingetCLIWrapper(wingetPath);
var wingetCliWrapper = new WingetCLIWrapper();
return wingetCliWrapper.RunCommand(pwshCmdlet, new WinGetCLICommandBuilder().AppendSwitch("--version"));
}

Expand All @@ -99,7 +98,7 @@ public static WinGetVersion InstalledWinGetVersion(PowerShellCmdlet pwshCmdlet,
return new WinGetVersion(comVersion);
}

versionResult = RunWinGetVersionFromCLI(pwshCmdlet, WingetCLIWrapper.WinGetFullPath);
versionResult = RunWinGetVersionFromCLI(pwshCmdlet);
}

return new WinGetVersion(versionResult.StdOut.Replace(Environment.NewLine, string.Empty));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,27 @@
namespace Microsoft.WinGet.Client.Engine.Helpers
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.WinGet.Client.Engine.Common;
using Microsoft.WinGet.Client.Engine.Exceptions;
using Microsoft.WinGet.Common.Command;
using Windows.ApplicationModel;
using Windows.Management.Deployment;

/// <summary>
/// Calls winget directly.
/// </summary>
internal class WingetCLIWrapper
{
private const int ErrorFileNotFound = 2;
private const string MicrosoftPublisherId = "8wekyb3d8bbwe";

private static string? winGetFullPath;

/// <summary>
/// The file name to use in start info.
/// </summary>
Expand All @@ -26,21 +36,25 @@ internal class WingetCLIWrapper
/// <summary>
/// Initializes a new instance of the <see cref="WingetCLIWrapper"/> class.
/// </summary>
/// <param name="wingetPath">The full path to the WinGet executable.</param>
public WingetCLIWrapper(string wingetPath)
public WingetCLIWrapper()
{
this.wingetPath = wingetPath ?? throw new ArgumentNullException(nameof(wingetPath));

if (Utilities.ExecutingAsSystem)
{
throw new NotSupportedException();
}

this.wingetPath = WinGetFullPath;
}

/// <summary>
/// Gets the cached path to the installed WinGet executable.
/// </summary>
public static string WinGetFullPath => LazyInitializer.EnsureInitialized(ref winGetFullPath, GetWinGetPath) !;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

WinGetFullPath caches a versioned WindowsApps path for the PowerShell process lifetime. RepairStateMachineAsync resolves it before deploying a different App Installer version, then re-enters AssertWinGet with the stale path, so the repair reruns the old binary or fails after its removal. Please re-resolve after deployment or avoid caching, and test a version-changing repair followed by another CLI command in the same session.


/// <summary>
/// Gets the WinGet app execution alias path in the package family directory.
/// </summary>
public static string WinGetFullPath
public static string WinGetAliasPath
{
get
{
Expand All @@ -51,6 +65,17 @@ public static string WinGetFullPath
}
}

/// <summary>
/// Checks if a package is published by Microsoft and signed by the Store.
/// </summary>
/// <param name="publisherId">The PublisherId of the package.</param>
/// <param name="signatureKind">The SignatureKind of the package.</param>
/// <returns>True if the package is published by Microsoft and signed by the Store.</returns>
internal static bool IsStoreSignedMicrosoftPackage(string? publisherId, PackageSignatureKind signatureKind)
{
return publisherId == MicrosoftPublisherId && signatureKind == PackageSignatureKind.Store;
}

/// <summary>
/// Runs winget command with parameters.
/// </summary>
Expand Down Expand Up @@ -87,5 +112,31 @@ internal WinGetCLICommandResult RunCommand(PowerShellCmdlet pwshCmdlet, WinGetCL

throw new WinGetCLITimeoutException(builder.Command, builder.Parameters);
}

private static string GetWinGetPath()
{
#if USE_PROD_CLSIDS
Func<Package, bool> packageFilter = p => IsStoreSignedMicrosoftPackage(p.Id.PublisherId, p.SignatureKind);
#else
// Development packages are not Store signed.
Func<Package, bool> packageFilter = _ => true;
#endif

var package = new PackageManager()
.FindPackagesForUserWithPackageTypes(string.Empty, Constants.WingetPackageFamilyName, PackageTypes.Main)
.FirstOrDefault(packageFilter);
string? location = package?.InstalledLocation.Path;
if (string.IsNullOrEmpty(location))
{
throw new Win32Exception(ErrorFileNotFound);
}

#if USE_PROD_CLSIDS
return Path.Combine(location, Constants.WinGetExe);
#else
// Development packages need alias activation to resolve their framework dependencies.
return WinGetAliasPath;
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,28 @@
<PropertyGroup Condition="'$(TargetFramework)' == '$(CoreFramework)'">
<CsWinRTIncludes>
Microsoft.Management.Deployment;
Windows.ApplicationModel.AppDisplayInf;
Windows.ApplicationModel.IAppDisplayInf;
Windows.ApplicationModel.AppExecutionContex;
Windows.ApplicationModel.IAppExecutionContex;
Windows.ApplicationModel.AppInf;
Windows.ApplicationModel.IAppInf;
Windows.ApplicationModel.AppInstallerInf;
Windows.ApplicationModel.IAppInstallerInf;
Windows.ApplicationModel.AppInstallerPolicySourc;
Windows.ApplicationModel.AddResourcePackageOption;
Windows.ApplicationModel.IAddResourcePackageOption;
Windows.ApplicationModel.FindRelatedPackagesOption;
Windows.ApplicationModel.IFindRelatedPackagesOption;
Windows.ApplicationModel.Packag;
Windows.ApplicationModel.IPackag;
Windows.ApplicationModel.Core.AppListEntr;
Windows.ApplicationModel.Core.IAppListEntr;
Windows.Data.Text.TextSegmen;
Windows.Devices.Geolocation;
Windows.Foundation;
Windows.Globalization.DayOfWee;
Windows.Management.Deployment;
Windows.Networking.Connectivity;
Windows.Networking.DomainNameTyp;
Windows.Networking.EndpointPai;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.WinGet.Client.Engine\Microsoft.WinGet.Client.Engine.csproj" />
<ProjectReference Include="..\Microsoft.WinGet.Client.Engine\Microsoft.WinGet.Client.Engine.csproj" Aliases="global,ClientEngine" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace Microsoft.WinGet.UnitTests
{
using System;
using System.IO;
using Microsoft.WinGet.Client.Engine.Common;
using Microsoft.WinGet.Client.Engine.Helpers;
Expand All @@ -15,14 +14,6 @@ namespace Microsoft.WinGet.UnitTests
/// <summary>Tests WinGet executable paths and environment integrity checks.</summary>
public class WinGetIntegrityTests
{
/// <summary>A missing executable path must not select the package alias.</summary>
[Fact]
public void MissingExecutablePathDoesNotFallBackToAlias()
{
static void CreateWithoutPath() => _ = new WingetCLIWrapper(null!);
Assert.Throws<ArgumentNullException>(CreateWithoutPath);
}

/// <summary>Checks environment health independently of executable launch success.</summary>
/// <param name="aliasEnabled">Whether the app-execution alias exists.</param>
/// <param name="pathFormat">The PATH, with placeholders for the WindowsApps directory.</param>
Expand Down
Loading
Loading