Skip to content

Commit 192f254

Browse files
authored
Merge branch 'main' into 20823-globalVarRef-document-defaultView
2 parents 6df789d + b1ed72d commit 192f254

File tree

31 files changed

+407
-267
lines changed

31 files changed

+407
-267
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ public static BuildScript WithDotNet(IAutobuilder<AutobuildOptionsShared> builde
8484
var temp = FileUtils.GetTemporaryWorkingDirectory(builder.Actions.GetEnvironmentVariable, builder.Options.Language.UpperCaseName, out var shouldCleanUp);
8585
return DotNet.WithDotNet(builder.Actions, builder.Logger, builder.Paths.Select(x => x.Item1), temp, shouldCleanUp, ensureDotNetAvailable, builder.Options.DotNetVersion, installDir =>
8686
{
87-
var env = new Dictionary<string, string>
88-
{
89-
{ "DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "true" },
90-
{ "MSBUILDDISABLENODEREUSE", "1" }
91-
};
87+
var env = DotNet.MinimalEnvironment.ToDictionary();
9288
if (installDir is not null)
9389
{
9490
// The installation succeeded, so use the newly installed .NET

csharp/codeql-extractor.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,8 @@ options:
7474
[EXPERIMENTAL] The value is a path to the MsBuild binary log file that should be extracted.
7575
This option only works when `--build-mode none` is also specified.
7676
type: array
77+
buildless_dependency_dir:
78+
title: The path where buildless (standalone) extraction should keep dependencies.
79+
description: >
80+
If set, the buildless (standalone) extractor will store dependencies in this directory.
81+
type: string
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.IO;
3+
using Semmle.Util;
4+
using Semmle.Util.Logging;
5+
6+
namespace Semmle.Extraction.CSharp.DependencyFetching
7+
{
8+
/// <summary>
9+
/// A directory used for storing fetched dependencies.
10+
/// When a specific directory is set via the dependency directory extractor option,
11+
/// we store dependencies in that directory for caching purposes.
12+
/// Otherwise, we create a temporary directory that is deleted upon disposal.
13+
/// </summary>
14+
public sealed class DependencyDirectory : IDisposable
15+
{
16+
private readonly string userReportedDirectoryPurpose;
17+
private readonly ILogger logger;
18+
private readonly bool attemptCleanup;
19+
20+
public DirectoryInfo DirInfo { get; }
21+
22+
public DependencyDirectory(string subfolderName, string userReportedDirectoryPurpose, ILogger logger)
23+
{
24+
this.logger = logger;
25+
this.userReportedDirectoryPurpose = userReportedDirectoryPurpose;
26+
27+
string path;
28+
if (EnvironmentVariables.GetBuildlessDependencyDir() is string dir)
29+
{
30+
path = dir;
31+
attemptCleanup = false;
32+
}
33+
else
34+
{
35+
path = FileUtils.GetTemporaryWorkingDirectory(out _);
36+
attemptCleanup = true;
37+
}
38+
DirInfo = new DirectoryInfo(Path.Join(path, subfolderName));
39+
DirInfo.Create();
40+
}
41+
42+
public void Dispose()
43+
{
44+
if (!attemptCleanup)
45+
{
46+
logger.LogInfo($"Keeping {userReportedDirectoryPurpose} directory {DirInfo.FullName} for possible caching purposes.");
47+
return;
48+
}
49+
50+
try
51+
{
52+
DirInfo.Delete(true);
53+
}
54+
catch (Exception exc)
55+
{
56+
logger.LogInfo($"Couldn't delete {userReportedDirectoryPurpose} directory {exc.Message}");
57+
}
58+
}
59+
60+
public override string ToString() => DirInfo.FullName;
61+
}
62+
}

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34
using System.IO;
45
using System.Linq;
56
using Newtonsoft.Json.Linq;
@@ -140,6 +141,8 @@ public IList<string> GetNugetFeedsFromFolder(string folderPath)
140141
// The version number should be kept in sync with the version .NET version used for building the application.
141142
public const string LatestDotNetSdkVersion = "9.0.300";
142143

144+
public static ReadOnlyDictionary<string, string> MinimalEnvironment => IDotNetCliInvoker.MinimalEnvironment;
145+
143146
/// <summary>
144147
/// Returns a script for downloading relevant versions of the
145148
/// .NET SDK. The SDK(s) will be installed at <code>installDir</code>
@@ -254,7 +257,6 @@ BuildScript GetInstall(string pwsh) =>
254257
else
255258
{
256259
var dotnetInstallPath = actions.PathCombine(tempWorkingDirectory, ".dotnet", "dotnet-install.sh");
257-
258260
var downloadDotNetInstallSh = BuildScript.DownloadFile(
259261
"https://dot.net/v1/dotnet-install.sh",
260262
dotnetInstallPath,
@@ -269,17 +271,28 @@ BuildScript GetInstall(string pwsh) =>
269271
prelude = downloadDotNetInstallSh & chmod.Script;
270272
postlude = shouldCleanUp ? BuildScript.DeleteFile(dotnetInstallPath) : BuildScript.Success;
271273

272-
getInstall = version => new CommandBuilder(actions).
273-
RunCommand(dotnetInstallPath).
274-
Argument("--channel").
275-
Argument("release").
276-
Argument("--version").
277-
Argument(version).
278-
Argument("--install-dir").
279-
Argument(path).Script;
274+
getInstall = version =>
275+
{
276+
var cb = new CommandBuilder(actions).
277+
RunCommand(dotnetInstallPath).
278+
Argument("--channel").
279+
Argument("release").
280+
Argument("--version").
281+
Argument(version);
282+
283+
// Request ARM64 architecture on Apple Silicon machines
284+
if (actions.IsRunningOnAppleSilicon())
285+
{
286+
cb.Argument("--architecture").
287+
Argument("arm64");
288+
}
289+
290+
return cb.Argument("--install-dir").
291+
Argument(path).Script;
292+
};
280293
}
281294

282-
var dotnetInfo = new CommandBuilder(actions).
295+
var dotnetInfo = new CommandBuilder(actions, environment: MinimalEnvironment).
283296
RunCommand(actions.PathCombine(path, "dotnet")).
284297
Argument("--info").Script;
285298

@@ -311,7 +324,7 @@ BuildScript GetInstall(string pwsh) =>
311324

312325
private static BuildScript GetInstalledSdksScript(IBuildActions actions)
313326
{
314-
var listSdks = new CommandBuilder(actions, silent: true).
327+
var listSdks = new CommandBuilder(actions, silent: true, environment: MinimalEnvironment).
315328
RunCommand("dotnet").
316329
Argument("--list-sdks");
317330
return listSdks.Script;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34
using System.Diagnostics;
45
using Semmle.Util;
56
using Semmle.Util.Logging;
@@ -36,10 +37,12 @@ private ProcessStartInfo MakeDotnetStartInfo(string args, string? workingDirecto
3637
{
3738
startInfo.WorkingDirectory = workingDirectory;
3839
}
39-
// Set the .NET CLI language to English to avoid localized output.
40-
startInfo.EnvironmentVariables["DOTNET_CLI_UI_LANGUAGE"] = "en";
41-
startInfo.EnvironmentVariables["MSBUILDDISABLENODEREUSE"] = "1";
42-
startInfo.EnvironmentVariables["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "true";
40+
41+
// Set minimal environment variables.
42+
foreach (var kvp in IDotNetCliInvoker.MinimalEnvironment)
43+
{
44+
startInfo.EnvironmentVariables[kvp.Key] = kvp.Value;
45+
}
4346

4447
// Configure the proxy settings, if applicable.
4548
if (this.proxy != null)

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
23

34
namespace Semmle.Extraction.CSharp.DependencyFetching
45
{
@@ -9,6 +10,20 @@ internal interface IDotNetCliInvoker
910
/// </summary>
1011
string Exec { get; }
1112

13+
/// <summary>
14+
/// A minimal environment for running the .NET CLI.
15+
///
16+
/// DOTNET_CLI_UI_LANGUAGE: The .NET CLI language is set to English to avoid localized output.
17+
/// MSBUILDDISABLENODEREUSE: To ensure clean environment for each build.
18+
/// DOTNET_SKIP_FIRST_TIME_EXPERIENCE: To skip first time experience messages.
19+
/// </summary>
20+
static ReadOnlyDictionary<string, string> MinimalEnvironment { get; } = new(new Dictionary<string, string>
21+
{
22+
{"DOTNET_CLI_UI_LANGUAGE", "en"},
23+
{"MSBUILDDISABLENODEREUSE", "1"},
24+
{"DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "true"}
25+
});
26+
1227
/// <summary>
1328
/// Execute `dotnet <paramref name="args"/>` and return true if the command succeeded, otherwise false.
1429
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ internal class NugetExeWrapper : IDisposable
2424
private readonly FileProvider fileProvider;
2525

2626
/// <summary>
27-
/// The computed packages directory.
28-
/// This will be in the Temp location
27+
/// The packages directory.
28+
/// This will be in the user-specified or computed Temp location
2929
/// so as to not trample the source tree.
3030
/// </summary>
31-
private readonly TemporaryDirectory packageDirectory;
31+
private readonly DependencyDirectory packageDirectory;
3232

3333
/// <summary>
3434
/// Create the package manager for a specified source tree.
3535
/// </summary>
36-
public NugetExeWrapper(FileProvider fileProvider, TemporaryDirectory packageDirectory, Semmle.Util.Logging.ILogger logger)
36+
public NugetExeWrapper(FileProvider fileProvider, DependencyDirectory packageDirectory, Semmle.Util.Logging.ILogger logger)
3737
{
3838
this.fileProvider = fileProvider;
3939
this.packageDirectory = packageDirectory;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ internal sealed partial class NugetPackageRestorer : IDisposable
2424
private readonly IDotNet dotnet;
2525
private readonly DependabotProxy? dependabotProxy;
2626
private readonly IDiagnosticsWriter diagnosticsWriter;
27-
private readonly TemporaryDirectory legacyPackageDirectory;
28-
private readonly TemporaryDirectory missingPackageDirectory;
27+
private readonly DependencyDirectory legacyPackageDirectory;
28+
private readonly DependencyDirectory missingPackageDirectory;
2929
private readonly ILogger logger;
3030
private readonly ICompilationInfoContainer compilationInfoContainer;
3131

32-
public TemporaryDirectory PackageDirectory { get; }
32+
public DependencyDirectory PackageDirectory { get; }
3333

3434
public NugetPackageRestorer(
3535
FileProvider fileProvider,
@@ -48,9 +48,9 @@ public NugetPackageRestorer(
4848
this.logger = logger;
4949
this.compilationInfoContainer = compilationInfoContainer;
5050

51-
PackageDirectory = new TemporaryDirectory(ComputeTempDirectoryPath("packages"), "package", logger);
52-
legacyPackageDirectory = new TemporaryDirectory(ComputeTempDirectoryPath("legacypackages"), "legacy package", logger);
53-
missingPackageDirectory = new TemporaryDirectory(ComputeTempDirectoryPath("missingpackages"), "missing package", logger);
51+
PackageDirectory = new DependencyDirectory("packages", "package", logger);
52+
legacyPackageDirectory = new DependencyDirectory("legacypackages", "legacy package", logger);
53+
missingPackageDirectory = new DependencyDirectory("missingpackages", "missing package", logger);
5454
}
5555

5656
public string? TryRestore(string package)

csharp/extractor/Semmle.Util/EnvironmentVariables.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,14 @@ public static IEnumerable<string> GetURLs(string name)
7676
{
7777
return Environment.GetEnvironmentVariable("CODEQL_EXTRACTOR_CSHARP_OVERLAY_BASE_METADATA_OUT");
7878
}
79+
80+
/// <summary>
81+
/// If set, returns the directory where buildless dependencies should be stored.
82+
/// This can be used for caching dependencies.
83+
/// </summary>
84+
public static string? GetBuildlessDependencyDir()
85+
{
86+
return Environment.GetEnvironmentVariable("CODEQL_EXTRACTOR_CSHARP_OPTION_BUILDLESS_DEPENDENCY_DIR");
87+
}
7988
}
8089
}

csharp/ql/integration-tests/linux/dotnet_10_rc2/Program.cs renamed to csharp/ql/integration-tests/all-platforms/dotnet_10/Program.cs

File renamed without changes.

0 commit comments

Comments
 (0)