Skip to content

Commit ba62f35

Browse files
Remove bundled WinGet COM fallback
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7e4e25c commit ba62f35

2 files changed

Lines changed: 11 additions & 74 deletions

File tree

src/UniGetUI.PackageEngine.Managers.WinGet/ClientHelpers/NativeWinGetHelper.cs

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Runtime.ExceptionServices;
23
using Microsoft.Management.Deployment;
34
using UniGetUI.Core.Classes;
45
using UniGetUI.Core.Logging;
@@ -21,42 +22,36 @@ internal sealed class NativeWinGetHelper : IWinGetManagerHelper
2122
public PackageManager WinGetManager = null!;
2223
public static PackageManager? ExternalWinGetManager;
2324
private readonly WinGet Manager;
24-
private readonly Func<WinGet, IWinGetManagerHelper> _bundledCliHelperFactory;
2525
private readonly Func<WinGet, IWinGetManagerHelper> _systemCliHelperFactory;
2626
private readonly Func<IReadOnlyList<CatalogPackage>>? _localPackagesProvider;
2727
private int _activeLocalPackageQueries;
28+
private ExceptionDispatchInfo? _lastActivationException;
2829

2930
public string ActivationMode { get; private set; } = string.Empty;
3031
public string ActivationSource { get; private set; } = string.Empty;
31-
private bool _isBundledActivation;
3232

3333
internal static IReadOnlyList<string> PreferredActivationModes =>
3434
[
3535
"packaged COM registration",
3636
"lower-trust COM registration",
37-
"bundled in-proc COM",
3837
];
3938

4039
public NativeWinGetHelper(WinGet manager)
4140
: this(
4241
manager,
43-
bundledCliHelperFactory: null,
4442
systemCliHelperFactory: null,
4543
skipInitialization: false,
4644
localPackagesProvider: null
4745
) { }
4846

4947
internal NativeWinGetHelper(
5048
WinGet manager,
51-
Func<WinGet, IWinGetManagerHelper>? bundledCliHelperFactory,
5249
Func<WinGet, IWinGetManagerHelper>? systemCliHelperFactory,
5350
bool skipInitialization,
5451
Func<IReadOnlyList<CatalogPackage>>? localPackagesProvider
5552
)
5653
{
5754
Manager = manager;
58-
_bundledCliHelperFactory =
59-
bundledCliHelperFactory ?? (static manager => new BundledWinGetHelper(manager));
6055
_systemCliHelperFactory =
6156
systemCliHelperFactory
6257
?? (static manager => new BundledWinGetHelper(manager, manager.Status.ExecutablePath));
@@ -83,14 +78,9 @@ internal NativeWinGetHelper(
8378
return;
8479
}
8580

86-
InitializeBundledFactory();
87-
}
81+
_lastActivationException?.Throw();
8882

89-
internal void UseBundledActivationForTesting()
90-
{
91-
_isBundledActivation = true;
92-
ActivationMode = "bundled in-proc COM";
93-
ActivationSource = "test";
83+
throw new InvalidOperationException("WinGet: Failed to initialize system COM activation.");
9484
}
9585

9686
internal bool HasActiveLocalPackageQuery => Volatile.Read(ref _activeLocalPackageQueries) > 0;
@@ -117,15 +107,17 @@ private bool TryInitializeLowerTrustFactory()
117107
}
118108
catch (WinGetComActivationException ex)
119109
{
110+
_lastActivationException = ExceptionDispatchInfo.Capture(ex);
120111
Logger.Warn(
121-
$"Lower-trust WinGet COM activation failed ({ex.HResultHex}: {ex.Reason}), attempting bundled in-proc activation..."
112+
$"Lower-trust WinGet COM activation failed ({ex.HResultHex}: {ex.Reason})."
122113
);
123114
return false;
124115
}
125116
catch (Exception ex)
126117
{
118+
_lastActivationException = ExceptionDispatchInfo.Capture(ex);
127119
Logger.Warn(
128-
$"Lower-trust WinGet COM activation failed ({ex.Message}), attempting bundled in-proc activation..."
120+
$"Lower-trust WinGet COM activation failed ({ex.Message})."
129121
);
130122
return false;
131123
}
@@ -148,34 +140,22 @@ private bool TryInitializeStandardFactory()
148140
}
149141
catch (WinGetComActivationException ex)
150142
{
143+
_lastActivationException = ExceptionDispatchInfo.Capture(ex);
151144
Logger.Warn(
152145
$"Packaged WinGet COM activation failed ({ex.HResultHex}: {ex.Reason}), attempting lower-trust activation..."
153146
);
154147
return false;
155148
}
156149
catch (Exception ex)
157150
{
151+
_lastActivationException = ExceptionDispatchInfo.Capture(ex);
158152
Logger.Warn(
159153
$"Packaged WinGet COM activation failed ({ex.Message}), attempting lower-trust activation..."
160154
);
161155
return false;
162156
}
163157
}
164158

165-
private void InitializeBundledFactory()
166-
{
167-
var factory = new WindowsPackageManagerBundledFactory();
168-
var winGetManager = factory.CreatePackageManager();
169-
ApplyFactory(
170-
factory,
171-
winGetManager,
172-
"bundled in-proc COM",
173-
factory.LibraryPath,
174-
"Connected to WinGet API using bundled in-proc activation."
175-
);
176-
_isBundledActivation = true;
177-
}
178-
179159
private void ApplyFactory(
180160
WindowsPackageManagerFactory factory,
181161
PackageManager winGetManager,
@@ -321,12 +301,6 @@ var filter_type in new[]
321301

322302
public IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
323303
{
324-
if (_isBundledActivation)
325-
{
326-
Logger.Info("WinGet is using bundled in-proc COM — falling back to CLI for update detection");
327-
return _bundledCliHelperFactory(Manager).GetAvailableUpdates_UnSafe();
328-
}
329-
330304
var logger = Manager.TaskLogger.CreateNew(LoggableTaskType.ListUpdates);
331305
IReadOnlyList<CatalogPackage> nativePackages;
332306
try
@@ -401,12 +375,6 @@ public IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
401375

402376
public IReadOnlyList<Package> GetInstalledPackages_UnSafe()
403377
{
404-
if (_isBundledActivation)
405-
{
406-
Logger.Info("WinGet is using bundled in-proc COM — falling back to CLI for installed package detection");
407-
return _bundledCliHelperFactory(Manager).GetInstalledPackages_UnSafe();
408-
}
409-
410378
var logger = Manager.TaskLogger.CreateNew(LoggableTaskType.ListInstalledPackages);
411379
IReadOnlyList<CatalogPackage> nativePackages;
412380
try

src/UniGetUI.PackageEngine.Tests/WinGetManagerTests.cs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -134,39 +134,11 @@ public void GetInstalledPackagesUpdatesNoPackagesFlagForFailureAndRecovery()
134134
public void NativeWinGetHelperPrefersSystemComBeforeBundledActivation()
135135
{
136136
Assert.Equal(
137-
["packaged COM registration", "lower-trust COM registration", "bundled in-proc COM"],
137+
["packaged COM registration", "lower-trust COM registration"],
138138
NativeWinGetHelper.PreferredActivationModes
139139
);
140140
}
141141

142-
[Fact]
143-
public void NativeWinGetHelperUsesBundledFallbackForInstalledPackagesWhenBundledActivationIsSelected()
144-
{
145-
var manager = new TestableWinGet();
146-
var expectedPackage = new PackageBuilder()
147-
.WithManager(manager)
148-
.WithName("Contoso Tool")
149-
.WithId("Contoso.Tool")
150-
.WithVersion("1.2.3")
151-
.Build();
152-
var bundledFallbackHelper = new TestWinGetManagerHelper
153-
{
154-
GetInstalledPackagesHandler = () => [expectedPackage],
155-
};
156-
var helper = new NativeWinGetHelper(
157-
manager,
158-
bundledCliHelperFactory: _ => bundledFallbackHelper,
159-
systemCliHelperFactory: null,
160-
skipInitialization: true,
161-
localPackagesProvider: null
162-
);
163-
helper.UseBundledActivationForTesting();
164-
165-
var packages = helper.GetInstalledPackages_UnSafe();
166-
167-
PackageAssert.Matches(Assert.Single(packages), "Contoso Tool", "Contoso.Tool", "1.2.3");
168-
}
169-
170142
[Fact]
171143
public void NativeWinGetHelperUsesSystemCliFallbackForInstalledPackagesWhenCompositeCatalogFails()
172144
{
@@ -183,7 +155,6 @@ public void NativeWinGetHelperUsesSystemCliFallbackForInstalledPackagesWhenCompo
183155
};
184156
var helper = new NativeWinGetHelper(
185157
manager,
186-
bundledCliHelperFactory: null,
187158
systemCliHelperFactory: _ => systemCliFallbackHelper,
188159
skipInitialization: true,
189160
localPackagesProvider: () =>
@@ -212,7 +183,6 @@ public void NativeWinGetHelperUsesSystemCliFallbackForUpdatesWhenCompositeCatalo
212183
};
213184
var helper = new NativeWinGetHelper(
214185
manager,
215-
bundledCliHelperFactory: null,
216186
systemCliHelperFactory: _ => systemCliFallbackHelper,
217187
skipInitialization: true,
218188
localPackagesProvider: () =>
@@ -258,7 +228,6 @@ public void AttemptFastRepairKeepsNativeHelperWhileLocalPackageEnumerationIsStil
258228
var manager = new TestableWinGet();
259229
var helper = new NativeWinGetHelper(
260230
manager,
261-
bundledCliHelperFactory: null,
262231
systemCliHelperFactory: null,
263232
skipInitialization: true,
264233
localPackagesProvider: null

0 commit comments

Comments
 (0)