Skip to content

Commit 60826bd

Browse files
authored
Merge pull request #20837 from michaelnebel/csharp/dotnet10
C#: Improve the logic for downloading .NET and setting environment variables.
2 parents 638c98b + 5cdfb3c commit 60826bd

File tree

16 files changed

+255
-246
lines changed

16 files changed

+255
-246
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/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`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "10.0.100"
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def test1(codeql, csharp):
2+
codeql.database.create()
3+
4+
def test2(codeql, csharp):
5+
codeql.database.create(build_mode="none")

0 commit comments

Comments
 (0)