From b69735ce7e582eeea71fde8792e15a796ca1bbac Mon Sep 17 00:00:00 2001 From: Kieron Lanning Date: Fri, 4 Sep 2026 15:39:08 +0100 Subject: [PATCH] build: fixing retry --- package.json | 2 +- src/Purview.Build/Modules/LintModule.cs | 65 +++++++++++++++++++------ 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index b196cec..5734b13 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "name": "purview-build", - "version": "0.2.2", + "version": "0.2.3", "private": true } \ No newline at end of file diff --git a/src/Purview.Build/Modules/LintModule.cs b/src/Purview.Build/Modules/LintModule.cs index 139210a..b8d4054 100644 --- a/src/Purview.Build/Modules/LintModule.cs +++ b/src/Purview.Build/Modules/LintModule.cs @@ -36,21 +36,7 @@ Task Restore() => cancellationToken ); - var restoreResult = await Restore(); - for (var attempt = 1; restoreResult.ExitCode != 0 && attempt < maxAttempts; attempt++) - { - context.Logger.LogWarning( - "dotnet tool restore failed (attempt {Attempt} of {MaxAttempts}). Retrying...", - attempt, - maxAttempts - ); - - await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken); - - restoreResult = await Restore(); - } - if (restoreResult.ExitCode != 0) - return restoreResult; + var restoreResult = await RestoreWithRetryAsync(Restore, maxAttempts, context, cancellationToken); // Restore worked, now run the linter return await context.Shell.Command.ExecuteCommandLineTool( @@ -59,4 +45,53 @@ Task Restore() => cancellationToken: cancellationToken ); } + + static async Task RestoreWithRetryAsync( + Func> restore, + int maxAttempts, + IModuleContext context, + CancellationToken cancellationToken + ) + { + Exception? lastException = null; + CommandResult? lastResult = null; + + for (var attempt = 1; attempt <= maxAttempts; attempt++) + { + try + { + var result = await restore(); + if (result.ExitCode == 0) + return result; + + lastException = null; + lastResult = result; + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + lastException = ex; + lastResult = null; + } + + if (attempt < maxAttempts) + { + context.Logger.LogWarning( + lastException, + "dotnet tool restore failed (attempt {Attempt} of {MaxAttempts}). Retrying...", + attempt, + maxAttempts + ); + + await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken); + } + } + + if (lastException is not null) + throw lastException; + + if (lastResult is not null) + throw new InvalidOperationException($"dotnet tool restore failed with exit code {lastResult.ExitCode}."); + + throw new InvalidOperationException("dotnet tool restore failed."); + } } \ No newline at end of file