Skip to content

Commit 53c9dbe

Browse files
committed
Use embedded CsWinRT (#5178)
## Issue Updating the version of CsWinRT that we use lead to a problem with conflicting WinRT.Runtime assemblies (microsoft/winget-dsc#140). While that issue was mitigated by delisting the offending version, in order to ship any release with the newer version of CsWinRT, we need to embed the WinRT.Runtime in our assemblies. ## Change Move to use embedded CsWinRT, specifically in `Microsoft.WinGet.Client.Engine` and `Microsoft.Management.Configuration.Processor`. The dependent projects are updated to no longer reference CsWinRT, but requires some special handling to prevent errors. Since the configuration code was leveraging a projection assembly, it is being replaced by the processor. The projected types are shared out to those dependent binaries via `InternalsVisibleTo`. Also fixes an annoying behavior where an exception escaping from a command execution would prevent the `--logs` option from opening the log location (error cases being more likely to be the time one would want the logs of course).
1 parent 2b8ca4d commit 53c9dbe

36 files changed

+344
-188
lines changed

.github/actions/spelling/excludes.txt

+2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@
100100
^src/catch2/
101101
^src/cpprestsdk/
102102
^src/JsonCppLib/
103+
^src/Microsoft.Management.Configuration.Processor/Microsoft.Management.Configuration.Processor.csproj$
103104
^src/PowerShell/Help/markdownlint\.yaml$
105+
^src/PowerShell/Microsoft.WinGet.Client.Engine/Microsoft.WinGet.Client.Engine.csproj$
104106
^src/PureLib/
105107
^src/SfsClient/
106108
^src/UndockedRegFreeWinRT/

src/AppInstallerCLI.sln

+7
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Microsoft.Management.Deploy
214214
EndProject
215215
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Management.Deployment.CsWinRTProjection", "Microsoft.Management.Deployment.CsWinRTProjection\Microsoft.Management.Deployment.CsWinRTProjection.csproj", "{9406322E-6272-487E-902A-9953889719EA}"
216216
EndProject
217+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "targets", "targets", "{A0B4F808-B190-41C4-97CB-C8EA1932F84F}"
218+
ProjectSection(SolutionItems) = preProject
219+
targets\EmbeddedCsWinRT.targets = targets\EmbeddedCsWinRT.targets
220+
targets\ReferenceEmbeddedCsWinRTProject.targets = targets\ReferenceEmbeddedCsWinRTProject.targets
221+
EndProjectSection
222+
EndProject
217223
Global
218224
GlobalSection(SolutionConfigurationPlatforms) = preSolution
219225
Debug|ARM64 = Debug|ARM64
@@ -1301,6 +1307,7 @@ Global
13011307
{5A52D9FC-0059-4A4A-8196-427A7AA0D1C5} = {7C218A3E-9BC8-48FF-B91B-BCACD828C0C9}
13021308
{1B9077B3-8923-4ECD-8FC9-B3190FCBE4D4} = {60618CAC-2995-4DF9-9914-45C6FC02C995}
13031309
{76B26B2C-602A-4AD0-9736-4162D3FCA92A} = {1A5D7A7D-5CB2-47D5-B40D-4E61CAEDC798}
1310+
{A0B4F808-B190-41C4-97CB-C8EA1932F84F} = {8D53D749-D51C-46F8-A162-9371AAA6C2E7}
13041311
EndGlobalSection
13051312
GlobalSection(ExtensibilityGlobals) = postSolution
13061313
SolutionGuid = {B6FDB70C-A751-422C-ACD1-E35419495857}

src/AppInstallerCLICore/AppInstallerCLICore.vcxproj

-16
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,6 @@
530530
<ProjectReference Include="..\YamlCppLib\YamlCppLib.vcxproj">
531531
<Project>{8bb94bb8-374f-4294-bca1-c7811514a6b7}</Project>
532532
</ProjectReference>
533-
<ProjectReference Include="..\Microsoft.Management.Configuration.Processor\Microsoft.Management.Configuration.Processor.csproj">
534-
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
535-
</ProjectReference>
536533
</ItemGroup>
537534
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
538535
<ImportGroup Label="ExtensionTargets">
@@ -546,18 +543,5 @@
546543
<Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.231028.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorTextNuget)', '$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.231028.1\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
547544
<Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.230706.1\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorTextNuget)', '$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.230706.1\build\native\Microsoft.Windows.CppWinRT.props'))" />
548545
<Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.230706.1\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorTextNuget)', '$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.230706.1\build\native\Microsoft.Windows.CppWinRT.targets'))" />
549-
<PropertyGroup>
550-
<!-- The output path for this project is different on some builds where the RIDs are not used in the output. -->
551-
<MicrosoftManagementConfigurationProcessorPath>$(SolutionDir)\AnyCPU\$(Configuration)\Microsoft.Management.Configuration.Processor\Microsoft.Management.Configuration.Processor.winmd</MicrosoftManagementConfigurationProcessorPath>
552-
<MicrosoftManagementConfigurationProcessorPath Condition="!Exists('$(MicrosoftManagementConfigurationProcessorPath)')">$(SolutionDir)\AnyCPU\$(Configuration)\Microsoft.Management.Configuration.Processor\net8.0-windows10.0.22000.0\win\Microsoft.Management.Configuration.Processor.winmd</MicrosoftManagementConfigurationProcessorPath>
553-
</PropertyGroup>
554-
<Message Importance="high" Text="Microsoft.Management.Configuration.Processor.winmd -&gt; $(MicrosoftManagementConfigurationProcessorPath)" />
555-
<Error Condition="!Exists('$(MicrosoftManagementConfigurationProcessorPath)')" Text="Microsoft.Management.Configuration.Processor.winmd was not found in $(MicrosoftManagementConfigurationProcessorPath)" />
556-
<ItemGroup Condition="Exists('$(MicrosoftManagementConfigurationProcessorPath)')">
557-
<Reference Include="Microsoft.Management.Configuration.Processor">
558-
<HintPath>$(MicrosoftManagementConfigurationProcessorPath)</HintPath>
559-
<IsWinMDFile>true</IsWinMDFile>
560-
</Reference>
561-
</ItemGroup>
562546
</Target>
563547
</Project>

src/AppInstallerCLICore/Command.cpp

+21-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,24 @@ using namespace AppInstaller::Settings;
1616

1717
namespace AppInstaller::CLI
1818
{
19-
constexpr Utility::LocIndView s_Command_ArgName_SilentAndInteractive = "silent|interactive"_liv;
19+
namespace
20+
{
21+
constexpr Utility::LocIndView s_Command_ArgName_SilentAndInteractive = "silent|interactive"_liv;
22+
23+
void LaunchLogsIfRequested(Execution::Context& context)
24+
{
25+
try
26+
{
27+
if (context.Args.Contains(Execution::Args::Type::OpenLogs))
28+
{
29+
// TODO: Consider possibly adding functionality that if the context contains 'Execution::Args::Type::Log' to open the path provided for the log
30+
// The above was omitted initially as a security precaution to ensure that user input to '--log' wouldn't be passed directly to ShellExecute
31+
ShellExecute(NULL, NULL, Runtime::GetPathTo(Runtime::PathName::DefaultLogLocation).wstring().c_str(), NULL, NULL, SW_SHOWNORMAL);
32+
}
33+
}
34+
CATCH_LOG();
35+
}
36+
}
2037

2138
Command::Command(
2239
std::string_view name,
@@ -952,12 +969,7 @@ namespace AppInstaller::CLI
952969
}
953970
else
954971
{
955-
if (context.Args.Contains(Execution::Args::Type::OpenLogs))
956-
{
957-
// TODO: Consider possibly adding functionality that if the context contains 'Execution::Args::Type::Log' to open the path provided for the log
958-
// The above was omitted initially as a security precaution to ensure that user input to '--log' wouldn't be passed directly to ShellExecute
959-
ShellExecute(NULL, NULL, Runtime::GetPathTo(Runtime::PathName::DefaultLogLocation).wstring().c_str(), NULL, NULL, SW_SHOWNORMAL);
960-
}
972+
LaunchLogsIfRequested(context);
961973

962974
if (context.Args.Contains(Execution::Args::Type::Wait))
963975
{
@@ -1046,6 +1058,8 @@ namespace AppInstaller::CLI
10461058
catch (...)
10471059
{
10481060
context.SetTerminationHR(Workflow::HandleException(context, std::current_exception()));
1061+
1062+
LaunchLogsIfRequested(context);
10491063
}
10501064
}
10511065

src/AppInstallerCLICore/Commands/DebugCommand.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#if _DEBUG
66
#include "DebugCommand.h"
77
#include <winrt/Microsoft.Management.Configuration.h>
8+
#include <winrt/Microsoft.Management.Configuration.SetProcessorFactory.h>
89
#include "AppInstallerDownloader.h"
910
#include "Sixel.h"
1011

@@ -106,6 +107,7 @@ namespace AppInstaller::CLI
106107
OutputProxyStubInterfaceRegistration<winrt::Microsoft::Management::Configuration::IConfigurationUnitProcessorDetails2>(context);
107108
OutputProxyStubInterfaceRegistration<winrt::Microsoft::Management::Configuration::IGetAllSettingsConfigurationUnitProcessor>(context);
108109
OutputProxyStubInterfaceRegistration<winrt::Microsoft::Management::Configuration::IConfigurationStatics2>(context);
110+
OutputProxyStubInterfaceRegistration<winrt::Microsoft::Management::Configuration::SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties>(context);
109111

110112
// TODO: Fix the layering inversion created by the COM deployment API (probably in order to operate winget.exe against the COM server).
111113
// Then this code can just have a CppWinRT reference to the deployment API and spit out the interface registrations just like for configuration.

src/AppInstallerCLICore/ConfigurationSetProcessorFactoryRemoting.cpp

+7-32
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <AppInstallerRuntime.h>
88
#include <AppInstallerStrings.h>
99
#include <winget/ILifetimeWatcher.h>
10-
#include <winrt/Microsoft.Management.Configuration.Processor.h>
1110
#include <winrt/Microsoft.Management.Configuration.SetProcessorFactory.h>
1211

1312
using namespace winrt::Windows::Foundation;
@@ -250,40 +249,40 @@ namespace AppInstaller::CLI::ConfigurationRemoting
250249
// Create a copy for remote and set remote module paths
251250
std::vector<winrt::hstring> newRemotePaths{ newModulePaths };
252251
m_remoteAdditionalModulePaths = winrt::single_threaded_vector<winrt::hstring>(std::move(newRemotePaths));
253-
m_remoteFactory.as<Processor::IPowerShellConfigurationProcessorFactoryProperties>().AdditionalModulePaths(m_remoteAdditionalModulePaths.GetView());
252+
m_remoteFactory.as<SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties>().AdditionalModulePaths(m_remoteAdditionalModulePaths.GetView());
254253

255254
// Store the updated module paths that we were given
256255
m_additionalModulePaths = winrt::single_threaded_vector<winrt::hstring>(std::move(newModulePaths));
257256
}
258257

259258
SetProcessorFactory::PwshConfigurationProcessorPolicy Policy() const
260259
{
261-
return Convert(m_remoteFactory.as<Processor::IPowerShellConfigurationProcessorFactoryProperties>().Policy());
260+
return m_remoteFactory.as<SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties>().Policy();
262261
}
263262

264263
void Policy(SetProcessorFactory::PwshConfigurationProcessorPolicy value)
265264
{
266-
m_remoteFactory.as<Processor::IPowerShellConfigurationProcessorFactoryProperties>().Policy(Convert(value));
265+
m_remoteFactory.as<SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties>().Policy(value);
267266
}
268267

269268
SetProcessorFactory::PwshConfigurationProcessorLocation Location() const
270269
{
271-
return Convert(m_remoteFactory.as<Processor::IPowerShellConfigurationProcessorFactoryProperties>().Location());
270+
return m_remoteFactory.as<SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties>().Location();
272271
}
273272

274273
void Location(SetProcessorFactory::PwshConfigurationProcessorLocation value)
275274
{
276-
m_remoteFactory.as<Processor::IPowerShellConfigurationProcessorFactoryProperties>().Location(Convert(value));
275+
m_remoteFactory.as<SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties>().Location(value);
277276
}
278277

279278
winrt::hstring CustomLocation() const
280279
{
281-
return m_remoteFactory.as<Processor::IPowerShellConfigurationProcessorFactoryProperties>().CustomLocation();
280+
return m_remoteFactory.as<SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties>().CustomLocation();
282281
}
283282

284283
void CustomLocation(winrt::hstring value)
285284
{
286-
m_remoteFactory.as<Processor::IPowerShellConfigurationProcessorFactoryProperties>().CustomLocation(value);
285+
m_remoteFactory.as<SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties>().CustomLocation(value);
287286
}
288287

289288
HRESULT STDMETHODCALLTYPE SetLifetimeWatcher(IUnknown* watcher)
@@ -292,30 +291,6 @@ namespace AppInstaller::CLI::ConfigurationRemoting
292291
}
293292

294293
private:
295-
static SetProcessorFactory::PwshConfigurationProcessorPolicy Convert(Processor::PowerShellConfigurationProcessorPolicy policy)
296-
{
297-
// We have used the same values intentionally; if that changes, update this.
298-
return ToEnum<SetProcessorFactory::PwshConfigurationProcessorPolicy>(ToIntegral(policy));
299-
}
300-
301-
static Processor::PowerShellConfigurationProcessorPolicy Convert(SetProcessorFactory::PwshConfigurationProcessorPolicy policy)
302-
{
303-
// We have used the same values intentionally; if that changes, update this.
304-
return ToEnum<Processor::PowerShellConfigurationProcessorPolicy>(ToIntegral(policy));
305-
}
306-
307-
static SetProcessorFactory::PwshConfigurationProcessorLocation Convert(Processor::PowerShellConfigurationProcessorLocation location)
308-
{
309-
// We have used the same values intentionally; if that changes, update this.
310-
return ToEnum<SetProcessorFactory::PwshConfigurationProcessorLocation>(ToIntegral(location));
311-
}
312-
313-
static Processor::PowerShellConfigurationProcessorLocation Convert(SetProcessorFactory::PwshConfigurationProcessorLocation location)
314-
{
315-
// We have used the same values intentionally; if that changes, update this.
316-
return ToEnum<Processor::PowerShellConfigurationProcessorLocation>(ToIntegral(location));
317-
}
318-
319294
IConfigurationSetProcessorFactory m_remoteFactory;
320295
wil::unique_event m_completionEvent;
321296
Collections::IVector<winrt::hstring> m_additionalModulePaths{ winrt::single_threaded_vector<winrt::hstring>() };

src/AppInstallerCLIPackage/AppInstallerCLIPackage.wapproj

-6
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,6 @@
244244
<WinGetAdditionalPackageFile Include="$(WinGetAdditionalPackageFileRoot)\$(PlatformTarget)\$(Configuration)\Microsoft.Management.Configuration\Microsoft.Management.Configuration.winmd">
245245
<PackagePath>Microsoft.Management.Configuration.winmd</PackagePath>
246246
</WinGetAdditionalPackageFile>
247-
<WinGetAdditionalPackageFile Include="$(WinGetAdditionalPackageFileRoot)\AnyCPU\$(Configuration)\Microsoft.Management.Configuration.Processor\**\Microsoft.Management.Configuration.Processor.winmd">
248-
<PackagePath>Microsoft.Management.Configuration.Processor.winmd</PackagePath>
249-
</WinGetAdditionalPackageFile>
250-
<WinGetAdditionalPackageFile Include="$(WinGetAdditionalPackageFileRoot)\AnyCPU\$(Configuration)\Microsoft.Management.Configuration.Projection\**\Microsoft.Management.Configuration.Projection.dll">
251-
<PackagePath>ConfigurationRemotingServer\Microsoft.Management.Configuration.Projection.dll</PackagePath>
252-
</WinGetAdditionalPackageFile>
253247
<WinGetAdditionalPackageFile Include="$(WinGetAdditionalPackageFileRoot)\$(PlatformTarget)\$(Configuration)\ConfigurationRemotingServer\net8.0-windows10.0.22000.0\$(ConfigServerRid)\**\*">
254248
<PackagePath>ConfigurationRemotingServer</PackagePath>
255249
<Recurse>true</Recurse>

src/AppInstallerCLIPackage/Package.appxmanifest

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<Interface Name="Microsoft.Management.Configuration.IConfigurationUnitProcessorDetails2" InterfaceId="E89623ED-76E2-5145-B920-D09659554E35" />
118118
<Interface Name="Microsoft.Management.Configuration.IGetAllSettingsConfigurationUnitProcessor" InterfaceId="72EB8304-D8D3-57D4-9940-7C1C4AD8C40C" />
119119
<Interface Name="Microsoft.Management.Configuration.IConfigurationStatics2" InterfaceId="540BE073-F2EF-5375-83AA-8E23086B0669" />
120+
<Interface Name="Microsoft.Management.Configuration.SetProcessorFactory.IPwshConfigurationSetProcessorFactoryProperties" InterfaceId="2C298A30-BD3B-5D00-BCD1-2EB633AB7E3B" />
120121
</ProxyStub>
121122
</Extension>
122123
<!-- This entry forces the package registration to process the windows.activatableClass.proxyStub extension above. -->

src/ConfigurationRemotingServer/ConfigurationRemotingServer.csproj

+1-7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
<OutputPath>$(SolutionDir)$(Platform)\$(Configuration)\$(MSBuildProjectName)\</OutputPath>
1111
<SelfContained>true</SelfContained>
1212
<RuntimeIdentifiers>win-x64;win-x86;win-arm64</RuntimeIdentifiers>
13-
<!--
14-
!!! Remove or update this on the next Microsoft.Windows.CsWinRT package version update. !!!
15-
-->
16-
<WindowsSdkPackageVersion>10.0.22000.53</WindowsSdkPackageVersion>
1713
</PropertyGroup>
1814

1915
<PropertyGroup Condition=" '$(Platform)' == 'x64' ">
@@ -28,9 +24,7 @@
2824
<RuntimeIdentifier>win-arm64</RuntimeIdentifier>
2925
</PropertyGroup>
3026

31-
<ItemGroup>
32-
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.1.6" />
33-
</ItemGroup>
27+
<Import Project="..\targets\ReferenceEmbeddedCsWinRTProject.targets" />
3428

3529
<ItemGroup>
3630
<ProjectReference Include="..\Microsoft.Management.Configuration.Processor\Microsoft.Management.Configuration.Processor.csproj" />

0 commit comments

Comments
 (0)