From c004f9236599a4592a224eb4dfc698c70d3a96b2 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Thu, 11 Apr 2024 14:40:30 +0200 Subject: [PATCH] Apply code review findings --- .../DependencyManager.Nuget.cs | 32 ++++++------------- .../DotNet.cs | 12 +++++-- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.Nuget.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.Nuget.cs index 867753bbbdf..298f462563b 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.Nuget.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.Nuget.cs @@ -96,6 +96,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching if (fallbackFeeds.Count == 0) { fallbackFeeds.Add(PublicNugetFeed); + logger.LogInfo($"No fallback Nuget feeds specified. Using default feed: {PublicNugetFeed}"); } logger.LogInfo($"Checking fallback Nuget feed reachability on feeds: {string.Join(", ", fallbackFeeds.OrderBy(f => f))}"); @@ -188,7 +189,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching var reachableFallbackFeeds = GetReachableFallbackNugetFeeds(); if (reachableFallbackFeeds.Count > 0) { - DownloadMissingPackages(allNonBinaryFiles, dllLocations, withNugetConfigFromSrc: false, fallbackNugetFeeds: reachableFallbackFeeds); + DownloadMissingPackages(allNonBinaryFiles, dllLocations, fallbackNugetFeeds: reachableFallbackFeeds); } else { @@ -196,7 +197,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching } } - private void DownloadMissingPackages(List allFiles, HashSet dllLocations, bool withNugetConfigFromSrc = true, IEnumerable? fallbackNugetFeeds = null) + private void DownloadMissingPackages(List allFiles, HashSet dllLocations, IEnumerable? fallbackNugetFeeds = null) { var alreadyDownloadedPackages = GetRestoredPackageDirectoryNames(packageDirectory.DirInfo); var alreadyDownloadedLegacyPackages = GetRestoredLegacyPackageNames(); @@ -230,7 +231,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching logger.LogInfo($"Found {notYetDownloadedPackages.Count} packages that are not yet restored"); using var tempDir = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName, "nugetconfig")); - var nugetConfig = withNugetConfigFromSrc + var nugetConfig = fallbackNugetFeeds is null ? GetNugetConfig(allFiles) : CreateFallbackNugetConfig(fallbackNugetFeeds, tempDir.DirInfo.FullName); @@ -241,7 +242,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching Parallel.ForEach(notYetDownloadedPackages, new ParallelOptions { MaxDegreeOfParallelism = threads }, package => { - var success = TryRestorePackageManually(package.Name, nugetConfig, package.PackageReferenceSource, tryWithoutNugetConfig: withNugetConfigFromSrc); + var success = TryRestorePackageManually(package.Name, nugetConfig, package.PackageReferenceSource, tryWithoutNugetConfig: fallbackNugetFeeds is null); if (!success) { return; @@ -258,15 +259,8 @@ namespace Semmle.Extraction.CSharp.DependencyFetching dllLocations.Add(missingPackageDirectory.DirInfo.FullName); } - private string? CreateFallbackNugetConfig(IEnumerable? fallbackNugetFeeds, string folderPath) + private string? CreateFallbackNugetConfig(IEnumerable fallbackNugetFeeds, string folderPath) { - if (fallbackNugetFeeds is null) - { - // We're not overriding the inherited Nuget feeds - logger.LogInfo("No fallback Nuget feeds provided. Not creating a fallback nuget.config file."); - return null; - } - var sb = new StringBuilder(); fallbackNugetFeeds.ForEach((feed, index) => sb.AppendLine($"")); @@ -587,19 +581,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching } } - private (HashSet, HashSet) GetAllFeeds(List allFiles) + private (HashSet explicitFeeds, HashSet allFeeds) GetAllFeeds(List allFiles) { - IList GetNugetFeeds(string nugetConfig) - { - logger.LogInfo($"Getting Nuget feeds from '{nugetConfig}'..."); - return dotnet.GetNugetFeeds(nugetConfig); - } + IList GetNugetFeeds(string nugetConfig) => dotnet.GetNugetFeeds(nugetConfig); - IList GetNugetFeedsFromFolder(string folderPath) - { - logger.LogInfo($"Getting Nuget feeds in folder '{folderPath}'..."); - return dotnet.GetNugetFeedsFromFolder(folderPath); - } + IList GetNugetFeedsFromFolder(string folderPath) => dotnet.GetNugetFeedsFromFolder(folderPath); var nugetConfigs = GetAllNugetConfigs(allFiles); var explicitFeeds = nugetConfigs diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs index bda99f8541b..796c60f9f31 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs @@ -113,9 +113,17 @@ namespace Semmle.Extraction.CSharp.DependencyFetching private const string nugetListSourceCommand = "nuget list source --format Short"; - public IList GetNugetFeeds(string nugetConfig) => GetResultList($"{nugetListSourceCommand} --configfile \"{nugetConfig}\""); + public IList GetNugetFeeds(string nugetConfig) + { + logger.LogInfo($"Getting Nuget feeds from '{nugetConfig}'..."); + return GetResultList($"{nugetListSourceCommand} --configfile \"{nugetConfig}\""); + } - public IList GetNugetFeedsFromFolder(string folderPath) => GetResultList(nugetListSourceCommand, folderPath); + public IList GetNugetFeedsFromFolder(string folderPath) + { + logger.LogInfo($"Getting Nuget feeds in folder '{folderPath}'..."); + return GetResultList(nugetListSourceCommand, folderPath); + } // The version number should be kept in sync with the version .NET version used for building the application. public const string LatestDotNetSdkVersion = "8.0.101";