Skip to content

Commit 74a28f7

Browse files
authored
Support binlog in build/restore of file-based programs (#49009)
1 parent 746f86e commit 74a28f7

File tree

6 files changed

+54
-37
lines changed

6 files changed

+54
-37
lines changed

src/Cli/dotnet/Commands/Build/BuildCommand.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public static CommandBase FromParseResult(ParseResult parseResult, string msbuil
2929
parseResult.GetResult(BuildCommandParser.SelfContainedOption) is not null,
3030
parseResult.GetResult(BuildCommandParser.NoSelfContainedOption) is not null);
3131

32-
string[] fileArgument = parseResult.GetValue(BuildCommandParser.SlnOrProjectOrFileArgument) ?? [];
32+
string[] args = parseResult.GetValue(BuildCommandParser.SlnOrProjectOrFileArgument) ?? [];
33+
34+
LoggerUtility.SeparateBinLogArguments(args, out var binLogArgs, out var nonBinLogArgs);
3335

3436
string[] forwardedOptions = parseResult.OptionValuesToBeForwarded(BuildCommandParser.GetCommand()).ToArray();
3537

@@ -39,11 +41,11 @@ public static CommandBase FromParseResult(ParseResult parseResult, string msbuil
3941

4042
CommandBase command;
4143

42-
if (fileArgument is [{ } arg] && VirtualProjectBuildingCommand.IsValidEntryPointPath(arg))
44+
if (nonBinLogArgs is [{ } arg] && VirtualProjectBuildingCommand.IsValidEntryPointPath(arg))
4345
{
4446
command = new VirtualProjectBuildingCommand(
4547
entryPointFileFullPath: Path.GetFullPath(arg),
46-
msbuildArgs: forwardedOptions,
48+
msbuildArgs: [.. forwardedOptions, .. binLogArgs],
4749
verbosity: parseResult.GetValue(CommonOptions.VerbosityOption),
4850
interactive: parseResult.GetValue(CommonOptions.InteractiveMsBuildForwardOption))
4951
{
@@ -65,7 +67,7 @@ public static CommandBase FromParseResult(ParseResult parseResult, string msbuil
6567

6668
msbuildArgs.AddRange(forwardedOptions);
6769

68-
msbuildArgs.AddRange(fileArgument);
70+
msbuildArgs.AddRange(args);
6971

7072
command = new RestoringCommand(
7173
msbuildArgs: msbuildArgs,

src/Cli/dotnet/Commands/Restore/RestoreCommand.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ public static CommandBase FromParseResult(ParseResult result, string? msbuildPat
2424

2525
result.ShowHelpOrErrorIfAppropriate();
2626

27-
string[] fileArgument = result.GetValue(RestoreCommandParser.SlnOrProjectOrFileArgument) ?? [];
27+
string[] args = result.GetValue(RestoreCommandParser.SlnOrProjectOrFileArgument) ?? [];
28+
29+
LoggerUtility.SeparateBinLogArguments(args, out var binLogArgs, out var nonBinLogArgs);
2830

2931
string[] forwardedOptions = result.OptionValuesToBeForwarded(RestoreCommandParser.GetCommand()).ToArray();
3032

31-
if (fileArgument is [{ } arg] && VirtualProjectBuildingCommand.IsValidEntryPointPath(arg))
33+
if (nonBinLogArgs is [{ } arg] && VirtualProjectBuildingCommand.IsValidEntryPointPath(arg))
3234
{
3335
return new VirtualProjectBuildingCommand(
3436
entryPointFileFullPath: Path.GetFullPath(arg),
35-
msbuildArgs: forwardedOptions,
37+
msbuildArgs: [.. forwardedOptions, ..binLogArgs],
3638
verbosity: result.GetValue(CommonOptions.VerbosityOption),
3739
interactive: result.GetValue(CommonOptions.InteractiveMsBuildForwardOption))
3840
{
@@ -41,7 +43,7 @@ public static CommandBase FromParseResult(ParseResult result, string? msbuildPat
4143
};
4244
}
4345

44-
return CreateForwarding(["-target:Restore", .. forwardedOptions, .. fileArgument], msbuildPath);
46+
return CreateForwarding(["-target:Restore", .. forwardedOptions, .. args], msbuildPath);
4547
}
4648

4749
public static MSBuildForwardingApp CreateForwarding(IEnumerable<string> msbuildArgs, string? msbuildPath = null)

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -515,24 +515,12 @@ public static RunCommand FromParseResult(ParseResult parseResult)
515515
// bl information to synchronize the restore and build logger configurations
516516
var applicationArguments = parseResult.GetValue(RunCommandParser.ApplicationArguments)?.ToList();
517517

518-
var binlogArgs = new List<string>();
519-
var nonBinLogArgs = new List<string>();
520-
foreach (var arg in applicationArguments ?? [])
521-
{
522-
if (LoggerUtility.IsBinLogArgument(arg))
523-
{
524-
binlogArgs.Add(arg);
525-
}
526-
else
527-
{
528-
nonBinLogArgs.Add(arg);
529-
}
530-
}
518+
LoggerUtility.SeparateBinLogArguments(applicationArguments, out var binLogArgs, out var nonBinLogArgs);
531519

532520
var restoreArgs = parseResult.OptionValuesToBeForwarded(RunCommandParser.GetCommand()).ToList();
533-
if (binlogArgs.Count > 0)
521+
if (binLogArgs.Count > 0)
534522
{
535-
restoreArgs.AddRange(binlogArgs);
523+
restoreArgs.AddRange(binLogArgs);
536524
}
537525

538526
var command = new RunCommand(

src/Cli/dotnet/Commands/Test/MSBuildUtility.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,7 @@ public static (IEnumerable<ParallelizableTestModuleGroupWithSequentialInnerModul
6161

6262
public static BuildOptions GetBuildOptions(ParseResult parseResult, int degreeOfParallelism)
6363
{
64-
var binLogArgs = new List<string>();
65-
var otherArgs = new List<string>();
66-
67-
foreach (var arg in parseResult.UnmatchedTokens)
68-
{
69-
if (LoggerUtility.IsBinLogArgument(arg))
70-
{
71-
binLogArgs.Add(arg);
72-
}
73-
else
74-
{
75-
otherArgs.Add(arg);
76-
}
77-
}
64+
LoggerUtility.SeparateBinLogArguments(parseResult.UnmatchedTokens, out var binLogArgs, out var otherArgs);
7865

7966
var msbuildArgs = parseResult.OptionValuesToBeForwarded(TestCommandParser.GetCommand())
8067
.Concat(binLogArgs);

src/Cli/dotnet/LoggerUtility.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ private static FacadeLogger ConfigureDispatcher(List<BinaryLogger> binaryLoggers
6161
return new FacadeLogger(dispatcher);
6262
}
6363

64+
internal static void SeparateBinLogArguments(IEnumerable<string>? args, out List<string> binLogArgs, out List<string> nonBinLogArgs)
65+
{
66+
binLogArgs = new List<string>();
67+
nonBinLogArgs = new List<string>();
68+
foreach (var arg in args ?? [])
69+
{
70+
if (IsBinLogArgument(arg))
71+
{
72+
binLogArgs.Add(arg);
73+
}
74+
else
75+
{
76+
nonBinLogArgs.Add(arg);
77+
}
78+
}
79+
}
80+
6481
internal static bool IsBinLogArgument(string arg)
6582
{
6683
const StringComparison comp = StringComparison.OrdinalIgnoreCase;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,27 @@ public void BinaryLog(bool beforeFile)
573573
.Should().BeEquivalentTo(["msbuild.binlog", "msbuild-dotnet-run.binlog"]);
574574
}
575575

576+
[Theory, CombinatorialData]
577+
public void BinaryLog_Build([CombinatorialValues("restore", "build")] string command, bool beforeFile)
578+
{
579+
var testInstance = _testAssetsManager.CreateTestDirectory();
580+
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), s_program);
581+
582+
string[] args = beforeFile
583+
? [command, "-bl", "Program.cs"]
584+
: [command, "Program.cs", "-bl"];
585+
586+
new DotnetCommand(Log, args)
587+
.WithWorkingDirectory(testInstance.Path)
588+
.Execute()
589+
.Should().Pass();
590+
591+
new DirectoryInfo(testInstance.Path)
592+
.EnumerateFiles("*.binlog", SearchOption.TopDirectoryOnly)
593+
.Select(f => f.Name)
594+
.Should().BeEquivalentTo(["msbuild.binlog"]);
595+
}
596+
576597
[Theory]
577598
[InlineData("-bl")]
578599
[InlineData("-BL")]

0 commit comments

Comments
 (0)