diff --git a/eng/BootStrapMSBuild.targets b/eng/BootStrapMSBuild.targets index d002c7c00b4..02e1e308112 100644 --- a/eng/BootStrapMSBuild.targets +++ b/eng/BootStrapMSBuild.targets @@ -68,6 +68,9 @@ + + + diff --git a/src/MSBuild/MSBuild.csproj b/src/MSBuild/MSBuild.csproj index 3be3acbe784..7d2a9b9e875 100644 --- a/src/MSBuild/MSBuild.csproj +++ b/src/MSBuild/MSBuild.csproj @@ -247,7 +247,7 @@ - + - + diff --git a/src/Tasks.UnitTests/WriteLinesToFile_Tests.cs b/src/Tasks.UnitTests/WriteLinesToFile_Tests.cs index 0b3d12f099e..d5495b9dfab 100644 --- a/src/Tasks.UnitTests/WriteLinesToFile_Tests.cs +++ b/src/Tasks.UnitTests/WriteLinesToFile_Tests.cs @@ -238,6 +238,57 @@ public void QuestionWriteLinesWriteOnlyWhenDifferentTest() } } + /// + /// Question WriteLines to return true when Lines are empty. + /// + [Fact] + public void QuestionWriteLinesWhenLinesAreEmpty() + { + // Test the combination of: + // 1) File exists + // 2) Overwrite + // 3) WriteOnlyWhenDifferent + + var fileExists = FileUtilities.GetTemporaryFile(); + var fileNotExists = FileUtilities.GetTemporaryFileName(); + try + { + TestWriteLines(fileExists, fileNotExists, Overwrite: true, WriteOnlyWhenDifferent: true); + TestWriteLines(fileExists, fileNotExists, Overwrite: false, WriteOnlyWhenDifferent: true); + TestWriteLines(fileExists, fileNotExists, Overwrite: true, WriteOnlyWhenDifferent: false); + TestWriteLines(fileExists, fileNotExists, Overwrite: false, WriteOnlyWhenDifferent: false); + } + finally + { + File.Delete(fileExists); + } + + void TestWriteLines(string fileExists, string fileNotExists, bool Overwrite, bool WriteOnlyWhenDifferent) + { + var test1 = new WriteLinesToFile + { + Overwrite = Overwrite, + BuildEngine = new MockEngine(_output), + File = new TaskItem(fileExists), + WriteOnlyWhenDifferent = WriteOnlyWhenDifferent, + FailIfNotIncremental = true, + // Tests Lines = null. + }; + test1.Execute().ShouldBeTrue(); + + var test2 = new WriteLinesToFile + { + Overwrite = Overwrite, + BuildEngine = new MockEngine(_output), + File = new TaskItem(fileNotExists), + WriteOnlyWhenDifferent = WriteOnlyWhenDifferent, + FailIfNotIncremental = true, + Lines = Array.Empty(), // Test empty. + }; + test2.Execute().ShouldBeTrue(); + } + } + /// /// Should create directory structure when target does not exist. /// diff --git a/src/Tasks/FileIO/WriteLinesToFile.cs b/src/Tasks/FileIO/WriteLinesToFile.cs index 7ae0228c8e5..9b94858fc12 100644 --- a/src/Tasks/FileIO/WriteLinesToFile.cs +++ b/src/Tasks/FileIO/WriteLinesToFile.cs @@ -133,8 +133,11 @@ public override bool Execute() if (FailIfNotIncremental) { - Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorReadingFile", File.ItemSpec); - return false; + if (Lines?.Length > 0) + { + Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorReadingFile", File.ItemSpec); + return false; + } } else { @@ -143,7 +146,7 @@ public override bool Execute() } else { - if (FailIfNotIncremental) + if (FailIfNotIncremental && Lines?.Length > 0) { Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorOrWarning", File.ItemSpec, string.Empty); return false;