@@ -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.
@@ -213,7 +212,7 @@ public override int Execute()
213212
214213 // Set up MSBuild.
215214 ReadOnlySpan < ILogger > binaryLoggers = binaryLogger is null ? [ ] : [ binaryLogger ] ;
216- projectCollection = new ProjectCollection (
215+ var projectCollection = new ProjectCollection (
217216 MSBuildArgs . GlobalProperties ,
218217 [ .. binaryLoggers , consoleLogger ] ,
219218 ToolsetDefinitionLocations . Default ) ;
@@ -223,19 +222,23 @@ public override int Execute()
223222 LogTaskInputs = binaryLoggers . Length != 0 ,
224223 } ;
225224
225+ // Projects need to be created before BeginBuild is called, otherwise binlog doesn't contain evaluation data.
226+ var restoreProject = NoRestore ? null : CreateProjectInstance ( projectCollection , addGlobalProperties : AddRestoreGlobalProperties ( MSBuildArgs . RestoreGlobalProperties ) ) ;
227+ var buildProject = NoBuild ? null : CreateProjectInstance ( projectCollection ) ;
228+
229+ BuildManager . DefaultBuildManager . BeginBuild ( parameters ) ;
230+
226231 // Do a restore first (equivalent to MSBuild's "implicit restore", i.e., `/restore`).
227232 // See https://github.com/dotnet/msbuild/blob/a1c2e7402ef0abe36bf493e395b04dd2cb1b3540/src/MSBuild/XMake.cs#L1838
228233 // and https://github.com/dotnet/msbuild/issues/11519.
229- if ( ! NoRestore )
234+ if ( restoreProject != null )
230235 {
231236 var restoreRequest = new BuildRequestData (
232- CreateProjectInstance ( projectCollection , addGlobalProperties : AddRestoreGlobalProperties ( MSBuildArgs . RestoreGlobalProperties ) ) ,
237+ restoreProject ,
233238 targetsToBuild : [ "Restore" ] ,
234239 hostServices : null ,
235240 BuildRequestDataFlags . ClearCachesAfterBuild | BuildRequestDataFlags . SkipNonexistentTargets | BuildRequestDataFlags . IgnoreMissingEmptyAndInvalidImports | BuildRequestDataFlags . FailOnUnresolvedSdk ) ;
236241
237- BuildManager . DefaultBuildManager . BeginBuild ( parameters ) ;
238-
239242 var restoreResult = BuildManager . DefaultBuildManager . BuildRequest ( restoreRequest ) ;
240243 if ( restoreResult . OverallResult != BuildResultCode . Success )
241244 {
@@ -244,18 +247,12 @@ public override int Execute()
244247 }
245248
246249 // Then do a build.
247- if ( ! NoBuild )
250+ if ( buildProject != null )
248251 {
249252 var buildRequest = new BuildRequestData (
250- CreateProjectInstance ( projectCollection ) ,
253+ buildProject ,
251254 targetsToBuild : MSBuildArgs . RequestedTargets ?? [ "Build" ] ) ;
252255
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-
259256 var buildResult = BuildManager . DefaultBuildManager . BuildRequest ( buildRequest ) ;
260257 if ( buildResult . OverallResult != BuildResultCode . Success )
261258 {
0 commit comments