Skip to content

Commit b2ca669

Browse files
Copilotbaronfel
andcommitted
Add exclusion for PublishDir in DefaultItemExcludes
Co-authored-by: baronfel <[email protected]>
1 parent 8eeeb57 commit b2ca669

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ Copyright (c) .NET Foundation. All rights reserved.
5151
<DefaultExcludesInProjectFolder>$(DefaultExcludesInProjectFolder);$(DefaultItemExcludesInProjectFolder);**/.*/**</DefaultExcludesInProjectFolder>
5252
</PropertyGroup>
5353

54+
<!-- Exclude PublishDir to prevent published artifacts from being included in subsequent builds/publishes.
55+
This is especially important for file-based apps where PublishDir is set to ./artifacts/<appname> next to the app file. -->
56+
<PropertyGroup Condition="'$(PublishDir)' != ''">
57+
<DefaultItemExcludes>$(DefaultItemExcludes);$(PublishDir)/**</DefaultItemExcludes>
58+
</PropertyGroup>
59+
5460
<!-- Set the default versions of the NETStandard.Library or Microsoft.NETCore.App packages to reference.
5561
The implicit package references themselves are defined in Microsoft.NET.Sdk.props, so that they can be overridden
5662
in the project file. -->

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)