Skip to content

Commit 3b86353

Browse files
authored
Exclude PublishDir from default items to prevent recursive artifacts in file-based apps (#51417)
1 parent 39856f4 commit 3b86353

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Copyright (c) .NET Foundation. All rights reserved.
3535
<DefaultItemExcludes>$(DefaultItemExcludes);$(BaseOutputPath)/**</DefaultItemExcludes>
3636
<!-- obj folder, by default -->
3737
<DefaultItemExcludes>$(DefaultItemExcludes);$(BaseIntermediateOutputPath)/**</DefaultItemExcludes>
38+
<!-- Exclude PublishDir to prevent published artifacts from being included in subsequent builds/publishes.
39+
While in most cases PublishDir is contained within BaseOutputPath or BaseIntermediateOutputPath,
40+
this is by no means required, so we should protect against this happening here. -->
41+
<DefaultItemExcludes Condition="'$(PublishDir)' != ''">$(DefaultItemExcludes);$(PublishDir)/**</DefaultItemExcludes>
3842

3943
<!-- Various files that should generally always be ignored -->
4044
<DefaultItemExcludes>$(DefaultItemExcludes);**/*.user</DefaultItemExcludes>

test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,35 @@ public void It_does_not_include_Windows_App_SDK_items_if_Windows_App_SDK_is_abse
854854
.BeEmpty();
855855
}
856856

857+
[Fact]
858+
public void It_excludes_items_in_publish_directory()
859+
{
860+
Action<GetValuesCommand> setup = getValuesCommand =>
861+
{
862+
// Create a PublishDir with a JSON file (simulating a previous publish)
863+
string publishDir = Path.Combine(getValuesCommand.ProjectRootPath, "artifacts", "TestLibrary");
864+
WriteFile(Path.Combine(publishDir, "appsettings.json"),
865+
"{ \"Setting\": \"Value\" }");
866+
867+
WriteFile(Path.Combine(getValuesCommand.ProjectRootPath, "Code", "Class1.cs"),
868+
"public class Class1 {}");
869+
};
870+
871+
Action<XDocument> projectChanges = project =>
872+
{
873+
var ns = project.Root.Name.Namespace;
874+
875+
var propertyGroup = new XElement(ns + "PropertyGroup");
876+
project.Root.Add(propertyGroup);
877+
propertyGroup.Add(new XElement(ns + "PublishDir", "artifacts\\TestLibrary\\"));
878+
};
879+
880+
var noneItems = GivenThatWeWantToBuildALibrary.GetValuesFromTestLibrary(Log, _testAssetsManager, "None", setup, projectChanges: projectChanges);
881+
882+
// The appsettings.json file in the PublishDir should not be included in None items
883+
noneItems.Should().NotContain(item => item.Contains("appsettings.json"));
884+
}
885+
857886
void RemoveGeneratedCompileItems(List<string> compileItems)
858887
{
859888
// Remove auto-generated compile items.

0 commit comments

Comments
 (0)