Skip to content

Commit 87d0d72

Browse files
tamasvajkmichaelnebel
authored andcommitted
C#: Fix lazy evaluation of not yet downloaded packages
1 parent afe1e9c commit 87d0d72

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
1212
// This class is used to read a set of files and decide different properties about the
1313
// content (by reading the content of the files only once).
1414
// The implementation is lazy, so the properties are only calculated when
15-
// the first property is accessed.
15+
// the first property is accessed.
1616
// </summary>
1717
internal partial class FileContent
1818
{
1919
private readonly ProgressMonitor progressMonitor;
2020
private readonly IUnsafeFileReader unsafeFileReader;
2121
private readonly Func<IEnumerable<string>> getFiles;
2222
private readonly Func<HashSet<string>> getAlreadyDownloadedPackages;
23-
private readonly HashSet<string> notYetDownloadedPackages = new HashSet<string>();
23+
private readonly HashSet<string> allPackages = new HashSet<string>();
2424
private readonly Initializer initialize;
2525

26-
public HashSet<string> NotYetDownloadedPackages
26+
public IEnumerable<string> NotYetDownloadedPackages
2727
{
2828
get
2929
{
3030
initialize.Run();
31-
return notYetDownloadedPackages;
31+
var alreadyDownloadedPackages = getAlreadyDownloadedPackages();
32+
return allPackages.Except(alreadyDownloadedPackages);
3233
}
3334
}
3435

@@ -101,22 +102,21 @@ private static bool IsGroupMatch(ReadOnlySpan<char> line, Regex regex, string gr
101102

102103
private void DoInitialize()
103104
{
104-
var alreadyDownloadedPackages = getAlreadyDownloadedPackages();
105105
foreach (var file in getFiles())
106106
{
107107
try
108108
{
109109
foreach (ReadOnlySpan<char> line in unsafeFileReader.ReadLines(file))
110110
{
111111

112-
// Find the not yet downloaded packages.
112+
// Find all the packages.
113113
foreach (var valueMatch in PackageReference().EnumerateMatches(line))
114114
{
115115
// We can't get the group from the ValueMatch, so doing it manually:
116116
var packageName = GetGroup(line, valueMatch, "Include");
117-
if (!string.IsNullOrEmpty(packageName) && !alreadyDownloadedPackages.Contains(packageName))
117+
if (!string.IsNullOrEmpty(packageName) && !allPackages.Contains(packageName))
118118
{
119-
notYetDownloadedPackages.Add(packageName);
119+
allPackages.Add(packageName);
120120
}
121121
}
122122

csharp/extractor/Semmle.Extraction.Tests/FileContent.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Xunit;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using Semmle.Util.Logging;
45
using Semmle.Extraction.CSharp.DependencyFetching;
56

@@ -62,7 +63,7 @@ public void TestFileContent1()
6263

6364
// Verify
6465
Assert.False(useAspNetDlls);
65-
Assert.Equal(3, notYetDownloadedPackages.Count);
66+
Assert.Equal(3, notYetDownloadedPackages.Count());
6667
Assert.Contains("DotNetAnalyzers.DocumentationAnalyzers".ToLowerInvariant(), notYetDownloadedPackages);
6768
Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), notYetDownloadedPackages);
6869
Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), notYetDownloadedPackages);
@@ -87,7 +88,7 @@ public void TestFileContent2()
8788

8889
// Verify
8990
Assert.True(useAspNetDlls);
90-
Assert.Equal(2, notYetDownloadedPackages.Count);
91+
Assert.Equal(2, notYetDownloadedPackages.Count());
9192
Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), notYetDownloadedPackages);
9293
Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), notYetDownloadedPackages);
9394
}

0 commit comments

Comments
 (0)