Skip to content

Commit c997920

Browse files
authored
Ensure all evaluation data are in binlogs from file-based apps (#49841)
1 parent 5a679ab commit c997920

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/Cli/dotnet/LoggerUtility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ internal static class LoggerUtility
4848
// We need a custom logger to handle this, because the MSBuild API for evaluation and execution calls logger Initialize and Shutdown methods, so will not allow us to do this.
4949
if (binaryLoggers.Count > 0)
5050
{
51-
var fakeLogger = ConfigureDispatcher(binaryLoggers);
51+
var fakeLogger = CreateFacadeLogger(binaryLoggers);
5252

5353
return fakeLogger;
5454
}
5555
return null;
5656
}
5757

58-
private static FacadeLogger ConfigureDispatcher(List<BinaryLogger> binaryLoggers)
58+
public static FacadeLogger CreateFacadeLogger(List<BinaryLogger> binaryLoggers)
5959
{
6060
var dispatcher = new PersistentDispatcher(binaryLoggers);
6161
return new FacadeLogger(dispatcher);

test/dotnet.Tests/CommandTests/Run/RunFileTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,8 @@ public void BinaryLog_EvaluationData()
939939
new FileInfo(binaryLogPath).Should().Exist();
940940

941941
var records = BinaryLog.ReadRecords(binaryLogPath).ToList();
942-
records.Any(static r => r.Args is ProjectEvaluationStartedEventArgs).Should().BeTrue();
943-
records.Any(static r => r.Args is ProjectEvaluationFinishedEventArgs).Should().BeTrue();
942+
records.Count(static r => r.Args is ProjectEvaluationStartedEventArgs).Should().Be(2);
943+
records.Count(static r => r.Args is ProjectEvaluationFinishedEventArgs).Should().Be(2);
944944
}
945945

946946
/// <summary>

0 commit comments

Comments
 (0)