From e166396f9df1fb59d51983749e912cb4b434f3c3 Mon Sep 17 00:00:00 2001 From: Erik Mavrinac Date: Thu, 7 Dec 2023 13:08:56 -0800 Subject: [PATCH] Clone the delete-before-copy escape hatch from MSBuild Copy.cs to allow turning it off in some repos (#511) --- src/CopyOnWrite/Copy.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/CopyOnWrite/Copy.cs b/src/CopyOnWrite/Copy.cs index 4e73cc6..e3061f6 100644 --- a/src/CopyOnWrite/Copy.cs +++ b/src/CopyOnWrite/Copy.cs @@ -26,6 +26,9 @@ public class Copy : Task, ICancelableTask internal const string AlwaysOverwriteReadOnlyFilesEnvVar = "MSBUILDALWAYSOVERWRITEREADONLYFILES"; private static readonly bool IsWindows = Environment.OSVersion.Platform == PlatformID.Win32NT; + // Escape hatch taken from MSBuild Traits.cs that allows turning off delete-before-copy logic. + private static readonly bool CopyWithoutDeleteEscapeHatch = Environment.GetEnvironmentVariable("MSBUILDCOPYWITHOUTDELETE") == "1"; + // Default parallelism determined empirically - times below are in seconds spent in the Copy task building this repo // with "build -skiptests -rebuild -configuration Release /ds" (with hack to build.ps1 to disable creating selfhost // build for non-selfhost first build; implies first running build in repo to pull packages and create selfhost) @@ -265,7 +268,8 @@ private void LogDiagnostic(string message, params object[] messageArgs) MakeFileWriteable(destinationFileState, true); } - if (destinationFileState.FileExists && + if (!CopyWithoutDeleteEscapeHatch && + destinationFileState.FileExists && !destinationFileState.IsReadOnly) { FileUtilities.DeleteNoThrow(destinationFileState.Name);