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..f7a9cafb5b9f 100644
--- a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs
+++ b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs
@@ -897,8 +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 project directory itself (simulates 'dotnet publish -o .')
- propertyGroup.Add(new XElement(ns + "PublishDir", ".\\"));
+ // 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.
+ // 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);