From 9da4d6c912028b01a74fb96c7e0f4df50b50a80a Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Fri, 24 Jul 2026 11:13:57 -0700 Subject: [PATCH 1/2] Fix 'dotnet publish -o .' excluding all source files (CS5001) 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 <223556219+Copilot@users.noreply.github.com> --- .../targets/Microsoft.NET.Sdk.DefaultItems.targets | 7 +++++-- .../Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets index db6ddd456520..171600d8be3d 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets @@ -38,8 +38,11 @@ Copyright (c) .NET Foundation. All rights reserved. - $(DefaultItemExcludes);$(PublishDir)/** + Guard against PublishDir being the project directory (e.g. 'dotnet publish -o .') which would exclude all source files. + Use NormalizeDirectory (not NormalizePath) on both operands so the comparison is trailing-slash insensitive: the CLI + forwards PublishDir as an absolute path with a trailing separator, and NormalizePath preserves the input's trailing + slash, so NormalizePath would treat 'C:\proj\' and 'C:\proj' as different and re-introduce the exclusion. --> + $(DefaultItemExcludes);$(PublishDir)/** $(DefaultItemExcludes);**/*.user diff --git a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs index 0e0d1b175fdd..bdb0d6cf9f99 100644 --- a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs +++ b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs @@ -897,8 +897,11 @@ public void It_does_not_exclude_source_files_when_publish_dir_is_project_root() var propertyGroup = new XElement(ns + "PropertyGroup"); project.Root.Add(propertyGroup); - // PublishDir set to the project directory itself (simulates 'dotnet publish -o .') - propertyGroup.Add(new XElement(ns + "PublishDir", ".\\")); + // PublishDir set to the absolute project directory with a trailing separator. + // This is exactly what the CLI forwards for 'dotnet publish -o .' (see + // OptionExtensions.ForwardAsOutputPath, which calls GetFullPath and appends a + // directory separator), and is the value that regressed in dotnet/sdk#55295. + propertyGroup.Add(new XElement(ns + "PublishDir", "$(MSBuildProjectDirectory)\\")); }; var compileItems = GivenThatWeWantToBuildALibrary.GetValuesFromTestLibrary(Log, TestAssetsManager, "Compile", setup, projectChanges: projectChanges); From 17b35b2a1e41f30476d1eba67b3b686125372177 Mon Sep 17 00:00:00 2001 From: Marc Paine Date: Mon, 27 Jul 2026 13:53:48 -0700 Subject: [PATCH 2/2] Use OS-aware trailing separator in publish-dir regression test 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 <223556219+Copilot@users.noreply.github.com> Copilot-Session: 781e1cb3-7f23-4d05-a226-648eb9db5a5e --- test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs index bdb0d6cf9f99..f7a9cafb5b9f 100644 --- a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs +++ b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs @@ -897,11 +897,12 @@ public void It_does_not_exclude_source_files_when_publish_dir_is_project_root() var propertyGroup = new XElement(ns + "PropertyGroup"); project.Root.Add(propertyGroup); - // PublishDir set to the absolute project directory with a trailing separator. + // PublishDir set to the absolute project directory with an OS-appropriate trailing separator. // This is exactly what the CLI forwards for 'dotnet publish -o .' (see // OptionExtensions.ForwardAsOutputPath, which calls GetFullPath and appends a // directory separator), and is the value that regressed in dotnet/sdk#55295. - propertyGroup.Add(new XElement(ns + "PublishDir", "$(MSBuildProjectDirectory)\\")); + // EnsureTrailingSlash yields the platform-correct separator so the test matches CLI behavior on all platforms. + propertyGroup.Add(new XElement(ns + "PublishDir", "$([MSBuild]::EnsureTrailingSlash($(MSBuildProjectDirectory)))")); }; var compileItems = GivenThatWeWantToBuildALibrary.GetValuesFromTestLibrary(Log, TestAssetsManager, "Compile", setup, projectChanges: projectChanges);