Skip to content

Commit b36f977

Browse files
authored
[release/9.0.1xx] Avoid workload gc failure due to 4-part version in ReleaseVersion used to find a max (#46989)
2 parents 198c3ab + ed9379d commit b36f977

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.Deployment.DotNet.Releases;
5+
6+
namespace Microsoft.DotNet.Workloads.Workload
7+
{
8+
internal class WorkloadUtilities
9+
{
10+
internal static int VersionCompare(string first, string second)
11+
{
12+
if (first.Equals(second))
13+
{
14+
return 0;
15+
}
16+
17+
var firstDash = first.IndexOf('-');
18+
var secondDash = second.IndexOf('-');
19+
firstDash = firstDash < 0 ? first.Length : firstDash;
20+
secondDash = secondDash < 0 ? second.Length : secondDash;
21+
22+
var firstVersion = new Version(first.Substring(0, firstDash));
23+
var secondVersion = new Version(second.Substring(0, secondDash));
24+
25+
var comparison = firstVersion.CompareTo(secondVersion);
26+
if (comparison != 0)
27+
{
28+
return comparison;
29+
}
30+
31+
var modifiedFirst = new ReleaseVersion(1, 1, 1, firstDash == first.Length ? null : first.Substring(firstDash));
32+
var modifiedSecond = new ReleaseVersion(1, 1, 1, secondDash == second.Length ? null : second.Substring(secondDash));
33+
34+
return modifiedFirst.CompareTo(modifiedSecond);
35+
}
36+
}
37+
}

src/Cli/dotnet/commands/dotnet-workload/install/WorkloadGarbageCollector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void GarbageCollectWorkloadSets()
115115
// If there isn't a rollback state file, don't garbage collect the latest workload set installed for the feature band
116116
if (installedWorkloadSets.Any())
117117
{
118-
var latestWorkloadSetVersion = installedWorkloadSets.Keys.MaxBy(k => new ReleaseVersion(k));
118+
var latestWorkloadSetVersion = installedWorkloadSets.Keys.Aggregate((s1, s2) => WorkloadUtilities.VersionCompare(s1, s2) >= 0 ? s1 : s2);
119119
_workloadSets[latestWorkloadSetVersion] = GCAction.Keep;
120120
_verboseReporter.WriteLine($"GC: Keeping latest installed workload set version {latestWorkloadSetVersion}");
121121
}

src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/SdkDirectoryWorkloadManifestProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ private static int VersionCompare(string first, string second)
230230
return comparison;
231231
}
232232

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

236-
return new ReleaseVersion(modifiedFirst).CompareTo(new ReleaseVersion(modifiedSecond));
236+
return modifiedFirst.CompareTo(modifiedSecond);
237237
}
238238

239239
void ThrowExceptionIfManifestsNotAvailable()

0 commit comments

Comments
 (0)