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 8d04b5e7c5f4..5d73e4d03ce3 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
@@ -35,6 +35,10 @@ Copyright (c) .NET Foundation. All rights reserved.
$(DefaultItemExcludes);$(BaseOutputPath)/**
$(DefaultItemExcludes);$(BaseIntermediateOutputPath)/**
+
+ $(DefaultItemExcludes);$(PublishDir)/**
$(DefaultItemExcludes);**/*.user
diff --git a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs
index 958b7cd48f66..d9fb34f84b41 100644
--- a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs
+++ b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs
@@ -854,6 +854,35 @@ public void It_does_not_include_Windows_App_SDK_items_if_Windows_App_SDK_is_abse
.BeEmpty();
}
+ [Fact]
+ public void It_excludes_items_in_publish_directory()
+ {
+ Action setup = getValuesCommand =>
+ {
+ // Create a PublishDir with a JSON file (simulating a previous publish)
+ string publishDir = Path.Combine(getValuesCommand.ProjectRootPath, "artifacts", "TestLibrary");
+ WriteFile(Path.Combine(publishDir, "appsettings.json"),
+ "{ \"Setting\": \"Value\" }");
+
+ WriteFile(Path.Combine(getValuesCommand.ProjectRootPath, "Code", "Class1.cs"),
+ "public class Class1 {}");
+ };
+
+ Action projectChanges = project =>
+ {
+ var ns = project.Root.Name.Namespace;
+
+ var propertyGroup = new XElement(ns + "PropertyGroup");
+ project.Root.Add(propertyGroup);
+ propertyGroup.Add(new XElement(ns + "PublishDir", "artifacts\\TestLibrary\\"));
+ };
+
+ var noneItems = GivenThatWeWantToBuildALibrary.GetValuesFromTestLibrary(Log, _testAssetsManager, "None", setup, projectChanges: projectChanges);
+
+ // The appsettings.json file in the PublishDir should not be included in None items
+ noneItems.Should().NotContain(item => item.Contains("appsettings.json"));
+ }
+
void RemoveGeneratedCompileItems(List compileItems)
{
// Remove auto-generated compile items.