Skip to content

Commit 0f31fc7

Browse files
committed
C#: Keep only one framework reference nuget package in standalone
1 parent 4e70e67 commit 0f31fc7

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

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

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
8282
? new[] { options.SolutionFile }
8383
: allNonBinaryFiles.SelectFileNamesByExtension(".sln");
8484
var dllPaths = options.DllDirs.Count == 0
85-
? allFiles.SelectFileNamesByExtension(".dll").ToList()
86-
: options.DllDirs.Select(Path.GetFullPath).ToList();
85+
? allFiles.SelectFileNamesByExtension(".dll").ToHashSet()
86+
: options.DllDirs.Select(Path.GetFullPath).ToHashSet();
8787

8888
if (options.UseNuGet)
8989
{
@@ -107,7 +107,7 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
107107
.RequiredPaths
108108
.Select(d => Path.Combine(packageDirectory.DirInfo.FullName, d))
109109
.ToList();
110-
dllPaths.AddRange(paths);
110+
dllPaths.UnionWith(paths);
111111

112112
LogAllUnusedPackages(dependencies);
113113
DownloadMissingPackages(allNonBinaryFiles, dllPaths);
@@ -205,7 +205,7 @@ private void RemoveNugetAnalyzerReferences()
205205
}
206206
}
207207

208-
private void AddNetFrameworkDlls(List<string> dllPaths)
208+
private void AddNetFrameworkDlls(ISet<string> dllPaths)
209209
{
210210
// Multiple dotnet framework packages could be present.
211211
// The order of the packages is important, we're adding the first one that is present in the nuget cache.
@@ -218,13 +218,19 @@ private void AddNetFrameworkDlls(List<string> dllPaths)
218218
};
219219

220220
var frameworkPath = packagesInPrioOrder
221-
.Select(GetPackageDirectory)
222-
.FirstOrDefault(dir => dir is not null);
221+
.Select((s, index) => (Index: index, Path: GetPackageDirectory(s)))
222+
.FirstOrDefault(pair => pair.Path is not null);
223223

224-
if (frameworkPath is not null)
224+
if (frameworkPath.Path is not null)
225225
{
226-
dllPaths.Add(frameworkPath);
227-
progressMonitor.LogInfo("Found .NET Core/Framework DLLs in NuGet packages. Not adding installation directory.");
226+
dllPaths.Add(frameworkPath.Path);
227+
progressMonitor.LogInfo($"Found .NET Core/Framework DLLs in NuGet packages at {frameworkPath.Path}. Not adding installation directory.");
228+
229+
for (var i = frameworkPath.Index + 1; i < packagesInPrioOrder.Length; i++)
230+
{
231+
RemoveNugetPackageReference(packagesInPrioOrder[i], dllPaths);
232+
}
233+
228234
return;
229235
}
230236

@@ -249,7 +255,29 @@ private void AddNetFrameworkDlls(List<string> dllPaths)
249255
dllPaths.Add(runtimeLocation);
250256
}
251257

252-
private void AddAspNetCoreFrameworkDlls(List<string> dllPaths)
258+
private void RemoveNugetPackageReference(string packagePrefix, ISet<string> dllPaths)
259+
{
260+
if (!options.UseNuGet)
261+
{
262+
return;
263+
}
264+
265+
var packageFolder = packageDirectory.DirInfo.FullName.ToLowerInvariant();
266+
if (packageFolder == null)
267+
{
268+
return;
269+
}
270+
271+
var packagePathPrefix = Path.Combine(packageFolder, packagePrefix.ToLowerInvariant());
272+
var toRemove = dllPaths.Where(s => s.ToLowerInvariant().StartsWith(packagePathPrefix));
273+
foreach (var path in toRemove)
274+
{
275+
dllPaths.Remove(path);
276+
progressMonitor.RemovedReference(path);
277+
}
278+
}
279+
280+
private void AddAspNetCoreFrameworkDlls(ISet<string> dllPaths)
253281
{
254282
if (!fileContent.IsNewProjectStructureUsed || !fileContent.UseAspNetCoreDlls)
255283
{
@@ -269,7 +297,7 @@ private void AddAspNetCoreFrameworkDlls(List<string> dllPaths)
269297
}
270298
}
271299

272-
private void AddMicrosoftWindowsDesktopDlls(List<string> dllPaths)
300+
private void AddMicrosoftWindowsDesktopDlls(ISet<string> dllPaths)
273301
{
274302
if (GetPackageDirectory("microsoft.windowsdesktop.app.ref") is string windowsDesktopApp)
275303
{
@@ -628,7 +656,7 @@ private void RestoreProjects(IEnumerable<string> projects, out IEnumerable<strin
628656
assets = assetFiles;
629657
}
630658

631-
private void DownloadMissingPackages(List<FileInfo> allFiles, List<string> dllPaths)
659+
private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<string> dllPaths)
632660
{
633661
var nugetConfigs = allFiles.SelectFileNamesByName("nuget.config").ToArray();
634662
string? nugetConfig = null;

0 commit comments

Comments
 (0)