Skip to content

Commit 3563572

Browse files
committed
C#: Add proper .NET environment for dotnet info and list-sdks and streamline the minimal dotnet environment.
1 parent 1230e24 commit 3563572

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
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: 5 additions & 2 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>
@@ -289,7 +292,7 @@ BuildScript GetInstall(string pwsh) =>
289292
};
290293
}
291294

292-
var dotnetInfo = new CommandBuilder(actions).
295+
var dotnetInfo = new CommandBuilder(actions, environment: MinimalEnvironment).
293296
RunCommand(actions.PathCombine(path, "dotnet")).
294297
Argument("--info").Script;
295298

@@ -321,7 +324,7 @@ BuildScript GetInstall(string pwsh) =>
321324

322325
private static BuildScript GetInstalledSdksScript(IBuildActions actions)
323326
{
324-
var listSdks = new CommandBuilder(actions, silent: true).
327+
var listSdks = new CommandBuilder(actions, silent: true, environment: MinimalEnvironment).
325328
RunCommand("dotnet").
326329
Argument("--list-sdks");
327330
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: 13 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,18 @@ internal interface IDotNetCliInvoker
910
/// </summary>
1011
string Exec { get; }
1112

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

0 commit comments

Comments
 (0)