diff --git a/src/Cli/dotnet/commands/dotnet-workload/WorkloadUtilities.cs b/src/Cli/dotnet/commands/dotnet-workload/WorkloadUtilities.cs new file mode 100644 index 000000000000..b621d0939767 --- /dev/null +++ b/src/Cli/dotnet/commands/dotnet-workload/WorkloadUtilities.cs @@ -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); + } + } +} diff --git a/src/Cli/dotnet/commands/dotnet-workload/install/WorkloadGarbageCollector.cs b/src/Cli/dotnet/commands/dotnet-workload/install/WorkloadGarbageCollector.cs index d9d72f80154f..0b74c06101d6 100644 --- a/src/Cli/dotnet/commands/dotnet-workload/install/WorkloadGarbageCollector.cs +++ b/src/Cli/dotnet/commands/dotnet-workload/install/WorkloadGarbageCollector.cs @@ -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}"); } diff --git a/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/SdkDirectoryWorkloadManifestProvider.cs b/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/SdkDirectoryWorkloadManifestProvider.cs index 94b917c537e8..c0e0da0e9466 100644 --- a/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/SdkDirectoryWorkloadManifestProvider.cs +++ b/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/SdkDirectoryWorkloadManifestProvider.cs @@ -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()