Skip to content

Commit

Permalink
Avoid workload gc failure due to 4-part version in ReleaseVersion use…
Browse files Browse the repository at this point in the history
…d to find a max (#45263)
  • Loading branch information
marcpopMSFT authored Dec 10, 2024
2 parents 25ad186 + 9bdf99c commit 5b7ffbc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
37 changes: 37 additions & 0 deletions src/Cli/dotnet/commands/dotnet-workload/WorkloadUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Deployment.DotNet.Releases;

namespace Microsoft.DotNet.Workloads.Workload
{
internal class WorkloadUtilities
{
internal static int VersionCompare(string first, string second)
{
if (first.Equals(second))
{
return 0;
}

var firstDash = first.IndexOf('-');
var secondDash = second.IndexOf('-');
firstDash = firstDash < 0 ? first.Length : firstDash;
secondDash = secondDash < 0 ? second.Length : secondDash;

var firstVersion = new Version(first.Substring(0, firstDash));
var secondVersion = new Version(second.Substring(0, secondDash));

var comparison = firstVersion.CompareTo(secondVersion);
if (comparison != 0)
{
return comparison;
}

var modifiedFirst = new ReleaseVersion(1, 1, 1, firstDash == first.Length ? null : first.Substring(firstDash));
var modifiedSecond = new ReleaseVersion(1, 1, 1, secondDash == second.Length ? null : second.Substring(secondDash));

return modifiedFirst.CompareTo(modifiedSecond);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void GarbageCollectWorkloadSets()
// If there isn't a rollback state file, don't garbage collect the latest workload set installed for the feature band
if (installedWorkloadSets.Any())
{
var latestWorkloadSetVersion = installedWorkloadSets.Keys.MaxBy(k => new ReleaseVersion(k));
var latestWorkloadSetVersion = installedWorkloadSets.Keys.Aggregate((s1, s2) => WorkloadUtilities.VersionCompare(s1, s2) >= 0 ? s1 : s2);
_workloadSets[latestWorkloadSetVersion] = GCAction.Keep;
_verboseReporter.WriteLine($"GC: Keeping latest installed workload set version {latestWorkloadSetVersion}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ private static int VersionCompare(string first, string second)
return comparison;
}

var modifiedFirst = "1.1.1" + (firstDash == first.Length ? string.Empty : first.Substring(firstDash));
var modifiedSecond = "1.1.1" + (secondDash == second.Length ? string.Empty : second.Substring(secondDash));
var modifiedFirst = new ReleaseVersion(1, 1, 1, firstDash == first.Length ? null : first.Substring(firstDash));
var modifiedSecond = new ReleaseVersion(1, 1, 1, secondDash == second.Length ? null : second.Substring(secondDash));

return new ReleaseVersion(modifiedFirst).CompareTo(new ReleaseVersion(modifiedSecond));
return modifiedFirst.CompareTo(modifiedSecond);
}

void ThrowExceptionIfManifestsNotAvailable()
Expand Down

0 comments on commit 5b7ffbc

Please sign in to comment.