Skip to content

Commit 21f101f

Browse files
committed
C#: Use NuGet version sorting instead of lexicographic directory name sorting for finding newest package version.
1 parent e380755 commit 21f101f

File tree

6 files changed

+10
-1
lines changed

6 files changed

+10
-1
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ codeql_csharp_library(
1616
"//csharp/extractor/Semmle.Extraction.CSharp",
1717
"//csharp/extractor/Semmle.Util",
1818
"@paket.main//newtonsoft.json",
19+
"@paket.main//nuget.versioning",
1920
],
2021
)

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Text.RegularExpressions;
1111
using System.Threading;
1212
using System.Threading.Tasks;
13+
using NuGet.Versioning;
1314
using Semmle.Util;
1415
using Semmle.Util.Logging;
1516

@@ -89,9 +90,12 @@ public string GetNewestNugetPackageVersionFolder(string packagePath, string pack
8990

9091
public static DirectoryInfo[] GetOrderedPackageVersionSubDirectories(string packagePath)
9192
{
93+
// Only consider directories with valid NuGet version names.
9294
return new DirectoryInfo(packagePath)
9395
.EnumerateDirectories("*", new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive, RecurseSubdirectories = false })
94-
.OrderByDescending(d => d.Name) // TODO: Improve sorting to handle pre-release versions.
96+
.SelectMany(d => NuGetVersion.TryParse(d.Name, out var version) ? new[] { new { Directory = d, NuGetVersion = version } } : [])
97+
.OrderByDescending(dw => dw.NuGetVersion)
98+
.Select(dw => dw.Directory)
9599
.ToArray();
96100
}
97101

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Newtonsoft.Json
2+
NuGet.Versioning

csharp/paket.dependencies

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ strategy: max
77
nuget Basic.CompilerLog.Util 0.9.21
88
nuget Mono.Posix.NETStandard
99
nuget Newtonsoft.Json
10+
nuget NuGet.Versioning
1011
nuget xunit
1112
nuget xunit.runner.visualstudio
1213
nuget xunit.runner.utility

csharp/paket.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

csharp/paket.main.bzl

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)