Skip to content

Fix dotnet build file.cs with MSBuild logger options - #54172

Open
xoofx wants to merge 10 commits into
dotnet:mainfrom
xoofx:fix-dotnet-build-cs-with-logger
Open

Fix dotnet build file.cs with MSBuild logger options#54172
xoofx wants to merge 10 commits into
dotnet:mainfrom
xoofx:fix-dotnet-build-cs-with-logger

Conversation

@xoofx

@xoofx xoofx commented May 4, 2026

Copy link
Copy Markdown
Member

Hello, this PR fixes file-based dotnet build detection when MSBuild logger options are passed before the C# file, for example:

dotnet build --logger:xyz Program.cs
dotnet build -l:xyz Program.cs

Previously, Program.cs could stop being recognized as a file-based app entry point and instead be forwarded to physical MSBuild as a project file. MSBuild would then try to parse the C# file as XML and fail with MSB4025.

I assume that this could be backported to net10.0 ?

Root cause

dotnet build did not define --logger / -l as known CLI options. As a result, logger arguments were handled as unmatched/MSBuild-style tokens during parsing, which interfered with the SlnOrProjectOrFileArgument values used by file-based build detection.

Fix

Adds hidden --logger / -l options to BuildCommandDefinition and forwards them to MSBuild as:

--logger:<value>

The option uses AllowSingleArgPerToken() so forms like the following preserve Program.cs as the single file-based entry-point argument:

dotnet build --logger xyz Program.cs
dotnet build --logger:xyz Program.cs
dotnet build -l xyz Program.cs
dotnet build -l:xyz Program.cs

Tests

Added regression coverage verifying that all logger forms above still produce a VirtualProjectBuildingCommand and forward --logger:xyz to MSBuild, instead of treating Program.cs as a project file.

Copilot AI review requested due to automatic review settings May 4, 2026 19:00

Copilot AI left a comment

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.

Pull request overview

This PR updates dotnet build so --logger / -l are parsed as recognized CLI options, preventing file-based Program.cs builds from being misclassified as regular MSBuild project inputs when logger options appear before the .cs file.

Changes:

  • Added hidden --logger / -l option handling to BuildCommandDefinition.
  • Forwarded parsed logger values to MSBuild in normalized --logger:<value> form.
  • Added regression tests covering file-based build detection with logger options before Program.cs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Cli/Microsoft.DotNet.Cli.Definitions/Commands/Build/BuildCommandDefinition.cs Adds hidden logger option parsing/forwarding for dotnet build.
test/dotnet.Tests/CommandTests/MSBuild/GivenDotnetBuildInvocation.cs Adds regression tests for file-based build detection with logger arguments.

Comment thread src/Cli/Microsoft.DotNet.Cli.Definitions/Commands/Build/BuildCommandDefinition.cs Outdated
@xoofx

xoofx commented May 7, 2026

Copy link
Copy Markdown
Member Author

cc: @marcpopMSFT or @dsplaisted if you can help to accept/merge this PR, thank you! ☺️

@dsplaisted

Copy link
Copy Markdown
Member

Thanks! I would recommend moving this to CommonOptions and then also adding it to publish, pack, clean, and restore, so it works in those cases too.

@xoofx
xoofx force-pushed the fix-dotnet-build-cs-with-logger branch from 91d694e to 829780f Compare May 7, 2026 20:29
@xoofx

xoofx commented May 7, 2026

Copy link
Copy Markdown
Member Author

Thanks! I would recommend moving this to CommonOptions and then also adding it to publish, pack, clean, and restore, so it works in those cases too.

Good call! Fixed via 829780f

@xoofx
xoofx force-pushed the fix-dotnet-build-cs-with-logger branch from 829780f to 83c35f9 Compare May 9, 2026 06:26
@xoofx

xoofx commented Jun 18, 2026

Copy link
Copy Markdown
Member Author

@dsplaisted will it be possible to merge this PR ?

@dsplaisted

Copy link
Copy Markdown
Member

@xoofx sorry for the delay, there are some merge conflicts and test failures that need to be resolved.

…ith-logger

# Conflicts:
#	test/dotnet.Tests/CommandTests/MSBuild/GivenDotnetBuildInvocation.cs
#	test/dotnet.Tests/CommandTests/MSBuild/GivenDotnetCleanInvocation.cs
#	test/dotnet.Tests/CommandTests/MSBuild/GivenDotnetPackInvocation.cs
#	test/dotnet.Tests/CommandTests/MSBuild/GivenDotnetPublishInvocation.cs
#	test/dotnet.Tests/CommandTests/MSBuild/GivenDotnetRestoreInvocation.cs
@xoofx
xoofx force-pushed the fix-dotnet-build-cs-with-logger branch from ced3d60 to 4ece4cf Compare August 4, 2026 18:58
@xoofx
xoofx requested a review from a team as a code owner August 4, 2026 18:58
@xoofx

xoofx commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

@dsplaisted the branch has been updated with merge conflicts resolved.

@dsplaisted

Copy link
Copy Markdown
Member

@xoofx Thanks, could you look at the test failures? Looks like there are two of them now.

@xoofx

xoofx commented Aug 8, 2026

Copy link
Copy Markdown
Member Author

Latest commit is green, just rebased

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants