Repository navigation
Fix 'dotnet publish -o .' excluding all source files (CS5001) - #55461
Conversation
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]>
|
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. |
There was a problem hiding this comment.
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
DefaultItemExcludesPublishDir guard to use[MSBuild]::NormalizeDirectory(...)for stable directory equality comparisons. - Strengthen the regression test to set
PublishDirto 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). |
|
@jjonescz since customers were reporting that 55295 was still broking in preview6, I had copilot take a look and this is it's proposal. |
|
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
|
@jjonescz fixed the copilot review comment in the test so needs a rereview. |
Fixes #55295
Problem
dotnet publish -o .(also-o ./and-o ../<projname>) fails withCSC error CS5001: Program does not contain a static 'Main' methodfor 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 inmainbut 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 toDefaultItemExcludesexcludes 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:NormalizePath(projDir)...\proj(no slash)NormalizePath(projDir, '<absProjDir>\')...\proj\(trailing slash) → not equal → guard failsThe CLI always forwards
PublishDiras an absolute path with a trailing separator (OptionExtensions.ForwardAsOutputPath→GetFullPath+ appendPath.DirectorySeparatorChar). So the guard comparedC:\proj\againstC:\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]::NormalizeDirectoryon 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:NormalizeDirectory(projDir, '<absProjDir>\')...\proj\→ equalNormalizeDirectory(projDir, './out/')...\proj\out\→ differsNormalizeDirectoryis already widely used in SDK targets, so no new dependency/risk. A comment was added explaining whyNormalizeDirectory(notNormalizePath) is required, to prevent a future regression.Test
Strengthened
It_does_not_exclude_source_files_when_publish_dir_is_project_rootto setPublishDirto$(MSBuildProjectDirectory)\— the absolute trailing-slash value the CLI actually forwards — which is the case that regressed. The existingIt_excludes_items_in_publish_directorytest continues to guard the real-subdirectory case.Validation
build.cmd -c Debugsucceeds.GivenThereAreDefaultItemspass against the built SDK, including both the root-dir (not excluded) and subdir (excluded) cases.