Skip to content

Commit 795af9f

Browse files
authored
Shutdown toolset compilers (#43135)
1 parent 666958c commit 795af9f

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

src/Cli/dotnet/BuildServer/VBCSCompilerServer.cs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33

44
using System.Reflection;
55
using Microsoft.DotNet.Cli;
6+
using Microsoft.DotNet.Cli.Utils;
67
using Microsoft.DotNet.CommandFactory;
8+
using NuGet.Configuration;
79

810
namespace Microsoft.DotNet.BuildServer
911
{
1012
internal class VBCSCompilerServer : IBuildServer
1113
{
14+
private static readonly string s_toolsetPackageName = "microsoft.net.sdk.compilers.toolset";
15+
private static readonly string s_vbcsCompilerExeFileName = "VBCSCompiler.exe";
16+
private static readonly string s_shutdownArg = "-shutdown";
17+
1218
internal static readonly string VBCSCompilerPath = Path.Combine(
1319
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
1420
"Roslyn",
@@ -28,18 +34,46 @@ public VBCSCompilerServer(ICommandFactory commandFactory = null)
2834

2935
public void Shutdown()
3036
{
31-
var command = _commandFactory
32-
.Create("exec", new[] { VBCSCompilerPath, "-shutdown" })
33-
.CaptureStdOut()
34-
.CaptureStdErr();
37+
List<string> errors = null;
38+
39+
// Shutdown the compiler from the SDK.
40+
execute(_commandFactory.Create("exec", [VBCSCompilerPath, s_shutdownArg]), ref errors);
41+
42+
// Shutdown toolset compilers.
43+
var nuGetPackageRoot = SettingsUtility.GetGlobalPackagesFolder(Settings.LoadDefaultSettings(root: null));
44+
var toolsetPackageDirectory = Path.Join(nuGetPackageRoot, s_toolsetPackageName);
45+
if (Directory.Exists(toolsetPackageDirectory))
46+
{
47+
foreach (var versionDirectory in Directory.EnumerateDirectories(toolsetPackageDirectory))
48+
{
49+
var vbcsCompilerPath = Path.Join(versionDirectory, s_vbcsCompilerExeFileName);
50+
if (File.Exists(vbcsCompilerPath))
51+
{
52+
execute(CommandFactoryUsingResolver.Create(vbcsCompilerPath, [s_shutdownArg]), ref errors);
53+
}
54+
}
55+
}
3556

36-
var result = command.Execute();
37-
if (result.ExitCode != 0)
57+
if (errors?.Count > 0)
3858
{
3959
throw new BuildServerException(
4060
string.Format(
4161
LocalizableStrings.ShutdownCommandFailed,
42-
result.StdErr));
62+
string.Join(Environment.NewLine, errors)));
63+
}
64+
65+
static void execute(ICommand command, ref List<string> errors)
66+
{
67+
command = command
68+
.CaptureStdOut()
69+
.CaptureStdErr();
70+
71+
var result = command.Execute();
72+
if (result.ExitCode != 0)
73+
{
74+
errors ??= new List<string>();
75+
errors.Add(result.StdErr);
76+
}
4377
}
4478
}
4579
}

0 commit comments

Comments
 (0)