Skip to content

Fix 'dotnet publish -o .' excluding all source files (CS5001) - #55461

Merged
marcpopMSFT merged 2 commits into
mainfrom
marcpopmsft-fix-publish-top-level-statements-output
Jul 28, 2026
Merged

marcpopMSFT merged 2 commits into
mainfrom
marcpopmsft-fix-publish-top-level-statements-output

Conversation

@marcpopMSFT

Copy link
Copy Markdown
Member

Fixes #55295

Problem

dotnet publish -o . (also -o ./ and -o ../<projname>) fails with CSC error CS5001: Program does not contain a static 'Main' method for projects with top-level statements (and silently produces an empty assembly otherwise). This is a regression of #53626 whose fix (#53923, merged April) is present in main but ineffective for the value the CLI actually forwards — so the customer is correct that it still repros in 11.0 preview 6 / main.

Root cause

The $(PublishDir)/** exclusion added to DefaultItemExcludes excludes every source file when the publish directory equals the project directory. #53923 added a guard to skip the exclusion in that case, but it used [MSBuild]::NormalizePath, which preserves the input's trailing slash:

Expression Result
NormalizePath(projDir) ...\proj (no slash)
NormalizePath(projDir, '<absProjDir>\') ...\proj\ (trailing slash) → not equal → guard fails

The CLI always forwards PublishDir as an absolute path with a trailing separator (OptionExtensions.ForwardAsOutputPath → GetFullPath + append Path.DirectorySeparatorChar). So the guard compared C:\proj\ against C:\proj, found them unequal, and re-applied the exclusion → all source excluded → CS5001.

The #53923 regression test only set PublishDir=.\ (relative), which never simulated the absolute-trailing-slash value the CLI emits, so it gave false confidence.

Fix

Use [MSBuild]::NormalizeDirectory on both operands. It always emits a consistent trailing slash, so equal directories compare equal regardless of the input's trailing slash, while real publish subdirectories still differ and remain correctly excluded:

Expression Result Behavior
NormalizeDirectory(projDir, '<absProjDir>\') ...\proj\ → equal correctly not excluded
NormalizeDirectory(projDir, './out/') ...\proj\out\ → differs correctly excluded

NormalizeDirectory is already widely used in SDK targets, so no new dependency/risk. A comment was added explaining why NormalizeDirectory (not NormalizePath) is required, to prevent a future regression.

Test

Strengthened It_does_not_exclude_source_files_when_publish_dir_is_project_root to set PublishDir to $(MSBuildProjectDirectory)\ — the absolute trailing-slash value the CLI actually forwards — which is the case that regressed. The existing It_excludes_items_in_publish_directory test continues to guard the real-subdirectory case.

Validation

  • Full build.cmd -c Debug succeeds.
  • All 22 tests in GivenThereAreDefaultItems pass against the built SDK, including both the root-dir (not excluded) and subdir (excluded) cases.

The PublishDir exclusion guard added in #53923 used [MSBuild]::NormalizePath,
which preserves the input's trailing slash. The CLI forwards PublishDir as an
absolute path WITH a trailing separator (OptionExtensions.ForwardAsOutputPath),
so the guard compared 'C:\proj\' against 'C:\proj', found them unequal, and
re-added $(PublishDir)/** to DefaultItemExcludes -- excluding every source
file and producing an empty assembly (CS5001 for top-level statements).

Switch both operands to [MSBuild]::NormalizeDirectory, which always emits a
consistent trailing slash, so the project directory compares equal regardless
of the input's trailing slash while real publish subdirectories still differ
and remain excluded. Strengthen the regression test to use the absolute
trailing-slash value the CLI actually forwards.

Fixes #55295

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings July 24, 2026 18:14
@marcpopMSFT
marcpopMSFT requested a review from a team as a code owner July 24, 2026 18:14
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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

Fixes a regression where dotnet publish -o . (and similar) could exclude all source files when PublishDir resolves to the project directory, causing empty outputs or CS5001 for top-level-statement apps. The change adjusts the MSBuild guard to compare directories in a trailing-separator-insensitive way and strengthens the associated regression test.

Changes:

  • Update DefaultItemExcludes PublishDir guard to use [MSBuild]::NormalizeDirectory(...) for stable directory equality comparisons.
  • Strengthen the regression test to set PublishDir to an absolute project directory value with a trailing separator (matching what the CLI forwards).

Reviewed changes

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

File Description
src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets Switch PublishDir-vs-project-dir comparison to NormalizeDirectory to avoid trailing-slash mismatches and prevent excluding all sources.
test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs Update regression test to emulate the CLI’s forwarded PublishDir (absolute path + trailing separator).

@marcpopMSFT
marcpopMSFT requested a review from jjonescz July 24, 2026 19:46
@marcpopMSFT

Copy link
Copy Markdown
Member Author

@jjonescz since customers were reporting that 55295 was still broking in preview6, I had copilot take a look and this is it's proposal.

@marcpopMSFT

Copy link
Copy Markdown
Member Author

Tested in a codespace and confirmed the original report failed before and passes now.

Replace the hard-coded backslash in the PublishDir value with
[MSBuild]::EnsureTrailingSlash so the test forwards a platform-correct
trailing separator, matching what the CLI produces on all platforms.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 781e1cb3-7f23-4d05-a226-648eb9db5a5e
@marcpopMSFT
marcpopMSFT enabled auto-merge (squash) July 27, 2026 21:06
@marcpopMSFT
marcpopMSFT requested a review from jjonescz July 27, 2026 21:06
@marcpopMSFT

Copy link
Copy Markdown
Member Author

@jjonescz fixed the copilot review comment in the test so needs a rereview.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot publish a project with top-level statements if an output directory is specified

3 participants