diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FeedManager.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FeedManager.cs index 1d5523a983cb..0cbf106df38a 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FeedManager.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FeedManager.cs @@ -60,17 +60,12 @@ internal sealed partial class FeedManager : IDisposable /// public ImmutableHashSet InheritedFeeds => AllFeeds.Except(ExplicitFeeds).ToImmutableHashSet(); - private readonly Lazy<(bool, ImmutableHashSet)> lazyReachableExplicitFeeds; - - /// - /// Gets whether there was a timeout when checking the reachability of the explicitly configured NuGet feeds. - /// - public bool ExplicitFeedTimeout => lazyReachableExplicitFeeds.Value.Item1; + private readonly Lazy> lazyReachableExplicitFeeds; /// /// Gets the list of reachable NuGet feeds that are explicitly configured. /// - public ImmutableHashSet ReachableExplicitFeeds => lazyReachableExplicitFeeds.Value.Item2; + public ImmutableHashSet ReachableExplicitFeeds => lazyReachableExplicitFeeds.Value; private readonly Lazy> lazyReachableFeeds; /// @@ -96,15 +91,11 @@ public FeedManager(ILogger logger, IDotNet dotnet, DependabotProxy? dependabotPr lazyExplicitFeeds = new Lazy>(GetExplicitFeeds); lazyAllFeeds = new Lazy>(GetAllFeeds); - lazyReachableExplicitFeeds = new Lazy<(bool, ImmutableHashSet)>(() => - { - var timeout = CheckSpecifiedFeeds(ExplicitFeeds, out var reachableFeeds); - return (timeout, reachableFeeds); - }); + lazyReachableExplicitFeeds = new Lazy>(() => CheckSpecifiedFeeds(ExplicitFeeds)); lazyReachableFeeds = new Lazy>(() => { // Inherited feeds should only be used, if they are indeed reachable (as they may be environment specific). - CheckSpecifiedFeeds(InheritedFeeds, out var reachableInheritedFeeds); + var reachableInheritedFeeds = CheckSpecifiedFeeds(InheritedFeeds); return ReachableExplicitFeeds.Union(reachableInheritedFeeds).ToImmutableHashSet(); }); lazyReachableFallbackFeeds = new Lazy>(() => @@ -271,7 +262,7 @@ private static async Task ExecuteGetRequest(string address, return await httpClient.GetAsync(address, HttpCompletionOption.ResponseHeadersRead, cancellationToken); } - private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount, out bool isTimeout) + private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount) { logger.LogInfo($"Checking if NuGet feed '{feed}' is reachable..."); @@ -304,8 +295,6 @@ private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount, using HttpClient client = new(httpClientHandler); - isTimeout = false; - for (var i = 0; i < tryCount; i++) { using var cts = new CancellationTokenSource(); @@ -335,7 +324,6 @@ private bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount, } logger.LogWarning($"Didn't receive answer from NuGet feed '{feed}'. Tried it {tryCount} times."); - isTimeout = true; return false; } @@ -359,12 +347,8 @@ private HashSet GetExcludedFeeds() /// Checks that we can connect to the specified NuGet feeds. /// /// The set of package feeds to check. - /// The list of feeds that were reachable. - /// - /// True if there is a timeout when trying to reach the feeds (excluding any feeds that are configured - /// to be excluded from the check) or false otherwise. - /// - private bool CheckSpecifiedFeeds(ImmutableHashSet feeds, out ImmutableHashSet reachableFeeds) + /// The list of feeds that were reachable. + private ImmutableHashSet CheckSpecifiedFeeds(ImmutableHashSet feeds) { // Exclude any feeds from the feed check that are configured by the corresponding environment variable. // These feeds are always assumed to be reachable. @@ -380,12 +364,10 @@ private bool CheckSpecifiedFeeds(ImmutableHashSet feeds, out ImmutableHa return true; }).ToHashSet(); - var reachable = GetReachableNuGetFeeds(feedsToCheck, isFallback: false, out var isTimeout); + var reachable = GetReachableNuGetFeeds(feedsToCheck, isFallback: false); // Always consider feeds excluded for the reachability check as reachable. - reachableFeeds = reachable.Union(feeds.Where(feed => excludedFeeds.Contains(feed))).ToImmutableHashSet(); - - return isTimeout; + return reachable.Union(feeds.Where(feed => excludedFeeds.Contains(feed))).ToImmutableHashSet(); } /// @@ -398,7 +380,7 @@ public bool IsDefaultFeedReachable() if (CheckNugetFeedResponsiveness) { var (initialTimeout, tryCount) = GetFeedRequestSettings(isFallback: false); - return IsFeedReachable(PublicNugetOrgFeed, initialTimeout, tryCount, out var _); + return IsFeedReachable(PublicNugetOrgFeed, initialTimeout, tryCount); } return true; @@ -409,22 +391,15 @@ public bool IsDefaultFeedReachable() /// /// The feeds to check. /// Whether the feeds are fallback feeds or not. - /// Whether a timeout occurred while checking the feeds. /// The list of feeds that could be reached. - private List GetReachableNuGetFeeds(HashSet feedsToCheck, bool isFallback, out bool isTimeout) + private List GetReachableNuGetFeeds(HashSet feedsToCheck, bool isFallback) { var fallbackStr = isFallback ? "fallback " : ""; logger.LogInfo($"Checking {fallbackStr}NuGet feed reachability on feeds: {string.Join(", ", feedsToCheck.OrderBy(f => f))}"); var (initialTimeout, tryCount) = GetFeedRequestSettings(isFallback); - var timeout = false; var reachableFeeds = feedsToCheck - .Where(feed => - { - var reachable = IsFeedReachable(feed, initialTimeout, tryCount, out var feedTimeout); - timeout |= feedTimeout; - return reachable; - }) + .Where(feed => IsFeedReachable(feed, initialTimeout, tryCount)) .ToList(); if (reachableFeeds.Count == 0) @@ -436,7 +411,6 @@ private List GetReachableNuGetFeeds(HashSet feedsToCheck, bool i logger.LogInfo($"Reachable {fallbackStr}NuGet feeds: {string.Join(", ", reachableFeeds.OrderBy(f => f))}"); } - isTimeout = timeout; return reachableFeeds; } @@ -460,7 +434,7 @@ private List GetReachableFallbackNugetFeeds() } } - return GetReachableNuGetFeeds(fallbackFeeds, isFallback: true, out var _); + return GetReachableNuGetFeeds(fallbackFeeds, isFallback: true); } private ImmutableHashSet GetExplicitFeeds() diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs index aeedbc176867..38924e2f876e 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs @@ -129,17 +129,6 @@ public HashSet Restore() var allExplicitReachable = explicitFeeds.Count == feedManager.ReachableExplicitFeeds.Count; EmitUnreachableFeedsDiagnostics(allExplicitReachable); - - if (feedManager.ExplicitFeedTimeout) - { - // If we experience a timeout, we use this fallback. - // todo: we could also check the reachability of the inherited nuget feeds, but to use those in the fallback we would need to handle authentication too. - var unresponsiveMissingPackageLocation = DownloadMissingPackages([]); - return unresponsiveMissingPackageLocation is null - ? [] - : [unresponsiveMissingPackageLocation]; - } - } try @@ -625,14 +614,6 @@ public void Dispose() feedManager.Dispose(); } - /// - /// Returns the full path to a temporary directory with the given subfolder name. - /// - private static string ComputeTempDirectoryPath(string subfolderName) - { - return Path.Join(FileUtils.GetTemporaryWorkingDirectory(out _), subfolderName); - } - /// /// Computes a unique temporary directory path based on the source directory and the subfolder name. /// diff --git a/csharp/ql/integration-tests/posix/standalone_dependencies_nuget_config_error_timeout/CompilationInfo.expected b/csharp/ql/integration-tests/posix/standalone_dependencies_nuget_config_error_timeout/CompilationInfo.expected index 5a7abcf543c8..4acd4f54e8a6 100644 --- a/csharp/ql/integration-tests/posix/standalone_dependencies_nuget_config_error_timeout/CompilationInfo.expected +++ b/csharp/ql/integration-tests/posix/standalone_dependencies_nuget_config_error_timeout/CompilationInfo.expected @@ -1,4 +1,8 @@ | All NuGet feeds reachable | 0.0 | +| Failed project restore with missing package error | 1.0 | +| Failed project restore with package source error | 0.0 | +| Failed solution restore with missing package error | 0.0 | +| Failed solution restore with package source error | 0.0 | | Fallback nuget restore | 1.0 | | Inherited NuGet feed count | 1.0 | | NuGet feed responsiveness checked | 1.0 | @@ -7,10 +11,13 @@ | Resolved assembly conflicts | 7.0 | | Resource extraction enabled | 0.0 | | Restored .NET framework variants | 0.0 | +| Restored projects through solution files | 0.0 | | Solution files on filesystem | 1.0 | | Source files generated | 0.0 | | Source files on filesystem | 1.0 | | Successfully ran fallback nuget restore | 1.0 | +| Successfully restored project files | 0.0 | +| Successfully restored solution files | 1.0 | | Unresolved references | 0.0 | | UseWPF set | 0.0 | | UseWindowsForms set | 0.0 | diff --git a/csharp/ql/integration-tests/posix/standalone_dependencies_nuget_config_fallback/CompilationInfo.expected b/csharp/ql/integration-tests/posix/standalone_dependencies_nuget_config_fallback/CompilationInfo.expected index 9cc03f2f5372..d421cfc42e1a 100644 --- a/csharp/ql/integration-tests/posix/standalone_dependencies_nuget_config_fallback/CompilationInfo.expected +++ b/csharp/ql/integration-tests/posix/standalone_dependencies_nuget_config_fallback/CompilationInfo.expected @@ -1,4 +1,8 @@ | All NuGet feeds reachable | 0.0 | +| Failed project restore with missing package error | 1.0 | +| Failed project restore with package source error | 0.0 | +| Failed solution restore with missing package error | 0.0 | +| Failed solution restore with package source error | 0.0 | | Fallback nuget restore | 1.0 | | Inherited NuGet feed count | 1.0 | | NuGet feed responsiveness checked | 1.0 | @@ -7,10 +11,13 @@ | Resolved assembly conflicts | 7.0 | | Resource extraction enabled | 0.0 | | Restored .NET framework variants | 0.0 | +| Restored projects through solution files | 0.0 | | Solution files on filesystem | 1.0 | | Source files generated | 0.0 | | Source files on filesystem | 1.0 | | Successfully ran fallback nuget restore | 1.0 | +| Successfully restored project files | 0.0 | +| Successfully restored solution files | 1.0 | | Unresolved references | 0.0 | | UseWPF set | 0.0 | | UseWindowsForms set | 0.0 |