Skip to content

Commit 09c1c71

Browse files
committed
Filter analyzer assemblies from nuget packages
1 parent 8a0dc31 commit 09c1c71

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
161161

162162
private void RemoveUnnecessaryNugetPackages(bool existsNetCoreRefNugetPackage, bool existsNetFrameworkRefNugetPackage)
163163
{
164+
RemoveNugetAnalyzerReferences();
164165
RemoveRuntimeNugetPackageReferences();
165166

166167
if (fileContent.IsNewProjectStructureUsed
@@ -181,6 +182,45 @@ private void RemoveUnnecessaryNugetPackages(bool existsNetCoreRefNugetPackage, b
181182
// (if the file names match)
182183
}
183184

185+
private void RemoveNugetAnalyzerReferences()
186+
{
187+
if (!options.UseNuGet)
188+
{
189+
return;
190+
}
191+
192+
var packageFolder = packageDirectory.DirInfo.FullName.ToLowerInvariant();
193+
if (packageFolder == null)
194+
{
195+
return;
196+
}
197+
198+
foreach (var filename in usedReferences.Keys)
199+
{
200+
var lowerFilename = filename.ToLowerInvariant();
201+
202+
if (lowerFilename.StartsWith(packageFolder))
203+
{
204+
var firstDirectorySeparatorCharIndex = lowerFilename.IndexOf(Path.DirectorySeparatorChar, packageFolder.Length + 1);
205+
if (firstDirectorySeparatorCharIndex == -1)
206+
{
207+
continue;
208+
}
209+
var secondDirectorySeparatorCharIndex = lowerFilename.IndexOf(Path.DirectorySeparatorChar, firstDirectorySeparatorCharIndex + 1);
210+
if (secondDirectorySeparatorCharIndex == -1)
211+
{
212+
continue;
213+
}
214+
var subFolderIndex = secondDirectorySeparatorCharIndex + 1;
215+
var isInAnalyzersFolder = lowerFilename.IndexOf("analyzers", subFolderIndex) == subFolderIndex;
216+
if (isInAnalyzersFolder)
217+
{
218+
usedReferences.Remove(filename);
219+
progressMonitor.RemovedReference(filename);
220+
}
221+
}
222+
}
223+
}
184224
private void AddNetFrameworkDlls(List<string> dllDirNames)
185225
{
186226
var runtime = new Runtime(dotnet);
@@ -240,6 +280,11 @@ private void RemoveNugetPackageReference(params string[] packagePrefixes)
240280
}
241281

242282
var packageFolder = packageDirectory.DirInfo.FullName.ToLowerInvariant();
283+
if (packageFolder == null)
284+
{
285+
return;
286+
}
287+
243288
var packagePathPrefixes = packagePrefixes.Select(p => Path.Combine(packageFolder, p.ToLowerInvariant()));
244289

245290
foreach (var filename in usedReferences.Keys)

csharp/ql/integration-tests/posix-only/standalone_dependencies/Assemblies.expected

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@
88
| /microsoft.netcore.app.ref/6.0.13/ref/net6.0/System.Data.dll |
99
| /microsoft.netcore.app.ref/6.0.13/ref/net6.0/System.Xml.dll |
1010
| /microsoft.netcore.app.ref/6.0.13/ref/net6.0/System.dll |
11-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/Microsoft.Interop.JavaScript.JSImportGenerator.dll |
12-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/Microsoft.Interop.LibraryImportGenerator.dll |
13-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/Microsoft.Interop.SourceGeneration.dll |
14-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/System.Text.Json.SourceGeneration.dll |
15-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/System.Text.RegularExpressions.Generator.dll |
16-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/zh-Hant/Microsoft.Interop.LibraryImportGenerator.resources.dll |
17-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/zh-Hant/Microsoft.Interop.SourceGeneration.resources.dll |
18-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/zh-Hant/System.Text.Json.SourceGeneration.resources.dll |
19-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/zh-Hant/System.Text.RegularExpressions.Generator.resources.dll |
2011
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/Microsoft.CSharp.dll |
2112
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/Microsoft.VisualBasic.Core.dll |
2213
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/Microsoft.VisualBasic.dll |

csharp/ql/integration-tests/windows-only/standalone_dependencies/Assemblies.expected

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@
88
| /microsoft.netcore.app.ref/6.0.13/ref/net6.0/System.Data.dll |
99
| /microsoft.netcore.app.ref/6.0.13/ref/net6.0/System.Xml.dll |
1010
| /microsoft.netcore.app.ref/6.0.13/ref/net6.0/System.dll |
11-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/Microsoft.Interop.JavaScript.JSImportGenerator.dll |
12-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/Microsoft.Interop.LibraryImportGenerator.dll |
13-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/Microsoft.Interop.SourceGeneration.dll |
14-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/System.Text.Json.SourceGeneration.dll |
15-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/System.Text.RegularExpressions.Generator.dll |
16-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/zh-Hant/Microsoft.Interop.LibraryImportGenerator.resources.dll |
17-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/zh-Hant/Microsoft.Interop.SourceGeneration.resources.dll |
18-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/zh-Hant/System.Text.Json.SourceGeneration.resources.dll |
19-
| /microsoft.netcore.app.ref/7.0.2/analyzers/dotnet/cs/zh-Hant/System.Text.RegularExpressions.Generator.resources.dll |
2011
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/Microsoft.CSharp.dll |
2112
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/Microsoft.VisualBasic.Core.dll |
2213
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/Microsoft.Win32.Primitives.dll |
@@ -174,10 +165,6 @@
174165
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/System.Xml.XmlSerializer.dll |
175166
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/mscorlib.dll |
176167
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/netstandard.dll |
177-
| /microsoft.windowsdesktop.app.ref/7.0.2/analyzers/dotnet/System.Windows.Forms.Analyzers.dll |
178-
| /microsoft.windowsdesktop.app.ref/7.0.2/analyzers/dotnet/cs/System.Windows.Forms.Analyzers.CSharp.dll |
179-
| /microsoft.windowsdesktop.app.ref/7.0.2/analyzers/dotnet/cs/zh-Hant/System.Windows.Forms.Analyzers.CSharp.resources.dll |
180-
| /microsoft.windowsdesktop.app.ref/7.0.2/analyzers/dotnet/zh-Hant/System.Windows.Forms.Analyzers.resources.dll |
181168
| /microsoft.windowsdesktop.app.ref/7.0.2/ref/net7.0/Accessibility.dll |
182169
| /microsoft.windowsdesktop.app.ref/7.0.2/ref/net7.0/Microsoft.VisualBasic.Forms.dll |
183170
| /microsoft.windowsdesktop.app.ref/7.0.2/ref/net7.0/Microsoft.VisualBasic.dll |

0 commit comments

Comments
 (0)