@@ -201,7 +201,6 @@ public override int Execute()
201201 }
202202
203203 Dictionary < string , string ? > savedEnvironmentVariables = [ ] ;
204- ProjectCollection ? projectCollection = null ;
205204 try
206205 {
207206 // Set environment variables.
@@ -212,17 +211,20 @@ public override int Execute()
212211 }
213212
214213 // 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 (
217217 MSBuildArgs . GlobalProperties ,
218- [ .. binaryLoggers , consoleLogger ] ,
218+ loggers ,
219219 ToolsetDefinitionLocations . Default ) ;
220220 var parameters = new BuildParameters ( projectCollection )
221221 {
222- Loggers = projectCollection . Loggers ,
222+ Loggers = loggers ,
223223 LogTaskInputs = binaryLoggers . Length != 0 ,
224224 } ;
225225
226+ BuildManager . DefaultBuildManager . BeginBuild ( parameters ) ;
227+
226228 // Do a restore first (equivalent to MSBuild's "implicit restore", i.e., `/restore`).
227229 // See https://github.com/dotnet/msbuild/blob/a1c2e7402ef0abe36bf493e395b04dd2cb1b3540/src/MSBuild/XMake.cs#L1838
228230 // and https://github.com/dotnet/msbuild/issues/11519.
@@ -234,8 +236,6 @@ public override int Execute()
234236 hostServices : null ,
235237 BuildRequestDataFlags . ClearCachesAfterBuild | BuildRequestDataFlags . SkipNonexistentTargets | BuildRequestDataFlags . IgnoreMissingEmptyAndInvalidImports | BuildRequestDataFlags . FailOnUnresolvedSdk ) ;
236238
237- BuildManager . DefaultBuildManager . BeginBuild ( parameters ) ;
238-
239239 var restoreResult = BuildManager . DefaultBuildManager . BuildRequest ( restoreRequest ) ;
240240 if ( restoreResult . OverallResult != BuildResultCode . Success )
241241 {
@@ -250,12 +250,6 @@ public override int Execute()
250250 CreateProjectInstance ( projectCollection ) ,
251251 targetsToBuild : MSBuildArgs . RequestedTargets ?? [ "Build" ] ) ;
252252
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-
259253 var buildResult = BuildManager . DefaultBuildManager . BuildRequest ( buildRequest ) ;
260254 if ( buildResult . OverallResult != BuildResultCode . Success )
261255 {
@@ -282,7 +276,7 @@ public override int Execute()
282276 Environment . SetEnvironmentVariable ( key , value ) ;
283277 }
284278
285- binaryLogger ? . Shutdown ( ) ;
279+ binaryLogger ? . Value . ReallyShutdown ( ) ;
286280 consoleLogger . Shutdown ( ) ;
287281 }
288282
@@ -310,7 +304,7 @@ static Action<IDictionary<string, string>> AddRestoreGlobalProperties(ReadOnlyDi
310304 } ;
311305 }
312306
313- static ILogger ? GetBinaryLogger ( IReadOnlyList < string > ? args )
307+ static Lazy < FacadeLogger > ? GetBinaryLogger ( IReadOnlyList < string > ? args )
314308 {
315309 if ( args is null ) return null ;
316310 // Like in MSBuild, only the last binary logger is used.
@@ -319,12 +313,17 @@ static Action<IDictionary<string, string>> AddRestoreGlobalProperties(ReadOnlyDi
319313 var arg = args [ i ] ;
320314 if ( LoggerUtility . IsBinLogArgument ( arg ) )
321315 {
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 ( ( ) =>
323318 {
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+ } ) ;
328327 }
329328 }
330329
0 commit comments