[Build] Separate WinRT props from common .NET props, make verify script more robust and faster (#48059)

<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request
Separates out the common .NET build properties from
**Common.Dotnet.CsWinRT.props** into a new file so POCO libraries don't
have to import WinRT or add exclusions to **verifyCommonProps.ps1**.
Also updates the verify script for robustness and speed.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [ ] Closes: #xxx
<!-- - [ ] Closes: #yyy (add separate lines for additional resolved
issues) -->
- [x] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [ ] **Tests:** Added/updated and all pass
- [ ] **Localization:** All end-user-facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments
This is a follow-on from #47211, which included a C# project that didn't
target WinRT. Previously, all C# projects were mandated to include
**Common.Dotnet.CsWinRT.props**, even if they didn't need the WinRT
import, because the common .NET build properties like `TargetFramework`
and Debug/Release configuration were included in the same file.

This PR separates out the non-WinRT information into a new
**Common.Dotnet.props** file. The existing
**Common.Dotnet.CsWinRT.props** file imports this, meaning no changes
are required for existing C# projects.

Additionally, the **verifyCommonProps.ps1** script has been updated to
remove redundant exclusions, add checks for malformed XML, and to speed
up the scan.

### Changes to verifyCommonProps.ps1
The following updates were made:

- Added descriptive header and param info.
- Now using .NET's `EnumerateFiles()` instead of Powershell's slow file
enumeration.
- Now using `XmlDocument.Load()` to quickly load the content of the
file.
- Parsing the document now uses `GetElementsByTagName()` with a '*'
wildcard for the namespace to pull out `Import` tags regardless of
location or ns prefix.
- Removed prior exclusions for **Microsoft.CmdPal.Core.*** and
**Microsoft.CmdPal.Ext.Shell** projects. There are no Core projects any
longer and the **Microsoft.CmdPal.Ext.Shell** project already includes
an import for **Common.Dotnet.CsWinRT.props**.
- Filename comparisons now use an exact match to the filename itself
rather than a wildcard substring match. This means the check is robust
against project names with the same suffix.
- Early exit `break` on successful match, so the whole file need not be
scanned.
- `try/catch` added to prevent a .csproj XML parsing error from breaking
the CI.

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed

- Built all Quick Accent projects and confirmed all unit tests passed.
- Edited a .csproj to exclude the end tag. Ran **verifyCommonProps.ps1**
to confirm the parsing error was reported.
- Edited **verifyCommonProps.ps1** to remove the exclusion for
**TemplateCmdPalExtension.csproj**. Ran the script to confirm that the
file was correctly flagged.
- Edited **PowerAccent.Common.csproj** to remove the Import for
**Common.Dotnet.props**. Ran the verify script to confirm that the file
was correctly flagged.
- Edited **PowerAccent.Core.csproj** to remove the Import for
**Common.Dotnet.CsWinRT.props**. Ran the verify script to confirm that
the file was correctly flagged.

## Verify Script Performance

File cache|Before (ms)|After (ms)
--|--|--
Cold|3123|1739
Warm|1849|686
This commit is contained in:
Dave Rayment
2026-08-18 07:44:10 +01:00
committed by GitHub
parent 93aeae9aa1
commit 0087d2d576
5 changed files with 96 additions and 82 deletions

View File

@@ -1,61 +1,65 @@
<#
.SYNOPSIS
Validates that all C# projects in the repository import a required shared props file.
.DESCRIPTION
Recursively searches for .csproj files under the given root directory and checks that
each one imports either Common.Dotnet.CsWinRT.props or Common.Dotnet.props. These
shared MSBuild props files enforce consistent build settings across all C# projects.
.PARAMETER sourceDir
Root directory to recursively search for .csproj files.
.OUTPUTS
Writes the path of any non-conforming or malformed .csproj file to the output stream.
Exits with code 1 if any such files are found, and with code 0 otherwise.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True, Position = 1)]
[string]$sourceDir
)
# scan all csharp project in the source directory
function Get-CSharpProjects {
param (
[string]$path
)
# Get all .csproj files under the specified path
return Get-ChildItem -Path $path -Recurse -Filter *.csproj | Select-Object -ExpandProperty FullName
}
# Check if the project file imports 'Common.Dotnet.CsWinRT.props'
function Test-ImportSharedCsWinRTProps {
param (
[string]$filePath
)
# Load the XML content of the .csproj file
[xml]$csprojContent = Get-Content -Path $filePath
# Check if the Import element with Project attribute containing 'Common.Dotnet.CsWinRT.props' exists
return $csprojContent.Project.Import | Where-Object { $null -ne $_.Project -and $_.Project.EndsWith('Common.Dotnet.CsWinRT.props') }
}
# Call the function with the provided source directory
$csprojFilesArray = Get-CSharpProjects -path $sourceDir
$hasInvalidCsProj = $false
# Enumerate the array of file paths and call Validate-ImportSharedCsWinRTProps for each file
foreach ($csprojFile in $csprojFilesArray) {
# Skip if the file ends with 'TemplateCmdPalExtension.csproj'
if ($csprojFile -like '*TemplateCmdPalExtension.csproj') {
continue
}
# The CmdPal.Core projects use a common shared props file, so skip them
if ($csprojFile -like '*Microsoft.CmdPal.Core.*.csproj') {
continue
}
if ($csprojFile -like '*Microsoft.CmdPal.Ext.Shell.csproj') {
$csprojFiles = [System.IO.Directory]::EnumerateFiles($sourceDir, '*.csproj', [System.IO.SearchOption]::AllDirectories)
foreach ($csprojFile in $csprojFiles) {
$filename = [System.IO.Path]::GetFileName($csprojFile)
# Skip the CmdPal extension template project, which doesn't require the shared props.
if ($filename -eq 'TemplateCmdPalExtension.csproj') {
continue
}
# The PowerAccent.Common project does not target WinRT, so skip it
if ($csprojFile -like '*PowerAccent.Common.csproj') {
$importExists = $false
try {
$xml = New-Object System.Xml.XmlDocument
$xml.Load($csprojFile)
# The '*' wildcard matches Import elements regardless of XML namespace.
foreach ($importNode in $xml.GetElementsByTagName('Import', '*')) {
if ($null -ne $importNode.Project) {
$importFilename = [System.IO.Path]::GetFileName($importNode.Project)
if ($importFilename -eq 'Common.Dotnet.CsWinRT.props' -or $importFilename -eq 'Common.Dotnet.props') {
$importExists = $true
break
}
}
}
}
catch {
Write-Output "Error parsing ${csprojFile}: $_"
$hasInvalidCsProj = $true
continue
}
$importExists = Test-ImportSharedCsWinRTProps -filePath $csprojFile
if (!$importExists) {
Write-Output "$csprojFile need to import 'Common.Dotnet.CsWinRT.props'."
if (-not $importExists) {
Write-Output "$csprojFile needs to import 'Common.Dotnet.CsWinRT.props' or 'Common.Dotnet.props'."
$hasInvalidCsProj = $true
}
}
@@ -64,4 +68,4 @@ if ($hasInvalidCsProj) {
exit 1
}
exit 0
exit 0

View File

@@ -1219,6 +1219,7 @@
<File Path="src/.editorconfig" />
<File Path="src/Common.Dotnet.AotCompatibility.props" />
<File Path="src/Common.Dotnet.CsWinRT.props" />
<File Path="src/Common.Dotnet.props" />
<File Path="src/Common.SelfContained.props" />
<File Path="src/Monaco.props" />
<File Path="src/Solution.props" />

View File

@@ -1,47 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Some items may be set in Directory.Build.props in root -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Import the base .NET properties -->
<Import Project=".\Common.Dotnet.props" />
<!-- Setup folder structure for CsWinRT generated files -->
<Import Project=".\Common.Dotnet.PrepareGeneratedFolder.targets" />
<PropertyGroup>
<CoreTargetFramework>net10.0</CoreTargetFramework>
<WindowsSdkPackageVersion>10.0.26100.68-preview</WindowsSdkPackageVersion>
<TargetFramework>$(CoreTargetFramework)-windows10.0.26100.0</TargetFramework>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
<RuntimeIdentifiers>win-x64;win-arm64</RuntimeIdentifiers>
</PropertyGroup>
<!-- Common from the debug / release items -->
<PropertyGroup>
<WarningLevel>4</WarningLevel>
<NoWarn></NoWarn>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>CA1824;CA1416;CA1720;CA1859;CA2263;CA2022;MVVMTK0045;MVVMTK0049</WarningsNotAsErrors>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<DebugSymbols>true</DebugSymbols>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<ErrorReport>prompt</ErrorReport>
<DefineConstants>RELEASE;TRACE</DefineConstants>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWinRT" />
</ItemGroup>
<!-- this may need to be removed on future CsWinRT upgrades-->
<!-- This may need to be removed on future CsWinRT upgrades-->
<Target Name="RemoveCsWinRTPackageAnalyzer" BeforeTargets="CoreCompile">
<ItemGroup>
<Analyzer Remove="@(Analyzer)" Condition="%(Analyzer.NuGetPackageId) == 'Microsoft.Windows.CsWinRT'" />
<Analyzer Remove="@(Analyzer)" Condition="%(Analyzer.NuGetPackageId) == 'Microsoft.Windows.CsWinRT'" />
</ItemGroup>
</Target>
</Project>

38
src/Common.Dotnet.props Normal file
View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Common Framework and Platform Settings -->
<PropertyGroup>
<CoreTargetFramework>net10.0</CoreTargetFramework>
<WindowsSdkPackageVersion>10.0.26100.68-preview</WindowsSdkPackageVersion>
<TargetFramework>$(CoreTargetFramework)-windows10.0.26100.0</TargetFramework>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
<RuntimeIdentifiers>win-x64;win-arm64</RuntimeIdentifiers>
</PropertyGroup>
<!-- Common Warning/Error Settings -->
<PropertyGroup>
<WarningLevel>4</WarningLevel>
<NoWarn></NoWarn>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<WarningsNotAsErrors>CA1824;CA1416;CA1720;CA1859;CA2263;CA2022;MVVMTK0045;MVVMTK0049</WarningsNotAsErrors>
</PropertyGroup>
<!-- Debug Configuration -->
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<DebugSymbols>true</DebugSymbols>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
</PropertyGroup>
<!-- Release Configuration -->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<ErrorReport>prompt</ErrorReport>
<DefineConstants>RELEASE;TRACE</DefineConstants>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
</PropertyGroup>
</Project>

View File

@@ -1,12 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Look at Directory.Build.props in root for common stuff as well -->
<Import Project="$(RepoRoot)src\Common.Dotnet.props" />
<Import Project="$(RepoRoot)src\Common.Dotnet.AotCompatibility.props" />
<PropertyGroup>
<!-- Currently hard-coded, as this project does not target WinRT.
To be removed after non-WinRT information is moved from
Common.Dotnet.CsWinRT.props. -->
<TargetFramework>net10.0-windows10.0.26100.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<!-- Required by the CsWinRT AOT optimizer: marshaling generic collections (e.g. the