-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MSBuildForwardingAppWithoutLogging.cs
205 lines (172 loc) · 7.88 KB
/
MSBuildForwardingAppWithoutLogging.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#if NET
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace Microsoft.DotNet.Cli.Utils
{
internal class MSBuildForwardingAppWithoutLogging
{
private static readonly bool AlwaysExecuteMSBuildOutOfProc = Env.GetEnvironmentVariableAsBool("DOTNET_CLI_RUN_MSBUILD_OUTOFPROC");
private static readonly bool UseMSBUILDNOINPROCNODE = Env.GetEnvironmentVariableAsBool("DOTNET_CLI_USE_MSBUILDNOINPROCNODE");
private const string MSBuildExeName = "MSBuild.dll";
private const string SdksDirectoryName = "Sdks";
// Null if we're running MSBuild in-proc.
private ForwardingAppImplementation _forwardingApp;
// Command line arguments we're asked to forward to MSBuild.
private readonly IEnumerable<string> _argsToForward;
internal static string MSBuildExtensionsPathTestHook = null;
// Path to the MSBuild binary to use.
public string MSBuildPath { get; }
// True if, given current state of the class, MSBuild would be executed in its own process.
public bool ExecuteMSBuildOutOfProc => _forwardingApp != null;
private readonly Dictionary<string, string> _msbuildRequiredEnvironmentVariables =
new Dictionary<string, string>
{
{ "MSBuildExtensionsPath", MSBuildExtensionsPathTestHook ?? AppContext.BaseDirectory },
{ "MSBuildSDKsPath", GetMSBuildSDKsPath() },
{ "DOTNET_HOST_PATH", GetDotnetPath() },
};
private readonly IEnumerable<string> _msbuildRequiredParameters =
new List<string> { "-maxcpucount", "-verbosity:m" };
public MSBuildForwardingAppWithoutLogging(IEnumerable<string> argsToForward, string msbuildPath = null)
{
string defaultMSBuildPath = GetMSBuildExePath();
_argsToForward = argsToForward;
MSBuildPath = msbuildPath ?? defaultMSBuildPath;
if (UseMSBUILDNOINPROCNODE)
{
// Force MSBuild to use external working node long living process for building projects
// We also refers to this as MSBuild Server V1 as entry process forwards most of the work to it.
EnvironmentVariable("MSBUILDNOINPROCNODE", "1");
}
// If DOTNET_CLI_RUN_MSBUILD_OUTOFPROC is set or we're asked to execute a non-default binary, call MSBuild out-of-proc.
if (AlwaysExecuteMSBuildOutOfProc || !string.Equals(MSBuildPath, defaultMSBuildPath, StringComparison.OrdinalIgnoreCase))
{
InitializeForOutOfProcForwarding();
}
}
private void InitializeForOutOfProcForwarding()
{
_forwardingApp = new ForwardingAppImplementation(
MSBuildPath,
_msbuildRequiredParameters.Concat(_argsToForward.Select(Escape)),
environmentVariables: _msbuildRequiredEnvironmentVariables);
}
public ProcessStartInfo GetProcessStartInfo()
{
Debug.Assert(_forwardingApp != null, "Can't get ProcessStartInfo when not executing out-of-proc");
return _forwardingApp.GetProcessStartInfo();
}
public string[] GetAllArguments()
{
return _msbuildRequiredParameters.Concat(_argsToForward.Select(Escape)).ToArray();
}
public void EnvironmentVariable(string name, string value)
{
if (_forwardingApp != null)
{
_forwardingApp.WithEnvironmentVariable(name, value);
}
else
{
_msbuildRequiredEnvironmentVariables.Add(name, value);
}
if (value == string.Empty || value == "\0")
{
// Do not allow MSBuild NOIPROCNODE as null env vars are not properly transferred to build nodes
_msbuildRequiredEnvironmentVariables["MSBUILDNOINPROCNODE"] = "0";
// Unlike ProcessStartInfo.EnvironmentVariables, Environment.SetEnvironmentVariable can't set a variable
// to an empty value, so we just fall back to calling MSBuild out-of-proc if we encounter this case.
// https://github.com/dotnet/runtime/issues/50554
InitializeForOutOfProcForwarding();
}
}
public int Execute()
{
if (_forwardingApp != null)
{
return GetProcessStartInfo().Execute();
}
else
{
return ExecuteInProc(GetAllArguments());
}
}
public int ExecuteInProc(string[] arguments)
{
// Save current environment variables before overwriting them.
Dictionary<string, string> savedEnvironmentVariables = new Dictionary<string, string>();
try
{
foreach (KeyValuePair<string, string> kvp in _msbuildRequiredEnvironmentVariables)
{
savedEnvironmentVariables[kvp.Key] = Environment.GetEnvironmentVariable(kvp.Key);
Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
}
try
{
// Execute MSBuild in the current process by calling its Main method.
return Microsoft.Build.CommandLine.MSBuildApp.Main(arguments);
}
catch (Exception exception)
{
// MSBuild, like all well-behaved CLI tools, handles all exceptions. In the unlikely case
// that something still escapes, we print the exception and fail the call. Non-localized
// string is OK here.
Console.Error.Write("Unhandled exception: ");
Console.Error.WriteLine(exception.ToString());
return unchecked((int)0xe0434352); // EXCEPTION_COMPLUS
}
}
finally
{
// Restore saved environment variables.
foreach (KeyValuePair<string, string> kvp in savedEnvironmentVariables)
{
Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
}
}
}
private static string Escape(string arg) =>
// this is a workaround for https://github.com/Microsoft/msbuild/issues/1622
IsRestoreSources(arg) ?
arg.Replace(";", "%3B")
.Replace("://", ":%2F%2F") :
arg;
private static string GetMSBuildExePath()
{
return Path.Combine(
AppContext.BaseDirectory,
MSBuildExeName);
}
private static string GetMSBuildSDKsPath()
{
var envMSBuildSDKsPath = Environment.GetEnvironmentVariable("MSBuildSDKsPath");
if (envMSBuildSDKsPath != null)
{
return envMSBuildSDKsPath;
}
return Path.Combine(
AppContext.BaseDirectory,
SdksDirectoryName);
}
private static string GetDotnetPath()
{
return new Muxer().MuxerPath;
}
private static bool IsRestoreSources(string arg)
{
return arg.StartsWith("/p:RestoreSources=", StringComparison.OrdinalIgnoreCase) ||
arg.StartsWith("/property:RestoreSources=", StringComparison.OrdinalIgnoreCase) ||
arg.StartsWith("-p:RestoreSources=", StringComparison.OrdinalIgnoreCase) ||
arg.StartsWith("-property:RestoreSources=", StringComparison.OrdinalIgnoreCase);
}
}
}
#endif