Consolidate duplicate MSBuildUtilities copies (#51487)#54947
Open
mthalman wants to merge 1 commit into
Open
Conversation
The MSBuildUtilities helper existed as two byte-identical-bodied copies differing only by namespace: one in src/Common (Microsoft.DotNet.Cli, used by the SDK MSBuild build tasks) and one in src/Cli/Microsoft.DotNet.FileBasedPrograms (Microsoft.DotNet.FileBasedPrograms, shipped in the source package and consumed by Roslyn, the dotnet CLI, and ProjectTools). Keep a single physical file in src/Common and switch the namespace with an MSBUILD_BUILD_TASKS compile symbol. The default (no symbol) form matches the file as it originally shipped in the source package (Microsoft.DotNet.FileBasedPrograms, #nullable enable, using System;) so external consumers see no change. The two build-tasks projects define MSBUILD_BUILD_TASKS to select the Microsoft.DotNet.Cli namespace and suppress the redundant nullable/using directives (which would otherwise trip IDE0240/IDE0005 there). The class body is unchanged. Verified by building all consumers (build tasks, ProjectTools, source package, dotnet CLI), inspecting the compiled assemblies' metadata to confirm the per-assembly namespaces, and running the build-tasks unit tests that exercise ConvertStringToBool. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
8 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes the duplicated MSBuildUtilities source by consolidating both byte-identical copies into src/Common/MSBuildUtilities.cs, using an MSBUILD_BUILD_TASKS compilation symbol to select the appropriate namespace for each consumer (build tasks vs. FileBasedPrograms source package).
Changes:
- Added
MSBUILD_BUILD_TASKSconstant to the two MSBuild build-task projects so the shared file compiles intoMicrosoft.DotNet.Cli. - Updated
src/Common/MSBuildUtilities.csto switch namespaces (and file-level directives) via#if MSBUILD_BUILD_TASKS. - Removed the old FileBasedPrograms-local
MSBuildUtilities.csand linked the sharedsrc/Common/MSBuildUtilities.csinto the FileBasedPrograms shared items.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Tasks/Microsoft.NET.Build.Tasks/Microsoft.NET.Build.Tasks.csproj | Defines MSBUILD_BUILD_TASKS so the shared utility compiles under the build-task namespace. |
| src/Tasks/Microsoft.NET.Build.Extensions.Tasks/Microsoft.NET.Build.Extensions.Tasks.csproj | Adds MSBUILD_BUILD_TASKS alongside existing constants for consistent build-task compilation. |
| src/Common/MSBuildUtilities.cs | Centralizes the implementation and conditionally selects namespace/directives per consumer. |
| src/Cli/Microsoft.DotNet.FileBasedPrograms/MSBuildUtilities.cs | Deletes the redundant local copy now that the file is shared. |
| src/Cli/Microsoft.DotNet.FileBasedPrograms/Microsoft.DotNet.FileBasedPrograms.projitems | Links the shared src/Common/MSBuildUtilities.cs into the FileBasedPrograms source package inputs. |
This was referenced Jun 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
MSBuildUtilitiesexisted as two copies whose class bodies were byte-for-byte identical, differing only by namespace:src/Common/MSBuildUtilities.csinMicrosoft.DotNet.Cli, compiled into the SDK MSBuild build tasks.src/Cli/Microsoft.DotNet.FileBasedPrograms/MSBuildUtilities.csinMicrosoft.DotNet.FileBasedPrograms, shipped in the source package and consumed by Roslyn, the dotnet CLI, and ProjectTools.Keeping two copies in sync is exactly the duplication tracked by #51487.
What
Collapse the two copies into the single physical file
src/Common/MSBuildUtilities.csand switch the namespace at compile time with anMSBUILD_BUILD_TASKSsymbol:Microsoft.DotNet.FileBasedPrograms, with#nullable enableandusing System;. External and uncontrolled consumers (Roslyn, the dotnet CLI) compile the shipped contentFiles with no symbols defined, so this default keeps their input unchanged.Microsoft.NET.Build.TasksandMicrosoft.NET.Build.Extensions.Tasks) defineMSBUILD_BUILD_TASKS, which selects theMicrosoft.DotNet.Clinamespace and drops the nullable/using directives. Those projects enable nullable and provideSystemvia ImplicitUsings, so emitting the directives there would otherwise trip IDE0240/IDE0005.The class body is unchanged, so the emitted IL for
ConvertStringToBoolis identical to before. Net result is+27 / -72with one shared file instead of two.Validation
Microsoft.DotNet.ProjectTools, theMicrosoft.DotNet.FileBasedProgramssource package, and the dotnet CLI.Microsoft.DotNet.Cli.MSBuildUtilities;Microsoft.DotNet.ProjectTools.dllexposesMicrosoft.DotNet.FileBasedPrograms.MSBuildUtilities.MSBuildUtilities.csintocontentFiles/cs/{net9.0,netstandard2.0}/defaulting to theMicrosoft.DotNet.FileBasedProgramsnamespace, matching the prior shipped contract.ConvertStringToBool(ProcessFrameworkReferences: 39/39 passing).InternalAPI.Unshipped.txtis unchanged because the default namespace remainsMicrosoft.DotNet.FileBasedPrograms.Contributes to #51487