Skip to content

Commit a07baaa

Browse files
authored
Ensure binlogs from file-based apps have evaluation data (#49588)
1 parent 81a7147 commit a07baaa

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public override int Execute()
161161
}
162162

163163
Dictionary<string, string?> savedEnvironmentVariables = [];
164+
ProjectCollection? projectCollection = null;
164165
try
165166
{
166167
// Set environment variables.
@@ -172,7 +173,7 @@ public override int Execute()
172173

173174
// Set up MSBuild.
174175
ReadOnlySpan<ILogger> binaryLoggers = binaryLogger is null ? [] : [binaryLogger];
175-
var projectCollection = new ProjectCollection(
176+
projectCollection = new ProjectCollection(
176177
GlobalProperties,
177178
[.. binaryLoggers, consoleLogger],
178179
ToolsetDefinitionLocations.Default);
@@ -181,7 +182,6 @@ public override int Execute()
181182
Loggers = projectCollection.Loggers,
182183
LogTaskInputs = binaryLoggers.Length != 0,
183184
};
184-
BuildManager.DefaultBuildManager.BeginBuild(parameters);
185185

186186
PrepareProjectInstance();
187187

@@ -199,6 +199,9 @@ public override int Execute()
199199
targetsToBuild: ["Restore"],
200200
hostServices: null,
201201
BuildRequestDataFlags.ClearCachesAfterBuild | BuildRequestDataFlags.SkipNonexistentTargets | BuildRequestDataFlags.IgnoreMissingEmptyAndInvalidImports | BuildRequestDataFlags.FailOnUnresolvedSdk);
202+
203+
BuildManager.DefaultBuildManager.BeginBuild(parameters);
204+
202205
var restoreResult = BuildManager.DefaultBuildManager.BuildRequest(restoreRequest);
203206
if (restoreResult.OverallResult != BuildResultCode.Success)
204207
{
@@ -212,6 +215,13 @@ public override int Execute()
212215
var buildRequest = new BuildRequestData(
213216
CreateProjectInstance(projectCollection),
214217
targetsToBuild: [BuildTarget]);
218+
219+
// For some reason we need to BeginBuild after creating BuildRequestData otherwise the binlog doesn't contain Evaluation.
220+
if (NoRestore)
221+
{
222+
BuildManager.DefaultBuildManager.BeginBuild(parameters);
223+
}
224+
215225
var buildResult = BuildManager.DefaultBuildManager.BuildRequest(buildRequest);
216226
if (buildResult.OverallResult != BuildResultCode.Success)
217227
{
@@ -459,6 +469,7 @@ private ProjectInstance CreateProjectInstance(
459469

460470
return ProjectInstance.FromProjectRootElement(projectRoot, new ProjectOptions
461471
{
472+
ProjectCollection = projectCollection,
462473
GlobalProperties = globalProperties,
463474
});
464475

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System.Runtime.Versioning;
55
using System.Text.Json;
6+
using Microsoft.Build.Framework;
7+
using Microsoft.Build.Logging.StructuredLogger;
68
using Microsoft.CodeAnalysis;
79
using Microsoft.DotNet.Cli.Commands;
810
using Microsoft.DotNet.Cli.Commands.Run;
@@ -772,7 +774,7 @@ Release config
772774
/// <c>dotnet run --bl file.cs</c> produces a binary log.
773775
/// </summary>
774776
[Theory, CombinatorialData]
775-
public void BinaryLog(bool beforeFile)
777+
public void BinaryLog_Run(bool beforeFile)
776778
{
777779
var testInstance = _testAssetsManager.CreateTestDirectory();
778780
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), s_program);
@@ -902,6 +904,29 @@ public void BinaryLog_NotSpecified()
902904
.Should().BeEmpty();
903905
}
904906

907+
/// <summary>
908+
/// Binary logs from our in-memory projects should have evaluation data.
909+
/// </summary>
910+
[Fact]
911+
public void BinaryLog_EvaluationData()
912+
{
913+
var testInstance = _testAssetsManager.CreateTestDirectory();
914+
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), s_program);
915+
916+
new DotnetCommand(Log, "run", "Program.cs", "-bl")
917+
.WithWorkingDirectory(testInstance.Path)
918+
.Execute()
919+
.Should().Pass()
920+
.And.HaveStdOut("Hello from Program");
921+
922+
string binaryLogPath = Path.Join(testInstance.Path, "msbuild.binlog");
923+
new FileInfo(binaryLogPath).Should().Exist();
924+
925+
var records = BinaryLog.ReadRecords(binaryLogPath).ToList();
926+
records.Any(static r => r.Args is ProjectEvaluationStartedEventArgs).Should().BeTrue();
927+
records.Any(static r => r.Args is ProjectEvaluationFinishedEventArgs).Should().BeTrue();
928+
}
929+
905930
/// <summary>
906931
/// Default projects do not include anything apart from the entry-point file.
907932
/// </summary>

test/dotnet.Tests/dotnet.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
</ItemGroup>
7777

7878
<ItemGroup>
79+
<PackageReference Include="Basic.CompilerLog.Util" />
7980
<PackageReference Include="Moq" />
8081
<PackageReference Include="Microsoft.Build.Tasks.Core" />
8182
<PackageReference Include="Microsoft.TemplateEngine.Mocks" />

0 commit comments

Comments
 (0)