Skip to content

Commit f8b29ad

Browse files
committed
Introduce environment variable to specify framework assembly locations
1 parent d358f8e commit f8b29ad

File tree

10 files changed

+69
-30
lines changed

10 files changed

+69
-30
lines changed

csharp/autobuilder/Semmle.Autobuild.Shared/AutobuildOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static string[] AsListWithExpandedEnvVars(this string? value, IBuildActio
7575
return defaultValue;
7676

7777
return value.
78-
Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).
78+
Split(FileUtils.NewLineCharacters, StringSplitOptions.RemoveEmptyEntries).
7979
Select(s => AsStringWithExpandedEnvVars(s, actions)).ToArray();
8080
}
8181

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,49 @@ private HashSet<string> AddFrameworkDlls(HashSet<string> dllPaths)
158158
{
159159
var frameworkLocations = new HashSet<string>();
160160

161+
var frameworkReferences = Environment.GetEnvironmentVariable(EnvironmentVariableNames.DotnetFrameworkReferences);
162+
var frameworkReferencesUseSubfolders = Environment.GetEnvironmentVariable(EnvironmentVariableNames.DotnetFrameworkReferencesUseSubfolders);
163+
_ = bool.TryParse(frameworkReferencesUseSubfolders, out var useSubfolders);
164+
if (!string.IsNullOrWhiteSpace(frameworkReferences))
165+
{
166+
var frameworkPaths = frameworkReferences.Split(FileUtils.NewLineCharacters, StringSplitOptions.RemoveEmptyEntries);
167+
168+
foreach (var path in frameworkPaths)
169+
{
170+
if (!Directory.Exists(path))
171+
{
172+
logger.LogError($"Specified framework reference path '{path}' does not exist.");
173+
continue;
174+
}
175+
176+
if (useSubfolders)
177+
{
178+
dllPaths.Add(path);
179+
frameworkLocations.Add(path);
180+
continue;
181+
}
182+
183+
try
184+
{
185+
var dlls = Directory.GetFiles(path, "*.dll", new EnumerationOptions { RecurseSubdirectories = false, MatchCasing = MatchCasing.CaseInsensitive });
186+
if (dlls.Length == 0)
187+
{
188+
logger.LogError($"No DLLs found in specified framework reference path '{path}'.");
189+
continue;
190+
}
191+
192+
dllPaths.UnionWith(dlls);
193+
frameworkLocations.UnionWith(dlls);
194+
}
195+
catch (Exception e)
196+
{
197+
logger.LogError($"Error while searching for DLLs in '{path}': {e.Message}");
198+
}
199+
}
200+
201+
return frameworkLocations;
202+
}
203+
161204
AddNetFrameworkDlls(dllPaths, frameworkLocations);
162205
AddAspNetCoreFrameworkDlls(dllPaths, frameworkLocations);
163206
AddMicrosoftWindowsDesktopDlls(dllPaths, frameworkLocations);

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
22
{
33
internal class EnvironmentVariableNames
44
{
5+
/// <summary>
6+
/// Controls whether to generate source files from Asp.Net Core views (`.cshtml`, `.razor`).
7+
/// </summary>
58
public const string WebViewGeneration = "CODEQL_EXTRACTOR_CSHARP_BUILDLESS_EXTRACT_WEB_VIEWS";
6-
public const string MonoPath = "CODEQL_EXTRACTOR_CSHARP_BUILDLESS_MONO_PATH";
9+
10+
/// <summary>
11+
/// Specifies the location of .Net framework references added to the compilation.
12+
/// </summary>
13+
public const string DotnetFrameworkReferences = "CODEQL_EXTRACTOR_CSHARP_BUILDLESS_DOTNET_FRAMEWORK_REFERENCES";
14+
15+
/// <summary>
16+
/// Controls whether to use framework dependencies from subfolders.
17+
/// </summary>
18+
public const string DotnetFrameworkReferencesUseSubfolders = "CODEQL_EXTRACTOR_CSHARP_BUILDLESS_DOTNET_FRAMEWORK_REFERENCES_USE_SUBFOLDERS";
719
}
820
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private record class PathFilter(Regex Regex, bool Include);
3131

3232
public IEnumerable<FileInfo> Filter(IEnumerable<FileInfo> files)
3333
{
34-
var filters = (Environment.GetEnvironmentVariable("LGTM_INDEX_FILTERS") ?? string.Empty).Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
34+
var filters = (Environment.GetEnvironmentVariable("LGTM_INDEX_FILTERS") ?? string.Empty).Split(FileUtils.NewLineCharacters, StringSplitOptions.RemoveEmptyEntries);
3535
if (filters.Length == 0)
3636
{
3737
return files;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public IEnumerable<string> GenerateFiles(IEnumerable<string> cshtmls, IEnumerabl
7171
var args = new StringBuilder();
7272
args.Append($"/target:exe /generatedfilesout:\"{outputFolder}\" /out:\"{dllPath}\" /analyzerconfig:\"{analyzerConfig}\" ");
7373

74-
foreach (var f in Directory.GetFiles(sourceGeneratorFolder, "*.dll"))
74+
foreach (var f in Directory.GetFiles(sourceGeneratorFolder, "*.dll", new EnumerationOptions { RecurseSubdirectories = false, MatchCasing = MatchCasing.CaseInsensitive }))
7575
{
7676
args.Append($"/analyzer:\"{f}\" ");
7777
}

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,12 @@ private IEnumerable<string> DesktopRuntimes
7878
.OrderByDescending(Path.GetFileName);
7979
}
8080

81-
string? monoDir = null;
82-
83-
var monoPathEnv = Environment.GetEnvironmentVariable(EnvironmentVariableNames.MonoPath);
84-
if (monoPathEnv is not null)
85-
{
86-
if (Directory.Exists(monoPathEnv))
87-
{
88-
monoDir = monoPathEnv;
89-
}
90-
else
91-
{
92-
logger.LogError($"The directory specified in {EnvironmentVariableNames.MonoPath} does not exist: {monoPathEnv}");
93-
}
94-
}
95-
else
96-
{
97-
var monoPath = FileUtils.FindProgramOnPath(Win32.IsWindows() ? "mono.exe" : "mono");
98-
string[] monoDirs = monoPath is not null
99-
? [Path.GetFullPath(Path.Combine(monoPath, "..", "lib", "mono")), monoPath]
100-
: ["/usr/lib/mono", "/usr/local/mono", "/usr/local/bin/mono", @"C:\Program Files\Mono\lib\mono"];
101-
102-
monoDir = monoDirs.FirstOrDefault(Directory.Exists);
103-
}
81+
var monoPath = FileUtils.FindProgramOnPath(Win32.IsWindows() ? "mono.exe" : "mono");
82+
string[] monoDirs = monoPath is not null
83+
? [Path.GetFullPath(Path.Combine(monoPath, "..", "lib", "mono")), monoPath]
84+
: ["/usr/lib/mono", "/usr/local/mono", "/usr/local/bin/mono", @"C:\Program Files\Mono\lib\mono"];
10485

86+
var monoDir = monoDirs.FirstOrDefault(Directory.Exists);
10587
if (monoDir is not null)
10688
{
10789
return Directory.EnumerateDirectories(monoDir)

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ private static ExitCode AnalyseTracing(
386386

387387
if (compilerArguments.GeneratedFilesOutputDirectory is not null)
388388
{
389-
paths.AddRange(Directory.GetFiles(compilerArguments.GeneratedFilesOutputDirectory, "*.cs", SearchOption.AllDirectories));
389+
paths.AddRange(Directory.GetFiles(compilerArguments.GeneratedFilesOutputDirectory, "*.cs", new EnumerationOptions { RecurseSubdirectories = true, MatchCasing = MatchCasing.CaseInsensitive }));
390390
}
391391

392392
return ReadSyntaxTrees(

csharp/extractor/Semmle.Extraction/CsProjFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private static (string[] csFiles, string[] references, string[] projectReference
112112
.Where(s => s is not null)
113113
?? Enumerable.Empty<string>();
114114

115-
var additionalCsFiles = System.IO.Directory.GetFiles(directoryName, "*.cs", SearchOption.AllDirectories);
115+
var additionalCsFiles = System.IO.Directory.GetFiles(directoryName, "*.cs", new EnumerationOptions { RecurseSubdirectories = true, MatchCasing = MatchCasing.CaseInsensitive });
116116

117117
var projectReferences = root
118118
.SelectNodes("/Project/ItemGroup/ProjectReference/@Include", mgr)

csharp/extractor/Semmle.Util/FileUtils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public static class FileUtils
1313
{
1414
public const string NugetExeUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe";
1515

16+
public static readonly char[] NewLineCharacters = ['\r', '\n'];
17+
1618
public static string ConvertToWindows(string path)
1719
{
1820
return path.Replace('/', '\\');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from create_database_utils import *
22
import os
33

4-
os.environ["CODEQL_EXTRACTOR_CSHARP_BUILDLESS_MONO_PATH"] = "/non-existent-path"
4+
os.environ["CODEQL_EXTRACTOR_CSHARP_BUILDLESS_DOTNET_FRAMEWORK_REFERENCES"] = "/non-existent-path"
55
run_codeql_database_create([], lang="csharp", extra_args=["--extractor-option=buildless=true"])

0 commit comments

Comments
 (0)