C#: Add proper .NET environment for dotnet info and list-sdks and streamline the minimal dotnet environment.

This commit is contained in:
Michael Nebel
2025-11-18 16:31:09 +01:00
parent 0daee6fbf2
commit fba496f911
4 changed files with 26 additions and 11 deletions

View File

@@ -84,11 +84,7 @@ namespace Semmle.Autobuild.CSharp
var temp = FileUtils.GetTemporaryWorkingDirectory(builder.Actions.GetEnvironmentVariable, builder.Options.Language.UpperCaseName, out var shouldCleanUp);
return DotNet.WithDotNet(builder.Actions, builder.Logger, builder.Paths.Select(x => x.Item1), temp, shouldCleanUp, ensureDotNetAvailable, builder.Options.DotNetVersion, installDir =>
{
var env = new Dictionary<string, string>
{
{ "DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "true" },
{ "MSBUILDDISABLENODEREUSE", "1" }
};
var env = DotNet.MinimalEnvironment.ToDictionary();
if (installDir is not null)
{
// The installation succeeded, so use the newly installed .NET

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
@@ -140,6 +141,8 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
// The version number should be kept in sync with the version .NET version used for building the application.
public const string LatestDotNetSdkVersion = "9.0.300";
public static ReadOnlyDictionary<string, string> MinimalEnvironment => IDotNetCliInvoker.MinimalEnvironment;
/// <summary>
/// Returns a script for downloading relevant versions of the
/// .NET SDK. The SDK(s) will be installed at <code>installDir</code>
@@ -289,7 +292,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
};
}
var dotnetInfo = new CommandBuilder(actions).
var dotnetInfo = new CommandBuilder(actions, environment: MinimalEnvironment).
RunCommand(actions.PathCombine(path, "dotnet")).
Argument("--info").Script;
@@ -321,7 +324,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
private static BuildScript GetInstalledSdksScript(IBuildActions actions)
{
var listSdks = new CommandBuilder(actions, silent: true).
var listSdks = new CommandBuilder(actions, silent: true, environment: MinimalEnvironment).
RunCommand("dotnet").
Argument("--list-sdks");
return listSdks.Script;

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Semmle.Util;
using Semmle.Util.Logging;
@@ -36,10 +37,12 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
{
startInfo.WorkingDirectory = workingDirectory;
}
// Set the .NET CLI language to English to avoid localized output.
startInfo.EnvironmentVariables["DOTNET_CLI_UI_LANGUAGE"] = "en";
startInfo.EnvironmentVariables["MSBUILDDISABLENODEREUSE"] = "1";
startInfo.EnvironmentVariables["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "true";
// Set minimal environment variables.
foreach (var kvp in IDotNetCliInvoker.MinimalEnvironment)
{
startInfo.EnvironmentVariables[kvp.Key] = kvp.Value;
}
// Configure the proxy settings, if applicable.
if (this.proxy != null)

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Semmle.Extraction.CSharp.DependencyFetching
{
@@ -9,6 +10,18 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
/// </summary>
string Exec { get; }
/// <summary>
/// A minimal environment for running the .NET CLI.
///
/// The .NET CLI language is set to English to avoid localized output.
/// </summary>
static ReadOnlyDictionary<string, string> MinimalEnvironment { get; } = new(new Dictionary<string, string>
{
{"DOTNET_CLI_UI_LANGUAGE", "en"},
{"MSBUILDDISABLENODEREUSE", "1"},
{"DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "true"}
});
/// <summary>
/// Execute `dotnet <paramref name="args"/>` and return true if the command succeeded, otherwise false.
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.