|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 |
| -#nullable disable |
5 |
| - |
6 | 4 | using Microsoft.Build.Framework;
|
7 | 5 | using Microsoft.NET.HostModel.Bundle;
|
8 | 6 |
|
9 | 7 | namespace Microsoft.NET.Build.Tasks
|
10 | 8 | {
|
11 |
| - public class GenerateBundle : TaskBase |
| 9 | + public class GenerateBundle : TaskBase, ICancelableTask |
12 | 10 | {
|
| 11 | + private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); |
| 12 | + private readonly Random _jitter = |
| 13 | +#if NET |
| 14 | + Random.Shared; |
| 15 | +#else |
| 16 | + new Random(); |
| 17 | +#endif |
| 18 | + |
13 | 19 | [Required]
|
14 |
| - public ITaskItem[] FilesToBundle { get; set; } |
| 20 | + public ITaskItem[] FilesToBundle { get; set; } = null!; |
15 | 21 | [Required]
|
16 |
| - public string AppHostName { get; set; } |
| 22 | + public string AppHostName { get; set; } = null!; |
17 | 23 | [Required]
|
18 | 24 | public bool IncludeSymbols { get; set; }
|
19 | 25 | [Required]
|
20 | 26 | public bool IncludeNativeLibraries { get; set; }
|
21 | 27 | [Required]
|
22 | 28 | public bool IncludeAllContent { get; set; }
|
23 | 29 | [Required]
|
24 |
| - public string TargetFrameworkVersion { get; set; } |
| 30 | + public string TargetFrameworkVersion { get; set; } = null!; |
25 | 31 | [Required]
|
26 |
| - public string RuntimeIdentifier { get; set; } |
| 32 | + public string RuntimeIdentifier { get; set; } = null!; |
27 | 33 | [Required]
|
28 |
| - public string OutputDir { get; set; } |
| 34 | + public string OutputDir { get; set; } = null!; |
29 | 35 | [Required]
|
30 | 36 | public bool ShowDiagnosticOutput { get; set; }
|
31 | 37 | [Required]
|
32 | 38 | public bool EnableCompressionInSingleFile { get; set; }
|
33 | 39 | public bool EnableMacOsCodeSign { get; set; } = true;
|
34 | 40 |
|
35 | 41 | [Output]
|
36 |
| - public ITaskItem[] ExcludedFiles { get; set; } |
| 42 | + public ITaskItem[] ExcludedFiles { get; set; } = null!; |
| 43 | + |
| 44 | + public int? RetryCount { get; set; } = 3; |
| 45 | + |
| 46 | + public void Cancel() => _cancellationTokenSource.Cancel(); |
37 | 47 |
|
38 | 48 | protected override void ExecuteCore()
|
| 49 | + { |
| 50 | + ExecuteWithRetry().GetAwaiter().GetResult(); |
| 51 | + } |
| 52 | + |
| 53 | + private async Task ExecuteWithRetry() |
39 | 54 | {
|
40 | 55 | OSPlatform targetOS = RuntimeIdentifier.StartsWith("win") ? OSPlatform.Windows :
|
41 | 56 | RuntimeIdentifier.StartsWith("osx") ? OSPlatform.OSX :
|
@@ -78,15 +93,40 @@ protected override void ExecuteCore()
|
78 | 93 | bundleRelativePath: item.GetMetadata(MetadataKeys.RelativePath)));
|
79 | 94 | }
|
80 | 95 |
|
81 |
| - bundler.GenerateBundle(fileSpec); |
| 96 | + // GenerateBundle has been throwing IOException intermittently in CI runs when accessing the singlefilehost binary specifically. |
| 97 | + // We hope that it's a Defender issue and that a quick retry will paper over the intermittent delay. |
| 98 | + await DoWithRetry(() => bundler.GenerateBundle(fileSpec)); |
82 | 99 |
|
83 | 100 | // Certain files are excluded from the bundle, based on BundleOptions.
|
84 | 101 | // For example:
|
85 | 102 | // Native files and contents files are excluded by default.
|
86 | 103 | // hostfxr and hostpolicy are excluded until singlefilehost is available.
|
87 | 104 | // Return the set of excluded files in ExcludedFiles, so that they can be placed in the publish directory.
|
88 | 105 |
|
89 |
| - ExcludedFiles = FilesToBundle.Zip(fileSpec, (item, spec) => (spec.Excluded) ? item : null).Where(x => x != null).ToArray(); |
| 106 | + ExcludedFiles = FilesToBundle.Zip(fileSpec, (item, spec) => (spec.Excluded) ? item : null).Where(x => x != null).ToArray()!; |
| 107 | + } |
| 108 | + |
| 109 | + public async Task DoWithRetry(Action action) |
| 110 | + { |
| 111 | + bool triedOnce = false; |
| 112 | + while (RetryCount > 0 || !triedOnce) |
| 113 | + { |
| 114 | + if (_cancellationTokenSource.IsCancellationRequested) |
| 115 | + { |
| 116 | + break; |
| 117 | + } |
| 118 | + try |
| 119 | + { |
| 120 | + action(); |
| 121 | + break; |
| 122 | + } |
| 123 | + catch (IOException) when (RetryCount > 0) |
| 124 | + { |
| 125 | + Log.LogMessage(MessageImportance.High, $"Unable to access file during bundling. Retrying {RetryCount} more times..."); |
| 126 | + RetryCount--; |
| 127 | + await Task.Delay(_jitter.Next(10, 50)); |
| 128 | + } |
| 129 | + } |
90 | 130 | }
|
91 | 131 | }
|
92 | 132 | }
|
0 commit comments