@@ -201,7 +201,6 @@ public override int Execute()
201
201
}
202
202
203
203
Dictionary < string , string ? > savedEnvironmentVariables = [ ] ;
204
- ProjectCollection ? projectCollection = null ;
205
204
try
206
205
{
207
206
// Set environment variables.
@@ -212,17 +211,20 @@ public override int Execute()
212
211
}
213
212
214
213
// Set up MSBuild.
215
- ReadOnlySpan < ILogger > binaryLoggers = binaryLogger is null ? [ ] : [ binaryLogger ] ;
216
- projectCollection = new ProjectCollection (
214
+ ReadOnlySpan < ILogger > binaryLoggers = binaryLogger is null ? [ ] : [ binaryLogger . Value ] ;
215
+ IEnumerable < ILogger > loggers = [ .. binaryLoggers , consoleLogger ] ;
216
+ var projectCollection = new ProjectCollection (
217
217
MSBuildArgs . GlobalProperties ,
218
- [ .. binaryLoggers , consoleLogger ] ,
218
+ loggers ,
219
219
ToolsetDefinitionLocations . Default ) ;
220
220
var parameters = new BuildParameters ( projectCollection )
221
221
{
222
- Loggers = projectCollection . Loggers ,
222
+ Loggers = loggers ,
223
223
LogTaskInputs = binaryLoggers . Length != 0 ,
224
224
} ;
225
225
226
+ BuildManager . DefaultBuildManager . BeginBuild ( parameters ) ;
227
+
226
228
// Do a restore first (equivalent to MSBuild's "implicit restore", i.e., `/restore`).
227
229
// See https://github.com/dotnet/msbuild/blob/a1c2e7402ef0abe36bf493e395b04dd2cb1b3540/src/MSBuild/XMake.cs#L1838
228
230
// and https://github.com/dotnet/msbuild/issues/11519.
@@ -234,8 +236,6 @@ public override int Execute()
234
236
hostServices : null ,
235
237
BuildRequestDataFlags . ClearCachesAfterBuild | BuildRequestDataFlags . SkipNonexistentTargets | BuildRequestDataFlags . IgnoreMissingEmptyAndInvalidImports | BuildRequestDataFlags . FailOnUnresolvedSdk ) ;
236
238
237
- BuildManager . DefaultBuildManager . BeginBuild ( parameters ) ;
238
-
239
239
var restoreResult = BuildManager . DefaultBuildManager . BuildRequest ( restoreRequest ) ;
240
240
if ( restoreResult . OverallResult != BuildResultCode . Success )
241
241
{
@@ -250,12 +250,6 @@ public override int Execute()
250
250
CreateProjectInstance ( projectCollection ) ,
251
251
targetsToBuild : MSBuildArgs . RequestedTargets ?? [ "Build" ] ) ;
252
252
253
- // For some reason we need to BeginBuild after creating BuildRequestData otherwise the binlog doesn't contain Evaluation.
254
- if ( NoRestore )
255
- {
256
- BuildManager . DefaultBuildManager . BeginBuild ( parameters ) ;
257
- }
258
-
259
253
var buildResult = BuildManager . DefaultBuildManager . BuildRequest ( buildRequest ) ;
260
254
if ( buildResult . OverallResult != BuildResultCode . Success )
261
255
{
@@ -282,7 +276,7 @@ public override int Execute()
282
276
Environment . SetEnvironmentVariable ( key , value ) ;
283
277
}
284
278
285
- binaryLogger ? . Shutdown ( ) ;
279
+ binaryLogger ? . Value . ReallyShutdown ( ) ;
286
280
consoleLogger . Shutdown ( ) ;
287
281
}
288
282
@@ -310,7 +304,7 @@ static Action<IDictionary<string, string>> AddRestoreGlobalProperties(ReadOnlyDi
310
304
} ;
311
305
}
312
306
313
- static ILogger ? GetBinaryLogger ( IReadOnlyList < string > ? args )
307
+ static Lazy < FacadeLogger > ? GetBinaryLogger ( IReadOnlyList < string > ? args )
314
308
{
315
309
if ( args is null ) return null ;
316
310
// Like in MSBuild, only the last binary logger is used.
@@ -319,12 +313,17 @@ static Action<IDictionary<string, string>> AddRestoreGlobalProperties(ReadOnlyDi
319
313
var arg = args [ i ] ;
320
314
if ( LoggerUtility . IsBinLogArgument ( arg ) )
321
315
{
322
- return new BinaryLogger
316
+ // We don't want to create the binlog file until actually needed, hence we wrap this in a Lazy.
317
+ return new ( ( ) =>
323
318
{
324
- Parameters = arg . IndexOf ( ':' ) is >= 0 and var index
325
- ? arg [ ( index + 1 ) ..]
326
- : "msbuild.binlog" ,
327
- } ;
319
+ var logger = new BinaryLogger
320
+ {
321
+ Parameters = arg . IndexOf ( ':' ) is >= 0 and var index
322
+ ? arg [ ( index + 1 ) ..]
323
+ : "msbuild.binlog" ,
324
+ } ;
325
+ return LoggerUtility . CreateFacadeLogger ( [ logger ] ) ;
326
+ } ) ;
328
327
}
329
328
}
330
329
0 commit comments