Skip to content

Commit 6f45de1

Browse files
authored
Merge pull request github#15325 from RasmusWL/c#-filter-order
C#: Respect order of `LGTM_INDEX_FILTERS` in buildless extraction
2 parents 2246c96 + 13c2362 commit 6f45de1

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,18 @@ public IEnumerable<FileInfo> Filter(IEnumerable<FileInfo> files)
7474
includeByDefault)
7575
});
7676

77-
// Move included pathfilters to the front of the list:
78-
pathFilters.Sort((pf1, pf2) => -1 * pf1.Include.CompareTo(pf2.Include));
7977
return unfilteredResult.Where(f =>
8078
{
8179
var include = f.FileInclusion.Include;
82-
foreach (var pathFilter in pathFilters)
80+
// LGTM_INDEX_FILTERS is a prioritized list, where later filters take
81+
// priority over earlier ones.
82+
for (int i = pathFilters.Count - 1; i >= 0; i--)
8383
{
84+
var pathFilter = pathFilters[i];
8485
if (pathFilter.Regex.IsMatch(f.FileInclusion.Path))
8586
{
8687
include = pathFilter.Include;
88+
break;
8789
}
8890
}
8991

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public void TestFiltersWithIncludeExcludeExcludeFirst()
165165
{
166166
(var testSubject, var progressMonitor, var files) = TestSetup();
167167

168+
// NOTE: the ordering DOES matter, later filters takes priority, so the exclude will end up not mattering at all.
168169
Environment.SetEnvironmentVariable("LGTM_INDEX_FILTERS", """
169170
exclude:c/x/z
170171
include:c/x
@@ -174,7 +175,8 @@ public void TestFiltersWithIncludeExcludeExcludeFirst()
174175

175176
var expected = GetExpected(
176177
[
177-
"/a/b/c/x/y/i.cs"
178+
"/a/b/c/x/y/i.cs",
179+
"/a/b/c/x/z/i.cs"
178180
]);
179181

180182
AssertFileInfoEquivalence(expected, filtered);

0 commit comments

Comments
 (0)