Skip to content

Commit ccbc6f4

Browse files
committed
Use git ls-files to find DLLs to index
1 parent 5337785 commit ccbc6f4

File tree

5 files changed

+86
-12
lines changed

5 files changed

+86
-12
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public void TestVcVarsAllBatFiles()
557557
[Fact]
558558
public void TestLinuxBuildlessExtractionSuccess()
559559
{
560-
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone --references:."] = 0;
560+
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone"] = 0;
561561
actions.FileExists["csharp.log"] = true;
562562
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_TRAP_DIR"] = "";
563563
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_SOURCE_ARCHIVE_DIR"] = "";
@@ -571,7 +571,7 @@ public void TestLinuxBuildlessExtractionSuccess()
571571
[Fact]
572572
public void TestLinuxBuildlessExtractionFailed()
573573
{
574-
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone --references:."] = 10;
574+
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone"] = 10;
575575
actions.FileExists["csharp.log"] = true;
576576
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_TRAP_DIR"] = "";
577577
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_SOURCE_ARCHIVE_DIR"] = "";
@@ -585,7 +585,7 @@ public void TestLinuxBuildlessExtractionFailed()
585585
[Fact]
586586
public void TestLinuxBuildlessExtractionSolution()
587587
{
588-
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone foo.sln --references:."] = 0;
588+
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone foo.sln"] = 0;
589589
actions.FileExists["csharp.log"] = true;
590590
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_TRAP_DIR"] = "";
591591
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_SOURCE_ARCHIVE_DIR"] = "";
@@ -873,7 +873,7 @@ public void TestSkipNugetMsBuild()
873873
[Fact]
874874
public void TestSkipNugetBuildless()
875875
{
876-
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone foo.sln --references:. --skip-nuget"] = 0;
876+
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone foo.sln --skip-nuget"] = 0;
877877
actions.FileExists["csharp.log"] = true;
878878
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_TRAP_DIR"] = "";
879879
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_SOURCE_ARCHIVE_DIR"] = "";

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ BuildScript GetCommand(string? solution)
3535
if (solution is not null)
3636
cmd.QuoteArgument(solution);
3737

38-
cmd.Argument("--references:.");
39-
4038
if (!builder.Options.NugetRestore)
4139
{
4240
cmd.Argument("--skip-nuget");

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,24 @@ internal class AssemblyCache
1515
/// <summary>
1616
/// Locate all reference files and index them.
1717
/// </summary>
18-
/// <param name="dirs">Directories to search.</param>
18+
/// <param name="paths">
19+
/// Paths to search. Directories are searched recursively. Files are added directly to the
20+
/// assembly cache.
21+
/// </param>
1922
/// <param name="progressMonitor">Callback for progress.</param>
20-
public AssemblyCache(IEnumerable<string> dirs, ProgressMonitor progressMonitor)
23+
public AssemblyCache(IEnumerable<string> paths, ProgressMonitor progressMonitor)
2124
{
22-
foreach (var dir in dirs)
25+
foreach (var path in paths)
2326
{
24-
progressMonitor.FindingFiles(dir);
25-
AddReferenceDirectory(dir);
27+
if (File.Exists(path))
28+
{
29+
pendingDllsToIndex.Enqueue(path);
30+
}
31+
else
32+
{
33+
progressMonitor.FindingFiles(path);
34+
AddReferenceDirectory(path);
35+
}
2636
}
2737
IndexReferences();
2838
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public sealed class DependencyManager : IDisposable
3131
private readonly FileContent fileContent;
3232
private readonly TemporaryDirectory packageDirectory;
3333
private TemporaryDirectory? razorWorkingDirectory;
34+
private readonly Git git;
3435

3536

3637
/// <summary>
@@ -68,7 +69,11 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
6869
? new[] { options.SolutionFile }
6970
: allFiles.SelectFileNamesByExtension(".sln");
7071

71-
var dllDirNames = options.DllDirs.Select(Path.GetFullPath).ToList();
72+
// If DLL reference paths are specified on the command-line, use those to discover
73+
// assemblies. Otherwise (the default), query the git CLI to determine which DLL files
74+
// are tracked as part of the repository.
75+
this.git = new Git(this.progressMonitor);
76+
var dllDirNames = options.DllDirs.Count == 0 ? this.git.ListFiles("*.dll") : options.DllDirs.Select(Path.GetFullPath).ToList();
7277

7378
// Find DLLs in the .Net / Asp.Net Framework
7479
if (options.ScanNetFrameworkDlls)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
5+
namespace Semmle.Extraction.CSharp.DependencyFetching
6+
{
7+
/// <summary>
8+
/// Utilities for querying information from the git CLI.
9+
/// </summary>
10+
internal class Git
11+
{
12+
private readonly ProgressMonitor progressMonitor;
13+
private const string git = "git";
14+
15+
public Git(ProgressMonitor progressMonitor)
16+
{
17+
this.progressMonitor = progressMonitor;
18+
}
19+
20+
/// <summary>
21+
/// Lists all files matching <paramref name="pattern"/> which are tracked in the
22+
/// current git repository.
23+
/// </summary>
24+
/// <param name="pattern">The file pattern.</param>
25+
/// <returns>A list of all tracked files which match <paramref name="pattern"/>.</returns>
26+
/// <exception cref="Exception"></exception>
27+
public List<string> ListFiles(string pattern)
28+
{
29+
var results = new List<string>();
30+
var args = string.Join(' ', "ls-files", $"\"{pattern}\"");
31+
32+
progressMonitor.RunningProcess($"{git} {args}");
33+
var pi = new ProcessStartInfo(git, args)
34+
{
35+
UseShellExecute = false,
36+
RedirectStandardOutput = true
37+
};
38+
39+
using var p = new Process() { StartInfo = pi };
40+
p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
41+
{
42+
if (!string.IsNullOrWhiteSpace(e.Data))
43+
{
44+
results.Add(e.Data);
45+
}
46+
});
47+
p.Start();
48+
p.BeginOutputReadLine();
49+
p.WaitForExit();
50+
51+
if (p.ExitCode != 0)
52+
{
53+
progressMonitor.CommandFailed(git, args, p.ExitCode);
54+
throw new Exception($"{git} {args} failed");
55+
}
56+
57+
return results;
58+
}
59+
60+
}
61+
}

0 commit comments

Comments
 (0)