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 5d73e4d03ce3..db6ddd456520 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 @@ -37,8 +37,9 @@ Copyright (c) .NET Foundation. All rights reserved. $(DefaultItemExcludes);$(BaseIntermediateOutputPath)/** - $(DefaultItemExcludes);$(PublishDir)/** + this is by no means required, so we should protect against this happening here. + Guard against PublishDir being the project directory (e.g. 'dotnet publish -o .') which would exclude all source files. --> + $(DefaultItemExcludes);$(PublishDir)/** $(DefaultItemExcludes);**/*.user diff --git a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs index d9fb34f84b41..dc398b67df48 100644 --- a/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs +++ b/test/Microsoft.NET.Build.Tests/GivenThereAreDefaultItems.cs @@ -883,6 +883,32 @@ public void It_excludes_items_in_publish_directory() noneItems.Should().NotContain(item => item.Contains("appsettings.json")); } + [Fact] + public void It_does_not_exclude_source_files_when_publish_dir_is_project_root() + { + Action setup = getValuesCommand => + { + 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); + // PublishDir set to the project directory itself (simulates 'dotnet publish -o .') + propertyGroup.Add(new XElement(ns + "PublishDir", ".\\")); + }; + + var compileItems = GivenThatWeWantToBuildALibrary.GetValuesFromTestLibrary(Log, _testAssetsManager, "Compile", setup, projectChanges: projectChanges); + + RemoveGeneratedCompileItems(compileItems); + + compileItems.Should().BeEquivalentTo(new[] { Path.Combine("Code", "Class1.cs"), "Helper.cs" }); + } + void RemoveGeneratedCompileItems(List compileItems) { // Remove auto-generated compile items.