Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" PrivateAssets="All" />
<PackageReference Include="System.CommandLine" />
<PackageReference Include="System.Collections.Immutable" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Microsoft.IO.Redist" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
Expand Down
23 changes: 11 additions & 12 deletions src/Workspaces/MSBuild/BuildHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.CommandLine;
using System.Globalization;
using System.IO.Pipes;
using System.Threading.Tasks;
Expand All @@ -13,28 +11,27 @@ namespace Microsoft.CodeAnalysis.MSBuild;

internal static class Program
{
internal static async Task Main(string[] args)
internal static async Task<int> Main(string[] args)
{
// Note: we should limit the data passed through via command line strings, and pass information through IBuildHost.ConfigureGlobalState whenever possible.
// This is because otherwise we might run into escaping issues, or command line length limits.

var pipeOption = new Option<string>("--pipe") { Required = true };
var localeOption = new Option<string>("--locale") { Required = true };
var command = new RootCommand { pipeOption, localeOption };
var parsedArguments = command.Parse(args);
var pipeName = parsedArguments.GetValue(pipeOption)!;
var locale = parsedArguments.GetValue(localeOption)!;

var logger = new BuildHostLogger(Console.Error);

if (args is not [var pipeName, var cultureName])
{
logger.LogCritical($"Expected arguments: <pipe name> <culture>");
return -1;
}

try
{
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(locale);
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(cultureName);
}
catch (CultureNotFoundException)
{
// We couldn't find the culture, log a warning and fallback to the OS configured value.
logger.LogWarning($"Culture {locale} was not found, falling back to OS culture");
logger.LogWarning($"Culture '{cultureName}' was not found, falling back to OS culture");
}

logger.LogInformation($"BuildHost Runtime Version: {System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription}");
Expand All @@ -50,5 +47,7 @@ internal static async Task Main(string[] args)
await server.RunAsync().ConfigureAwait(false);

logger.LogInformation("RPC channel closed; process exiting.");

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,7 @@ private static string GetBuildHostPath(string contentFolderName, string assembly

private static void AppendBuildHostCommandLineArgumentsAndConfigureProcess(ProcessStartInfo processStartInfo, string pipeName)
{
AddArgument(processStartInfo, "--pipe");
AddArgument(processStartInfo, pipeName);

AddArgument(processStartInfo, "--locale");
AddArgument(processStartInfo, System.Globalization.CultureInfo.CurrentUICulture.Name);

// MSBUILD_EXE_PATH is read by MSBuild to find related tasks and targets. We don't want this to be inherited by our build process, or otherwise
Expand Down
27 changes: 15 additions & 12 deletions src/Workspaces/MSBuild/Test/BuildHostProcessManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Moq;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -63,14 +64,15 @@ internal void ProcessStartInfo_PassesPipeName(BuildHostProcessKind buildHostKind
const string PipeName = "TestPipe";

var processStartInfo = BuildHostProcessManager.CreateBuildHostStartInfo(buildHostKind, PipeName, dotnetPath: null);

#if NET
var binlogIndex = processStartInfo.ArgumentList.IndexOf("--pipe");
Assert.True(binlogIndex >= 0);
Assert.Equal(PipeName, processStartInfo.ArgumentList[binlogIndex + 1]);
var args = processStartInfo.ArgumentList;
#else
Assert.Contains($"--pipe {PipeName}", processStartInfo.Arguments);
var args = processStartInfo.Arguments.Split(' ');
#endif
Assert.True(args.Count() >= 2, $"Expected at least 2 args: '{string.Join(",", args)}'");

Assert.Equal(PipeName, args[^2]);
Assert.Equal(System.Globalization.CultureInfo.CurrentUICulture.Name, args[^1]);
}

[Theory]
Expand All @@ -80,16 +82,17 @@ internal void ProcessStartInfo_PassesPipeName(BuildHostProcessKind buildHostKind
[UseCulture("de-DE", "de-DE")]
internal void ProcessStartInfo_PassesLocale(BuildHostProcessKind buildHostKind)
{
const string Locale = "de-DE";

var processStartInfo = BuildHostProcessManager.CreateBuildHostStartInfo(buildHostKind, pipeName: "", dotnetPath: null);
const string PipeName = "TestPipe";

var processStartInfo = BuildHostProcessManager.CreateBuildHostStartInfo(buildHostKind, PipeName, dotnetPath: null);
#if NET
var localeIndex = processStartInfo.ArgumentList.IndexOf("--locale");
Assert.True(localeIndex >= 0);
Assert.Equal(Locale, processStartInfo.ArgumentList[localeIndex + 1]);
var args = processStartInfo.ArgumentList;
#else
Assert.Contains($"--locale {Locale}", processStartInfo.Arguments);
var args = processStartInfo.Arguments.Split(' ');
#endif
Assert.True(args.Count() >= 2, $"Expected at least 2 args: '{string.Join(",", args)}'");

Assert.Equal(PipeName, args[^2]);
Assert.Equal("de-DE", args[^1]);
}
}
Loading