Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 29, 2025

MSBuild previously accepted multiple -bl flags but only wrote the last-specified binlog, preventing tools like CodeQL from controlling binlog output locations when users also specify binlogs.

Changes

Smart multi-binlog handling

  • Analyzes all -bl parameter sets to detect configuration differences beyond file paths
  • Same configuration: Single logger writes once, then copies to all destinations atomically on shutdown (ensures consistency)
  • Different configurations: Creates separate logger instances to respect each configuration (e.g., ProjectImports=None vs ProjectImports=Embed)

BinaryLogger enhancements

  • Added AdditionalFilePaths property to track copy destinations
  • Added ExtractFilePathFromParameters() to parse file paths from parameter strings
  • Added ExtractNonPathParameters() to extract and normalize configuration options
  • Enhanced Shutdown() to copy binlog to additional paths with error handling

Command-line processing

  • Updated ProcessBinaryLogger() in XMake.cs with configuration detection logic
  • Deduplicates identical file paths across multiple flags

Example

# Same configuration - optimized single write + copy
msbuild project.csproj /bl:1.binlog /bl:2.binlog /bl:3.binlog

# Different configurations - separate loggers
msbuild project.csproj /bl:full.binlog /bl:minimal.binlog;ProjectImports=None

Both approaches produce identical binlogs when configurations match, and distinct binlogs when configurations differ.

Original prompt

This section details on the original issue you should resolve

<issue_title>MSBuild.exe should support writing multiple binlogs if supplied as part of command line arguments</issue_title>
<issue_description>### Summary

Today it's possible to provide the -bl flag multiple times, but the behavior of the engine is that the last-provided flag wins:

> dotnet build -bl:1.binlog -bl:2.binlog
Restore complete (0.1s)
    info NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  lib2 net10.0 succeeded (0.1s) → D:\Code\scratch\bleh\lib2\bin\Debug\net10.0\lib2.dll
  bleh net10.0 succeeded (0.1s) → bin\Debug\net10.0\bleh.dll

Build succeeded in 0.5s
> ls *.binlog

    Directory: D:\Code\scratch\bleh\lib

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          10/29/2025 11:14 AM         454756 2.binlog

This is confusing, and in extreme cases can mean that tools that need binlogs and provide them via things like response files can be overridden by a users invocation.

Background and Motivation

The inability to write more than one binlog file means that tools like codeql have a harder time adopting binlog usage for C# code analysis, because they can't control where the binlog is generated.

Proposed Feature

The command-line handling should accept multiple bl flags, resolve them into their distinct binlog filenames, ensure that set is distinct, and then the binary logger should be able to write binary logs to all of those specified files.

This file writing could be done in two ways (at least)

  • multiple independent writers, each writing to a separate file
  • one writer, writing the binlog file to a temp location and then atomically copying that file to the various destinations at build-finish

We should use the second option for consistency.

Alternative Designs

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits October 29, 2025 16:52
- Added AdditionalFilePaths property to BinaryLogger to track multiple output destinations
- Modified ProcessBinaryLogger to collect all distinct binlog paths from multiple -bl flags
- Updated BinaryLogger.Shutdown to copy the binlog to all additional paths
- Added ExtractFilePathFromParameters helper method to parse file paths from parameters
- Added error message resource for copy failures

Co-authored-by: baronfel <[email protected]>
- Added MultipleBinaryLogsCreatesMultipleFiles test to verify all specified binlog files are created
- Added MultipleBinaryLogsWithDuplicatesCreateDistinctFiles test to verify duplicates are handled correctly
- Both tests verify files are identical

Co-authored-by: baronfel <[email protected]>
- Enhanced ProcessBinaryLogger to detect configuration differences
- If all binlog options only differ in file paths, use optimized copy approach
- If configurations differ (e.g., ProjectImports settings), create separate logger instances
- Added ExtractNonPathParameters method to compare configurations
- Added test for different configurations creating separate loggers
- This approach respects all configuration options while optimizing when possible

Co-authored-by: baronfel <[email protected]>
Copilot AI changed the title [WIP] Support writing multiple binlogs in MSBuild Support multiple binary logs from command line arguments Oct 29, 2025
Copilot finished work on behalf of baronfel October 29, 2025 17:13
Copilot AI requested a review from baronfel October 29, 2025 17:13
/// Gets or sets additional output file paths. When set, the binlog will be copied to all these paths
/// after the build completes. The primary FilePath will be used as the temporary write location.
/// </summary>
public List<string> AdditionalFilePaths { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rainersigwald is this the binlog API that end-users end up using? If so we probably want to think about this exposed shape a bit more and force this stuff into 'normal 'logger parameter parsing instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MSBuild.exe should support writing multiple binlogs if supplied as part of command line arguments

2 participants