Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Microsoft.Android.Build.BaseTasks] retry when copying files (#245)
Context: dotnet/android#9133 Context: https://learn.microsoft.com/visualstudio/msbuild/copy-task?view=vs-2022 We sometimes get collisions between the Design-Time-Build (or AntiVirus) and our main build. This can result in errors such as: Error (active) XALNS7019 System.UnauthorizedAccessException: Access to the path 'D:\Projects\MauiApp2\obj\Debug\net9.0-android\android\assets\armeabi-v7a\MauiApp2.dll' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalDelete(String path, Boolean checkHost) at Microsoft.Android.Build.Tasks.Files.CopyIfChanged(String source, String destination) in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/Files.cs:line 125 at Xamarin.Android.Tasks.MonoAndroidHelper.CopyAssemblyAndSymbols(String source, String destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs:line 344 at Xamarin.Android.Tasks.LinkAssembliesNoShrink.CopyIfChanged(ITaskItem source, ITaskItem destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 161 at Xamarin.Android.Tasks.LinkAssembliesNoShrink.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 76 at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25 MauiApp2 (net9.0-android) C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.6.340\tools\Xamarin.Android.Common.targets 1407 If we look at the [MSBuild `<Copy/>` task][0] we see that it has a retry system in the cases of `UnauthorizedAccessException` or `IOException` when the code is `ACCESS_DENIED` or `ERROR_SHARING_VIOLATION`. The `<Copy/>` task also has public `Retries` and `RetryDelayMilliseconds` properties to control behavior. Duplicate that kind of logic into our `Files.Copy*IfChanged()` helper methods. This should give our builds a bit more resiliency to these kinds of issues. Instead of adding new `Files.Copy*IfChanged()` method overloads which accept "retries" and "retryDelay" parameters, we instead use environment variables to allow overriding these values: * `DOTNET_ANDROID_FILE_WRITE_RETRY_ATTEMPTS`: The number of times to try to retry a copy operation; corresponds to the `Copy.Retries` MSBuild task property. The default value is 10. * `DOTNET_ANDROID_FILE_WRITE_RETRY_DELAY_MS`: The amount of time, in milliseconds, to delay between attempted copies; corresponds to the `Copy.RetryDelayMilliseconds` MSBuild task property. The default value is 1000 ms. [0]: https://github.com/dotnet/msbuild/blob/main/src/Tasks/Copy.cs#L897
- Loading branch information