Skip to content

Commit 1ff4c0d

Browse files
committed
Restore and use Microsoft.CodeAnalysis.ResxSourceGenerator
1 parent 79fe5f8 commit 1ff4c0d

File tree

11 files changed

+295
-64
lines changed

11 files changed

+295
-64
lines changed

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

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void exitCallback(int ret, string msg, bool silent)
153153
{
154154
new ImplicitUsingsGenerator(fileContent, logger, tempWorkingDirectory),
155155
new RazorGenerator(fileProvider, fileContent, dotnet, this, logger, tempWorkingDirectory, usedReferences.Keys),
156-
new ResxGenerator(fileProvider, fileContent, dotnet, this, logger, tempWorkingDirectory, usedReferences.Keys),
156+
new ResxGenerator(fileProvider, fileContent, dotnet, this, logger, nugetPackageRestorer, tempWorkingDirectory, usedReferences.Keys),
157157
};
158158

159159
foreach (var sourceGenerator in sourceGenerators)
@@ -256,35 +256,6 @@ private void RemoveNugetAnalyzerReferences()
256256
}
257257
}
258258

259-
private void SelectNewestFrameworkPath(string frameworkPath, string frameworkType, ISet<AssemblyLookupLocation> dllLocations, ISet<string> frameworkLocations)
260-
{
261-
var versionFolders = GetPackageVersionSubDirectories(frameworkPath);
262-
if (versionFolders.Length > 1)
263-
{
264-
var versions = string.Join(", ", versionFolders.Select(d => d.Name));
265-
logger.LogDebug($"Found multiple {frameworkType} DLLs in NuGet packages at {frameworkPath}. Using the latest version ({versionFolders[0].Name}) from: {versions}.");
266-
}
267-
268-
var selectedFrameworkFolder = versionFolders.FirstOrDefault()?.FullName;
269-
if (selectedFrameworkFolder is null)
270-
{
271-
logger.LogDebug($"Found {frameworkType} DLLs in NuGet packages at {frameworkPath}, but no version folder was found.");
272-
selectedFrameworkFolder = frameworkPath;
273-
}
274-
275-
dllLocations.Add(selectedFrameworkFolder);
276-
frameworkLocations.Add(selectedFrameworkFolder);
277-
logger.LogDebug($"Found {frameworkType} DLLs in NuGet packages at {selectedFrameworkFolder}.");
278-
}
279-
280-
private static DirectoryInfo[] GetPackageVersionSubDirectories(string packagePath)
281-
{
282-
return new DirectoryInfo(packagePath)
283-
.EnumerateDirectories("*", new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive, RecurseSubdirectories = false })
284-
.OrderByDescending(d => d.Name) // TODO: Improve sorting to handle pre-release versions.
285-
.ToArray();
286-
}
287-
288259
private void RemoveFrameworkNugetPackages(ISet<AssemblyLookupLocation> dllLocations, int fromIndex = 0)
289260
{
290261
var packagesInPrioOrder = FrameworkPackageNames.NetFrameworks;
@@ -311,10 +282,12 @@ private void AddNetFrameworkDlls(ISet<AssemblyLookupLocation> dllLocations, ISet
311282
{
312283
foreach (var fp in frameworkPaths)
313284
{
314-
dotnetFrameworkVersionVariantCount += GetPackageVersionSubDirectories(fp.Path!).Length;
285+
dotnetFrameworkVersionVariantCount += NugetPackageRestorer.GetOrderedPackageVersionSubDirectories(fp.Path!).Length;
315286
}
316287

317-
SelectNewestFrameworkPath(frameworkPath.Path, ".NET Framework", dllLocations, frameworkLocations);
288+
var folder = nugetPackageRestorer.GetNewestNugetPackageVersionFolder(frameworkPath.Path, ".NET Framework");
289+
dllLocations.Add(folder);
290+
frameworkLocations.Add(folder);
318291
RemoveFrameworkNugetPackages(dllLocations, frameworkPath.Index + 1);
319292
return;
320293
}
@@ -332,7 +305,7 @@ private void AddNetFrameworkDlls(ISet<AssemblyLookupLocation> dllLocations, ISet
332305
if (runtimeLocation is null)
333306
{
334307
logger.LogInfo("No .NET Desktop Runtime location found. Attempting to restore the .NET Framework reference assemblies manually.");
335-
runtimeLocation = nugetPackageRestorer.TryRestoreLatestNetFrameworkReferenceAssemblies();
308+
runtimeLocation = nugetPackageRestorer.TryRestore(FrameworkPackageNames.LatestNetFrameworkReferenceAssemblies);
336309
}
337310
}
338311

@@ -372,7 +345,9 @@ private void AddAspNetCoreFrameworkDlls(ISet<AssemblyLookupLocation> dllLocation
372345
// First try to find ASP.NET Core assemblies in the NuGet packages
373346
if (GetPackageDirectory(FrameworkPackageNames.AspNetCoreFramework) is string aspNetCorePackage)
374347
{
375-
SelectNewestFrameworkPath(aspNetCorePackage, "ASP.NET Core", dllLocations, frameworkLocations);
348+
var folder = nugetPackageRestorer.GetNewestNugetPackageVersionFolder(aspNetCorePackage, "ASP.NET Core");
349+
dllLocations.Add(folder);
350+
frameworkLocations.Add(folder);
376351
return;
377352
}
378353

@@ -388,7 +363,9 @@ private void AddMicrosoftWindowsDesktopDlls(ISet<AssemblyLookupLocation> dllLoca
388363
{
389364
if (GetPackageDirectory(FrameworkPackageNames.WindowsDesktopFramework) is string windowsDesktopApp)
390365
{
391-
SelectNewestFrameworkPath(windowsDesktopApp, "Windows Desktop App", dllLocations, frameworkLocations);
366+
var folder = nugetPackageRestorer.GetNewestNugetPackageVersionFolder(windowsDesktopApp, "Windows Desktop App");
367+
dllLocations.Add(folder);
368+
frameworkLocations.Add(folder);
392369
}
393370
}
394371

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public partial record class RestoreResult(bool Success, IList<string> Output)
3030
private readonly Lazy<bool> hasNugetPackageSourceError = new(() => Output.Any(s => s.Contains("NU1301")));
3131
public bool HasNugetPackageSourceError => hasNugetPackageSourceError.Value;
3232

33+
private readonly Lazy<bool> hasNugetNoStablePackageVersionError = new(() => Output.Any(s => s.Contains("NU1103")));
34+
public bool HasNugetNoStablePackageVersionError => hasNugetNoStablePackageVersionError.Value;
35+
3336
private static IEnumerable<string> GetFirstGroupOnMatch(Regex regex, IEnumerable<string> lines) =>
3437
lines
3538
.Select(line => regex.Match(line))

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

Lines changed: 96 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,48 @@ public NugetPackageRestorer(
4848
missingPackageDirectory = new TemporaryDirectory(ComputeTempDirectoryPath(fileProvider.SourceDir.FullName, "missingpackages"), "missing package", logger);
4949
}
5050

51-
public string? TryRestoreLatestNetFrameworkReferenceAssemblies()
51+
public string? TryRestore(string package)
5252
{
53-
if (TryRestorePackageManually(FrameworkPackageNames.LatestNetFrameworkReferenceAssemblies))
53+
if (TryRestorePackageManually(package))
5454
{
55-
return DependencyManager.GetPackageDirectory(FrameworkPackageNames.LatestNetFrameworkReferenceAssemblies, missingPackageDirectory.DirInfo);
55+
var packageDir = DependencyManager.GetPackageDirectory(package, missingPackageDirectory.DirInfo);
56+
if (packageDir is not null)
57+
{
58+
return GetNewestNugetPackageVersionFolder(packageDir, package);
59+
}
5660
}
5761

5862
return null;
5963
}
6064

65+
public string GetNewestNugetPackageVersionFolder(string packagePath, string packageFriendlyName)
66+
{
67+
var versionFolders = GetOrderedPackageVersionSubDirectories(packagePath);
68+
if (versionFolders.Length > 1)
69+
{
70+
var versions = string.Join(", ", versionFolders.Select(d => d.Name));
71+
logger.LogDebug($"Found multiple {packageFriendlyName} DLLs in NuGet packages at {packagePath}. Using the latest version ({versionFolders[0].Name}) from: {versions}.");
72+
}
73+
74+
var selectedFrameworkFolder = versionFolders.FirstOrDefault()?.FullName;
75+
if (selectedFrameworkFolder is null)
76+
{
77+
logger.LogDebug($"Found {packageFriendlyName} DLLs in NuGet packages at {packagePath}, but no version folder was found.");
78+
selectedFrameworkFolder = packagePath;
79+
}
80+
81+
logger.LogDebug($"Found {packageFriendlyName} DLLs in NuGet packages at {selectedFrameworkFolder}.");
82+
return selectedFrameworkFolder;
83+
}
84+
85+
public static DirectoryInfo[] GetOrderedPackageVersionSubDirectories(string packagePath)
86+
{
87+
return new DirectoryInfo(packagePath)
88+
.EnumerateDirectories("*", new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive, RecurseSubdirectories = false })
89+
.OrderByDescending(d => d.Name) // TODO: Improve sorting to handle pre-release versions.
90+
.ToArray();
91+
}
92+
6193
public HashSet<AssemblyLookupLocation> Restore()
6294
{
6395
var assemblyLookupLocations = new HashSet<AssemblyLookupLocation>();
@@ -408,7 +440,8 @@ private static IEnumerable<string> GetRestoredPackageDirectoryNames(DirectoryInf
408440
.Select(d => Path.GetFileName(d).ToLowerInvariant());
409441
}
410442

411-
private bool TryRestorePackageManually(string package, string? nugetConfig = null, PackageReferenceSource packageReferenceSource = PackageReferenceSource.SdkCsProj, bool tryWithoutNugetConfig = true)
443+
private bool TryRestorePackageManually(string package, string? nugetConfig = null, PackageReferenceSource packageReferenceSource = PackageReferenceSource.SdkCsProj,
444+
bool tryWithoutNugetConfig = true, bool tryPrereleaseVersion = true)
412445
{
413446
logger.LogInfo($"Restoring package {package}...");
414447
using var tempDir = new TemporaryDirectory(
@@ -430,59 +463,91 @@ private bool TryRestorePackageManually(string package, string? nugetConfig = nul
430463
return false;
431464
}
432465

433-
var res = dotnet.Restore(new(tempDir.DirInfo.FullName, missingPackageDirectory.DirInfo.FullName, ForceDotnetRefAssemblyFetching: false, PathToNugetConfig: nugetConfig));
434-
if (!res.Success)
466+
var res = TryRestorePackageManually(package, nugetConfig, tempDir, tryPrereleaseVersion);
467+
if (res.Success)
435468
{
436-
if (tryWithoutNugetConfig && res.HasNugetPackageSourceError && nugetConfig is not null)
469+
return true;
470+
}
471+
472+
if (tryWithoutNugetConfig && res.HasNugetPackageSourceError && nugetConfig is not null)
473+
{
474+
logger.LogDebug($"Trying to restore '{package}' without nuget.config.");
475+
// Restore could not be completed because the listed source is unavailable. Try without the nuget.config:
476+
res = TryRestorePackageManually(package, nugetConfig: null, tempDir, tryPrereleaseVersion);
477+
if (res.Success)
437478
{
438-
// Restore could not be completed because the listed source is unavailable. Try without the nuget.config:
439-
res = dotnet.Restore(new(tempDir.DirInfo.FullName, missingPackageDirectory.DirInfo.FullName, ForceDotnetRefAssemblyFetching: false, PathToNugetConfig: null, ForceReevaluation: true));
479+
return true;
440480
}
481+
}
482+
483+
logger.LogInfo($"Failed to restore nuget package {package}");
484+
return false;
485+
}
441486

442-
// TODO: the restore might fail, we could retry with
443-
// - a prerelease (*-* instead of *) version of the package,
444-
// - a different target framework moniker.
487+
private RestoreResult TryRestorePackageManually(string package, string? nugetConfig, TemporaryDirectory tempDir, bool tryPrereleaseVersion)
488+
{
489+
var res = dotnet.Restore(new(tempDir.DirInfo.FullName, missingPackageDirectory.DirInfo.FullName, ForceDotnetRefAssemblyFetching: false, PathToNugetConfig: nugetConfig, ForceReevaluation: true));
445490

446-
if (!res.Success)
491+
if (!res.Success && tryPrereleaseVersion && res.HasNugetNoStablePackageVersionError)
492+
{
493+
logger.LogDebug($"Failed to restore nuget package {package} because no stable version was found.");
494+
try
447495
{
448-
logger.LogInfo($"Failed to restore nuget package {package}");
449-
return false;
496+
TryChangePackageVersion(tempDir.DirInfo, "*-*");
497+
498+
res = dotnet.Restore(new(tempDir.DirInfo.FullName, missingPackageDirectory.DirInfo.FullName, ForceDotnetRefAssemblyFetching: false, PathToNugetConfig: nugetConfig, ForceReevaluation: true));
499+
return res;
500+
}
501+
finally
502+
{
503+
TryChangePackageVersion(tempDir.DirInfo, "*");
450504
}
451505
}
452506

453-
return true;
507+
return res;
454508
}
455509

456510
private void TryChangeTargetFrameworkMoniker(DirectoryInfo tempDir)
511+
{
512+
TryChangeProjectFile(tempDir, TargetFramework(), $"<TargetFramework>{FrameworkPackageNames.LatestNetFrameworkMoniker}</TargetFramework>", "target framework moniker");
513+
}
514+
515+
private void TryChangePackageVersion(DirectoryInfo tempDir, string newVersion)
516+
{
517+
TryChangeProjectFile(tempDir, PackageReferenceVersion(), $"Version=\"{newVersion}\"", "package reference version");
518+
}
519+
520+
private bool TryChangeProjectFile(DirectoryInfo projectDir, Regex pattern, string replacement, string patternName)
457521
{
458522
try
459523
{
460-
logger.LogInfo($"Changing the target framework moniker in {tempDir.FullName}...");
524+
logger.LogDebug($"Changing the {patternName} in {projectDir.FullName}...");
461525

462-
var csprojs = tempDir.GetFiles("*.csproj", new EnumerationOptions { RecurseSubdirectories = false, MatchCasing = MatchCasing.CaseInsensitive });
526+
var csprojs = projectDir.GetFiles("*.csproj", new EnumerationOptions { RecurseSubdirectories = false, MatchCasing = MatchCasing.CaseInsensitive });
463527
if (csprojs.Length != 1)
464528
{
465-
logger.LogError($"Could not find the .csproj file in {tempDir.FullName}, count = {csprojs.Length}");
466-
return;
529+
logger.LogError($"Could not find the .csproj file in {projectDir.FullName}, count = {csprojs.Length}");
530+
return false;
467531
}
468532

469533
var csproj = csprojs[0];
470534
var content = File.ReadAllText(csproj.FullName);
471-
var matches = TargetFramework().Matches(content);
535+
var matches = pattern.Matches(content);
472536
if (matches.Count == 0)
473537
{
474-
logger.LogError($"Could not find target framework in {csproj.FullName}");
475-
}
476-
else
477-
{
478-
content = TargetFramework().Replace(content, $"<TargetFramework>{FrameworkPackageNames.LatestNetFrameworkMoniker}</TargetFramework>", 1);
479-
File.WriteAllText(csproj.FullName, content);
538+
logger.LogError($"Could not find the {patternName} in {csproj.FullName}");
539+
return false;
480540
}
541+
542+
content = pattern.Replace(content, replacement, 1);
543+
File.WriteAllText(csproj.FullName, content);
544+
return true;
481545
}
482546
catch (Exception exc)
483547
{
484-
logger.LogError($"Failed to update target framework in {tempDir.FullName}: {exc}");
548+
logger.LogError($"Failed to change the {patternName} in {projectDir.FullName}: {exc}");
485549
}
550+
return false;
486551
}
487552

488553
private static async Task ExecuteGetRequest(string address, HttpClient httpClient, CancellationToken cancellationToken)
@@ -664,6 +729,9 @@ private IEnumerable<string> GetFeeds(Func<IList<string>> getNugetFeeds)
664729
[GeneratedRegex(@"<TargetFramework>.*</TargetFramework>", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]
665730
private static partial Regex TargetFramework();
666731

732+
[GeneratedRegex(@"Version=""[*|*-*]""", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]
733+
private static partial Regex PackageReferenceVersion();
734+
667735
[GeneratedRegex(@"^(.+)\.(\d+\.\d+\.\d+(-(.+))?)$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]
668736
private static partial Regex LegacyNugetPackage();
669737

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/ResxGenerator.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ public ResxGenerator(
1515
IDotNet dotnet,
1616
ICompilationInfoContainer compilationInfoContainer,
1717
ILogger logger,
18+
NugetPackageRestorer nugetPackageRestorer,
1819
TemporaryDirectory tempWorkingDirectory,
1920
IEnumerable<string> references) : base(fileProvider, fileContent, dotnet, compilationInfoContainer, logger, tempWorkingDirectory, references)
2021
{
2122
try
2223
{
23-
// todo: download latest `Microsoft.CodeAnalysis.ResxSourceGenerator` and set `sourceGeneratorFolder`
24+
// The package is downloaded to `missingpackages`, which is okay, we're already after the DLL collection phase.
25+
var nugetFolder = nugetPackageRestorer.TryRestore("Microsoft.CodeAnalysis.ResxSourceGenerator");
26+
if (nugetFolder is not null)
27+
{
28+
sourceGeneratorFolder = System.IO.Path.Combine(nugetFolder, "analyzers", "dotnet", "cs");
29+
}
2430
}
2531
catch (Exception e)
2632
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| Program.cs |
2+
| test-db/working/implicitUsings/GlobalUsings.g.cs |
3+
| test-db/working/resx/[...]/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/test.Designer.cs |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import csharp
2+
3+
private string getPath(File f) {
4+
result = f.getRelativePath() and
5+
not exists(
6+
result
7+
.indexOf("Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator")
8+
)
9+
or
10+
exists(int index |
11+
index =
12+
f.getRelativePath()
13+
.indexOf("Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator") and
14+
result =
15+
f.getRelativePath().substring(0, index - 32 - 2) + "/[...]/" +
16+
f.getRelativePath().substring(index, f.getRelativePath().length())
17+
)
18+
}
19+
20+
from File f
21+
where f.fromSource()
22+
select getPath(f)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var dummy = "dummy";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "8.0.101"
4+
}
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<Target Name="DeleteBinObjFolders" BeforeTargets="Clean">
11+
<RemoveDir Directories=".\bin" />
12+
<RemoveDir Directories=".\obj" />
13+
</Target>
14+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import os
2+
from create_database_utils import *
3+
4+
run_codeql_database_create(lang="csharp", extra_args=["--build-mode=none"])

0 commit comments

Comments
 (0)