From a82d79dc8dcd8ae92741eb7bcf787559629e8e0a Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Fri, 30 Aug 2024 15:47:40 +0200 Subject: [PATCH] Shutdown toolset compilers --- .../dotnet/BuildServer/VBCSCompilerServer.cs | 48 ++++++++++++++++--- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/Cli/dotnet/BuildServer/VBCSCompilerServer.cs b/src/Cli/dotnet/BuildServer/VBCSCompilerServer.cs index b324abf9122e..8684564e03db 100644 --- a/src/Cli/dotnet/BuildServer/VBCSCompilerServer.cs +++ b/src/Cli/dotnet/BuildServer/VBCSCompilerServer.cs @@ -3,12 +3,18 @@ using System.Reflection; using Microsoft.DotNet.Cli; +using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.CommandFactory; +using NuGet.Configuration; namespace Microsoft.DotNet.BuildServer { internal class VBCSCompilerServer : IBuildServer { + private static readonly string s_toolsetPackageName = "microsoft.net.sdk.compilers.toolset"; + private static readonly string s_vbcsCompilerExeFileName = "VBCSCompiler.exe"; + private static readonly string s_shutdownArg = "-shutdown"; + internal static readonly string VBCSCompilerPath = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Roslyn", @@ -28,18 +34,46 @@ public VBCSCompilerServer(ICommandFactory commandFactory = null) public void Shutdown() { - var command = _commandFactory - .Create("exec", new[] { VBCSCompilerPath, "-shutdown" }) - .CaptureStdOut() - .CaptureStdErr(); + List errors = null; + + // Shutdown the compiler from the SDK. + execute(_commandFactory.Create("exec", [VBCSCompilerPath, s_shutdownArg]), ref errors); + + // Shutdown toolset compilers. + var nuGetPackageRoot = SettingsUtility.GetGlobalPackagesFolder(Settings.LoadDefaultSettings(root: null)); + var toolsetPackageDirectory = Path.Join(nuGetPackageRoot, s_toolsetPackageName); + if (Directory.Exists(toolsetPackageDirectory)) + { + foreach (var versionDirectory in Directory.EnumerateDirectories(toolsetPackageDirectory)) + { + var vbcsCompilerPath = Path.Join(versionDirectory, s_vbcsCompilerExeFileName); + if (File.Exists(vbcsCompilerPath)) + { + execute(CommandFactoryUsingResolver.Create(vbcsCompilerPath, [s_shutdownArg]), ref errors); + } + } + } - var result = command.Execute(); - if (result.ExitCode != 0) + if (errors?.Count > 0) { throw new BuildServerException( string.Format( LocalizableStrings.ShutdownCommandFailed, - result.StdErr)); + string.Join(Environment.NewLine, errors))); + } + + static void execute(ICommand command, ref List errors) + { + command = command + .CaptureStdOut() + .CaptureStdErr(); + + var result = command.Execute(); + if (result.ExitCode != 0) + { + errors ??= new List(); + errors.Add(result.StdErr); + } } } }