Skip to content

Commit 81b3ff2

Browse files
authored
Merge pull request github#14545 from tamasvajk/standalone/remove-netstandard.library.ref
C#: Handle `netstandard` references in standalone extraction
2 parents 79e1aa0 + 3f1e145 commit 81b3ff2

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

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

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,22 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
9999

100100
var existsNetCoreRefNugetPackage = false;
101101
var existsNetFrameworkRefNugetPackage = false;
102+
var existsNetstandardLibRefNugetPackage = false;
103+
var existsNetstandardLibNugetPackage = false;
102104

103105
// Find DLLs in the .Net / Asp.Net Framework
104106
// This block needs to come after the nuget restore, because the nuget restore might fetch the .NET Core/Framework reference assemblies.
105107
if (options.ScanNetFrameworkDlls)
106108
{
107109
existsNetCoreRefNugetPackage = IsNugetPackageAvailable("microsoft.netcore.app.ref");
108110
existsNetFrameworkRefNugetPackage = IsNugetPackageAvailable("microsoft.netframework.referenceassemblies");
111+
existsNetstandardLibRefNugetPackage = IsNugetPackageAvailable("netstandard.library.ref");
112+
existsNetstandardLibNugetPackage = IsNugetPackageAvailable("netstandard.library");
109113

110-
if (existsNetCoreRefNugetPackage || existsNetFrameworkRefNugetPackage)
114+
if (existsNetCoreRefNugetPackage
115+
|| existsNetFrameworkRefNugetPackage
116+
|| existsNetstandardLibRefNugetPackage
117+
|| existsNetstandardLibNugetPackage)
111118
{
112119
progressMonitor.LogInfo("Found .NET Core/Framework DLLs in NuGet packages. Not adding installation directory.");
113120
}
@@ -125,7 +132,7 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
125132
UseReference(filename);
126133
}
127134

128-
RemoveUnnecessaryNugetPackages(existsNetCoreRefNugetPackage, existsNetFrameworkRefNugetPackage);
135+
RemoveUnnecessaryNugetPackages(existsNetCoreRefNugetPackage, existsNetFrameworkRefNugetPackage, existsNetstandardLibRefNugetPackage, existsNetstandardLibNugetPackage);
129136
ResolveConflicts();
130137

131138
// Output the findings
@@ -160,7 +167,8 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
160167
DateTime.Now - startTime);
161168
}
162169

163-
private void RemoveUnnecessaryNugetPackages(bool existsNetCoreRefNugetPackage, bool existsNetFrameworkRefNugetPackage)
170+
private void RemoveUnnecessaryNugetPackages(bool existsNetCoreRefNugetPackage, bool existsNetFrameworkRefNugetPackage,
171+
bool existsNetstandardLibRefNugetPackage, bool existsNetstandardLibNugetPackage)
164172
{
165173
RemoveNugetAnalyzerReferences();
166174
RemoveRuntimeNugetPackageReferences();
@@ -172,10 +180,38 @@ private void RemoveUnnecessaryNugetPackages(bool existsNetCoreRefNugetPackage, b
172180
RemoveNugetPackageReference("microsoft.aspnetcore.app.ref");
173181
}
174182

175-
if (existsNetCoreRefNugetPackage && existsNetFrameworkRefNugetPackage)
183+
// Multiple dotnet framework packages could be present. We keep only one.
184+
// The order of the packages is important, we're keeping the first one that is present in the nuget cache.
185+
var packagesInPrioOrder = new (bool isPresent, string prefix)[]
176186
{
177-
// Multiple packages are available, we keep only one:
178-
RemoveNugetPackageReference("microsoft.netframework.referenceassemblies.");
187+
// net7.0, ... net5.0, netcoreapp3.1, netcoreapp3.0
188+
(existsNetCoreRefNugetPackage, "microsoft.netcore.app.ref"),
189+
// net48, ..., net20
190+
(existsNetFrameworkRefNugetPackage, "microsoft.netframework.referenceassemblies."),
191+
// netstandard2.1
192+
(existsNetstandardLibRefNugetPackage, "netstandard.library.ref"),
193+
// netstandard2.0
194+
(existsNetstandardLibNugetPackage, "netstandard.library")
195+
};
196+
197+
for (var i = 0; i < packagesInPrioOrder.Length; i++)
198+
{
199+
var (isPresent, _) = packagesInPrioOrder[i];
200+
if (!isPresent)
201+
{
202+
continue;
203+
}
204+
205+
// Package is present, remove all the lower priority packages:
206+
for (var j = i + 1; j < packagesInPrioOrder.Length; j++)
207+
{
208+
var (otherIsPresent, otherPrefix) = packagesInPrioOrder[j];
209+
if (otherIsPresent)
210+
{
211+
RemoveNugetPackageReference(otherPrefix);
212+
}
213+
}
214+
break;
179215
}
180216

181217
// TODO: There could be multiple `microsoft.netframework.referenceassemblies` packages,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,5 @@
168168
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/WindowsBase.dll |
169169
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/mscorlib.dll |
170170
| /microsoft.netcore.app.ref/7.0.2/ref/net7.0/netstandard.dll |
171-
| /netstandard.library/2.0.3/build/netstandard2.0/ref/System.ComponentModel.Composition.dll |
172171
| /newtonsoft.json/12.0.1/lib/portable-net45+win8+wp8+wpa81/Newtonsoft.Json.dll |
173172
| /nunit/3.13.3/lib/netstandard2.0/nunit.framework.dll |

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,5 @@
212212
| /microsoft.windowsdesktop.app.ref/7.0.2/ref/net7.0/UIAutomationTypes.dll |
213213
| /microsoft.windowsdesktop.app.ref/7.0.2/ref/net7.0/WindowsBase.dll |
214214
| /microsoft.windowsdesktop.app.ref/7.0.2/ref/net7.0/WindowsFormsIntegration.dll |
215-
| /netstandard.library/2.0.3/build/netstandard2.0/ref/System.ComponentModel.Composition.dll |
216215
| /newtonsoft.json/12.0.1/lib/portable-net45+win8+wp8+wpa81/Newtonsoft.Json.dll |
217216
| /nunit/3.13.3/lib/netstandard2.0/nunit.framework.dll |

0 commit comments

Comments
 (0)