Skip to content

Commit 3487f9d

Browse files
authored
Merge pull request #15070 from tamasvajk/standalone/exclusions
C#: Remove unneeded options and add support for `paths/paths-ignore` in standalone
2 parents 7da10e0 + ee70de8 commit 3487f9d

File tree

12 files changed

+442
-231
lines changed

12 files changed

+442
-231
lines changed

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,15 +590,15 @@ public void TestLinuxBuildlessExtractionFailed()
590590
[Fact]
591591
public void TestLinuxBuildlessExtractionSolution()
592592
{
593-
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone foo.sln"] = 0;
593+
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone"] = 0;
594594
actions.FileExists["csharp.log"] = true;
595595
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_TRAP_DIR"] = "";
596596
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_SOURCE_ARCHIVE_DIR"] = "";
597597
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_SCRATCH_DIR"] = "scratch";
598598
actions.EnumerateFiles[@"C:\Project"] = "foo.cs\ntest.sln";
599599
actions.EnumerateDirectories[@"C:\Project"] = "";
600600

601-
var autobuilder = CreateAutoBuilder(false, buildless: "true", solution: "foo.sln");
601+
var autobuilder = CreateAutoBuilder(false, buildless: "true");
602602
TestAutobuilderScript(autobuilder, 0, 1);
603603
}
604604

@@ -890,15 +890,15 @@ public void TestSkipNugetMsBuild()
890890
[Fact]
891891
public void TestSkipNugetBuildless()
892892
{
893-
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone foo.sln --skip-nuget"] = 0;
893+
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone"] = 0;
894894
actions.FileExists["csharp.log"] = true;
895895
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_TRAP_DIR"] = "";
896896
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_SOURCE_ARCHIVE_DIR"] = "";
897897
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_SCRATCH_DIR"] = "scratch";
898898
actions.EnumerateFiles[@"C:\Project"] = "foo.cs\ntest.sln";
899899
actions.EnumerateDirectories[@"C:\Project"] = "";
900900

901-
var autobuilder = CreateAutoBuilder(false, buildless: "true", solution: "foo.sln", nugetRestore: "false");
901+
var autobuilder = CreateAutoBuilder(false, buildless: "true");
902902
TestAutobuilderScript(autobuilder, 0, 1);
903903
}
904904

csharp/autobuilder/Semmle.Autobuild.CSharp/StandaloneBuildRule.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal StandaloneBuildRule(string? dotNetPath)
1717

1818
public BuildScript Analyse(IAutobuilder<CSharpAutobuildOptions> builder, bool auto)
1919
{
20-
BuildScript GetCommand(string? solution)
20+
BuildScript GetCommand()
2121
{
2222
string standalone;
2323
if (builder.CodeQLExtractorLangRoot is not null && builder.CodeQlPlatform is not null)
@@ -32,14 +32,6 @@ BuildScript GetCommand(string? solution)
3232
var cmd = new CommandBuilder(builder.Actions);
3333
cmd.RunCommand(standalone);
3434

35-
if (solution is not null)
36-
cmd.QuoteArgument(solution);
37-
38-
if (!builder.Options.NugetRestore)
39-
{
40-
cmd.Argument("--skip-nuget");
41-
}
42-
4335
if (!string.IsNullOrEmpty(this.dotNetPath))
4436
{
4537
cmd.Argument("--dotnet");
@@ -50,16 +42,11 @@ BuildScript GetCommand(string? solution)
5042
}
5143

5244
if (!builder.Options.Buildless)
45+
{
5346
return BuildScript.Failure;
47+
}
5448

55-
if (!builder.Options.Solution.Any())
56-
return GetCommand(null);
57-
58-
var script = BuildScript.Success;
59-
foreach (var solution in builder.Options.Solution)
60-
script &= GetCommand(solution);
61-
62-
return script;
49+
return GetCommand();
6350
}
6451
}
6552
}

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

Lines changed: 61 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -80,67 +80,16 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
8080
this.nonGeneratedSources = allNonBinaryFiles.SelectFileNamesByExtension(".cs").ToList();
8181
this.generatedSources = new();
8282
var allProjects = allNonBinaryFiles.SelectFileNamesByExtension(".csproj");
83-
var solutions = options.SolutionFile is not null
84-
? new[] { options.SolutionFile }
85-
: allNonBinaryFiles.SelectFileNamesByExtension(".sln");
86-
var dllPaths = options.DllDirs.Count == 0
87-
? allFiles.SelectFileNamesByExtension(".dll").ToHashSet()
88-
: options.DllDirs.Select(Path.GetFullPath).ToHashSet();
89-
90-
if (options.UseNuGet)
91-
{
92-
try
93-
{
94-
var nuget = new NugetPackages(sourceDir.FullName, legacyPackageDirectory, progressMonitor);
95-
nuget.InstallPackages();
96-
97-
var nugetPackageDlls = legacyPackageDirectory.DirInfo.GetFiles("*.dll", new EnumerationOptions { RecurseSubdirectories = true });
98-
var nugetPackageDllPaths = nugetPackageDlls.Select(f => f.FullName).ToHashSet();
99-
var excludedPaths = nugetPackageDllPaths
100-
.Where(path => IsPathInSubfolder(path, legacyPackageDirectory.DirInfo.FullName, "tools"));
101-
102-
foreach (var excludedPath in excludedPaths)
103-
{
104-
progressMonitor.LogInfo($"Excluded Nuget DLL: {excludedPath}");
105-
}
106-
107-
nugetPackageDllPaths.ExceptWith(excludedPaths);
108-
dllPaths.UnionWith(nugetPackageDllPaths);
109-
}
110-
catch (FileNotFoundException)
111-
{
112-
progressMonitor.MissingNuGet();
113-
}
114-
115-
var restoredProjects = RestoreSolutions(solutions, out var assets1);
116-
var projects = allProjects.Except(restoredProjects);
117-
RestoreProjects(projects, out var assets2);
118-
119-
var dependencies = Assets.GetCompilationDependencies(progressMonitor, assets1.Union(assets2));
120-
121-
var paths = dependencies
122-
.Paths
123-
.Select(d => Path.Combine(packageDirectory.DirInfo.FullName, d))
124-
.ToList();
125-
dllPaths.UnionWith(paths);
126-
127-
LogAllUnusedPackages(dependencies);
128-
DownloadMissingPackages(allNonBinaryFiles, dllPaths);
129-
}
130-
131-
var frameworkLocations = new HashSet<string>();
83+
var allSolutions = allNonBinaryFiles.SelectFileNamesByExtension(".sln");
84+
var dllPaths = allFiles.SelectFileNamesByExtension(".dll").ToHashSet();
13285

86+
RestoreNugetPackages(allNonBinaryFiles, allProjects, allSolutions, dllPaths);
13387
// Find DLLs in the .Net / Asp.Net Framework
134-
// This block needs to come after the nuget restore, because the nuget restore might fetch the .NET Core/Framework reference assemblies.
135-
if (options.ScanNetFrameworkDlls)
136-
{
137-
AddNetFrameworkDlls(dllPaths, frameworkLocations);
138-
AddAspNetCoreFrameworkDlls(dllPaths, frameworkLocations);
139-
AddMicrosoftWindowsDesktopDlls(dllPaths, frameworkLocations);
140-
}
88+
// This needs to come after the nuget restore, because the nuget restore might fetch the .NET Core/Framework reference assemblies.
89+
var frameworkLocations = AddFrameworkDlls(dllPaths);
14190

14291
assemblyCache = new AssemblyCache(dllPaths, frameworkLocations, progressMonitor);
143-
AnalyseSolutions(solutions);
92+
AnalyseSolutions(allSolutions);
14493

14594
foreach (var filename in assemblyCache.AllAssemblies.Select(a => a.Filename))
14695
{
@@ -182,6 +131,58 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
182131
DateTime.Now - startTime);
183132
}
184133

134+
private HashSet<string> AddFrameworkDlls(HashSet<string> dllPaths)
135+
{
136+
var frameworkLocations = new HashSet<string>();
137+
138+
AddNetFrameworkDlls(dllPaths, frameworkLocations);
139+
AddAspNetCoreFrameworkDlls(dllPaths, frameworkLocations);
140+
AddMicrosoftWindowsDesktopDlls(dllPaths, frameworkLocations);
141+
142+
return frameworkLocations;
143+
}
144+
145+
private void RestoreNugetPackages(List<FileInfo> allNonBinaryFiles, IEnumerable<string> allProjects, IEnumerable<string> allSolutions, HashSet<string> dllPaths)
146+
{
147+
try
148+
{
149+
var nuget = new NugetPackages(sourceDir.FullName, legacyPackageDirectory, progressMonitor);
150+
nuget.InstallPackages();
151+
152+
var nugetPackageDlls = legacyPackageDirectory.DirInfo.GetFiles("*.dll", new EnumerationOptions { RecurseSubdirectories = true });
153+
var nugetPackageDllPaths = nugetPackageDlls.Select(f => f.FullName).ToHashSet();
154+
var excludedPaths = nugetPackageDllPaths
155+
.Where(path => IsPathInSubfolder(path, legacyPackageDirectory.DirInfo.FullName, "tools"));
156+
157+
foreach (var excludedPath in excludedPaths)
158+
{
159+
progressMonitor.LogInfo($"Excluded Nuget DLL: {excludedPath}");
160+
}
161+
162+
nugetPackageDllPaths.ExceptWith(excludedPaths);
163+
dllPaths.UnionWith(nugetPackageDllPaths);
164+
}
165+
catch (FileNotFoundException)
166+
{
167+
progressMonitor.MissingNuGet();
168+
}
169+
170+
var restoredProjects = RestoreSolutions(allSolutions, out var assets1);
171+
var projects = allProjects.Except(restoredProjects);
172+
RestoreProjects(projects, out var assets2);
173+
174+
var dependencies = Assets.GetCompilationDependencies(progressMonitor, assets1.Union(assets2));
175+
176+
var paths = dependencies
177+
.Paths
178+
.Select(d => Path.Combine(packageDirectory.DirInfo.FullName, d))
179+
.ToList();
180+
dllPaths.UnionWith(paths);
181+
182+
LogAllUnusedPackages(dependencies);
183+
DownloadMissingPackages(allNonBinaryFiles, dllPaths);
184+
}
185+
185186
private static bool IsPathInSubfolder(string path, string rootFolder, string subFolder)
186187
{
187188
return path.IndexOf(
@@ -192,11 +193,6 @@ private static bool IsPathInSubfolder(string path, string rootFolder, string sub
192193

193194
private void RemoveNugetAnalyzerReferences()
194195
{
195-
if (!options.UseNuGet)
196-
{
197-
return;
198-
}
199-
200196
var packageFolder = packageDirectory.DirInfo.FullName.ToLowerInvariant();
201197
if (packageFolder == null)
202198
{
@@ -279,11 +275,7 @@ private void AddNetFrameworkDlls(ISet<string> dllPaths, ISet<string> frameworkLo
279275

280276
string? runtimeLocation = null;
281277

282-
if (options.UseSelfContainedDotnet)
283-
{
284-
runtimeLocation = Runtime.ExecutingRuntime;
285-
}
286-
else if (fileContent.IsNewProjectStructureUsed)
278+
if (fileContent.IsNewProjectStructureUsed)
287279
{
288280
runtimeLocation = Runtime.NetCoreRuntime;
289281
}
@@ -301,11 +293,6 @@ private void AddNetFrameworkDlls(ISet<string> dllPaths, ISet<string> frameworkLo
301293

302294
private void RemoveNugetPackageReference(string packagePrefix, ISet<string> dllPaths)
303295
{
304-
if (!options.UseNuGet)
305-
{
306-
return;
307-
}
308-
309296
var packageFolder = packageDirectory.DirInfo.FullName.ToLowerInvariant();
310297
if (packageFolder == null)
311298
{
@@ -353,11 +340,6 @@ private void AddMicrosoftWindowsDesktopDlls(ISet<string> dllPaths, ISet<string>
353340

354341
private string? GetPackageDirectory(string packagePrefix)
355342
{
356-
if (!options.UseNuGet)
357-
{
358-
return null;
359-
}
360-
361343
return new DirectoryInfo(packageDirectory.DirInfo.FullName)
362344
.EnumerateDirectories(packagePrefix + "*", new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive, RecurseSubdirectories = false })
363345
.FirstOrDefault()?
@@ -366,11 +348,6 @@ private void AddMicrosoftWindowsDesktopDlls(ISet<string> dllPaths, ISet<string>
366348

367349
private IEnumerable<string> GetAllPackageDirectories()
368350
{
369-
if (!options.UseNuGet)
370-
{
371-
return Enumerable.Empty<string>();
372-
}
373-
374351
return new DirectoryInfo(packageDirectory.DirInfo.FullName)
375352
.EnumerateDirectories("*", new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive, RecurseSubdirectories = false })
376353
.Select(d => d.Name);
@@ -455,14 +432,14 @@ private void GenerateSourceFilesFromWebViews(List<FileInfo> allFiles)
455432

456433
private IEnumerable<FileInfo> GetAllFiles()
457434
{
458-
var files = sourceDir.GetFiles("*.*", new EnumerationOptions { RecurseSubdirectories = true })
459-
.Where(d => !options.ExcludesFile(d.FullName));
435+
IEnumerable<FileInfo> files = sourceDir.GetFiles("*.*", new EnumerationOptions { RecurseSubdirectories = true });
460436

461437
if (options.DotNetPath != null)
462438
{
463439
files = files.Where(f => !f.FullName.StartsWith(options.DotNetPath, StringComparison.OrdinalIgnoreCase));
464440
}
465441

442+
files = new FilePathFilter(sourceDir, progressMonitor).Filter(files);
466443
return files;
467444
}
468445

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

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,6 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
1010
/// </summary>
1111
public interface IDependencyOptions
1212
{
13-
/// <summary>
14-
/// Directories to search DLLs in.
15-
/// </summary>
16-
IList<string> DllDirs { get; }
17-
18-
/// <summary>
19-
/// Files/patterns to exclude.
20-
/// </summary>
21-
IList<string> Excludes { get; }
22-
23-
/// <summary>
24-
/// Whether to analyse NuGet packages.
25-
/// </summary>
26-
bool UseNuGet { get; }
27-
28-
/// <summary>
29-
/// The solution file to analyse, or null if not specified.
30-
/// </summary>
31-
string? SolutionFile { get; }
32-
33-
/// <summary>
34-
/// Whether to use the packaged dotnet runtime.
35-
/// </summary>
36-
bool UseSelfContainedDotnet { get; }
37-
38-
/// <summary>
39-
/// Whether to search the .Net framework directory.
40-
/// </summary>
41-
bool ScanNetFrameworkDlls { get; }
42-
43-
/// <summary>
44-
/// Determine whether the given path should be excluded.
45-
/// </summary>
46-
/// <param name="path">The path to query.</param>
47-
/// <returns>True iff the path matches an exclusion.</returns>
48-
bool ExcludesFile(string path);
49-
5013
/// <summary>
5114
/// The number of threads to use.
5215
/// </summary>
@@ -62,21 +25,6 @@ public class DependencyOptions : IDependencyOptions
6225
{
6326
public static IDependencyOptions Default => new DependencyOptions();
6427

65-
public IList<string> DllDirs { get; set; } = new List<string>();
66-
67-
public IList<string> Excludes { get; set; } = new List<string>();
68-
69-
public bool UseNuGet { get; set; } = true;
70-
71-
public string? SolutionFile { get; set; }
72-
73-
public bool UseSelfContainedDotnet { get; set; } = false;
74-
75-
public bool ScanNetFrameworkDlls { get; set; } = true;
76-
77-
public bool ExcludesFile(string path) =>
78-
Excludes.Any(path.Contains);
79-
8028
public int Threads { get; set; } = EnvironmentVariables.GetDefaultNumberOfThreads();
8129

8230
public string? DotNetPath { get; set; } = null;

0 commit comments

Comments
 (0)