diff --git a/csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs b/csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs index a29079d205b..a396ab751ea 100644 --- a/csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs +++ b/csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs @@ -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 - { - { "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 diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs index 761062bd6af..7bc79238415 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs @@ -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 MinimalEnvironment => IDotNetCliInvoker.MinimalEnvironment; + /// /// Returns a script for downloading relevant versions of the /// .NET SDK. The SDK(s) will be installed at installDir @@ -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; diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs index e3efce09481..45f69a1fdfc 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs @@ -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) diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNetCliInvoker.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNetCliInvoker.cs index 89631ffa861..6e80e1cb2bf 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNetCliInvoker.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNetCliInvoker.cs @@ -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 /// string Exec { get; } + /// + /// A minimal environment for running the .NET CLI. + /// + /// The .NET CLI language is set to English to avoid localized output. + /// + static ReadOnlyDictionary MinimalEnvironment { get; } = new(new Dictionary + { + {"DOTNET_CLI_UI_LANGUAGE", "en"}, + {"MSBUILDDISABLENODEREUSE", "1"}, + {"DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "true"} + }); + /// /// Execute `dotnet ` 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`.