AgentSkillsCN

binlog-failure-analysis

.NET/MSBuild 构建失败时的必备技能。关键要求:一旦看到“构建失败”“error CS”“error MSB”,或 dotnet build/msbuild/dotnet test 命令返回非零退出码,务必第一时间调用此技能——在向用户报告错误或尝试修复之前,切勿直接分析控制台输出。利用二进制日志进行全面的构建失败分析。

SKILL.md
--- frontmatter
name: binlog-failure-analysis
description: "MANDATORY skill for .NET/MSBuild build failures. BLOCKING REQUIREMENT: When you see 'Build failed', 'error CS', 'error MSB', or non-zero exit code from dotnet build/msbuild/dotnet test commands, you MUST invoke this skill IMMEDIATELY as your first action - before reporting errors to the user or attempting fixes. Do NOT analyze console output directly. Uses binary logs for comprehensive build failure analysis."

Analyzing MSBuild Failures with Binary Logs

CRITICAL: Mandatory Invocation Rules

This skill is a BLOCKING REQUIREMENT when any .NET build fails. You MUST invoke this skill when:

  • dotnet build, dotnet test, msbuild, or any MSBuild-based command exits with non-zero code
  • Console output contains "Build failed", "error CS", "error MSB", or similar build errors
  • Any .NET compilation or restore operation fails

What you MUST NOT do:

  • Do NOT report build errors directly from console output to the user
  • Do NOT attempt to fix errors based on console output alone
  • Do NOT skip this skill because "the errors look simple"

Correct behavior: Invoke this skill → analyze with binlog-mcp tools → then report findings and suggest fixes.

Fallback: Only if the binlog-mcp tools fail to provide useful information (e.g., binlog not generated, tools return errors, or analysis is inconclusive), you may fall back to analyzing console output directly. Always attempt binlog analysis first.

When an MSBuild build fails, use the binlog-mcp tool to deeply analyze the failure. This skill guides you through generating a binary log and using the MCP tools to diagnose issues.

Step 1: Generate a Binary Log

Re-run the failed build command with the /bl flag to generate a binary log file:

bash
# For dotnet builds
dotnet build /bl

# For msbuild directly
msbuild /bl

# Custom binlog filename
dotnet build /bl:build.binlog

The /bl flag tells MSBuild to generate a msbuild.binlog file (or the specified filename) in the current directory.

Step 2: Load the Binary Log

Use the load_binlog tool to load the generated binlog file:

code
load_binlog with path: "<absolute-path-to-binlog>"

This must be called before using any other binlog analysis tools. Returns InterestingBuildData containing total duration in milliseconds and node count.

Step 3: Analyze the Failure

Get Diagnostics (Errors and Warnings)

Use get_diagnostics to extract all errors and warnings:

code
get_diagnostics with:
  - binlog_file: "<path>"
  - includeErrors: true
  - includeWarnings: true
  - includeDetails: true
  - projectIds: [optional array of project IDs to filter]
  - targetIds: [optional array of target IDs to filter]
  - taskIds: [optional array of task IDs to filter]
  - maxResults: [optional max number of diagnostics]

Returns DiagnosticAnalysisResult with severity classification (Error, Warning, Info), source locations, file paths, line numbers, and context information.

Search for Specific Issues

Use search_binlog with the powerful query language to find specific issues:

code
search_binlog with:
  - binlog_file: "<path>"
  - query: "error CS1234"        # Find specific error codes
  - query: "$task Csc"           # Find all C# compilation tasks
  - query: "under($project MyProject)"  # Find nodes under a specific project
  - maxResults: 300              # Default limit
  - includeDuration: true        # Include timing info
  - includeContext: true         # Include project/target/task IDs

Investigate Expensive Operations

If the build is slow or timing out:

code
get_expensive_targets with binlog_file and top_number: 10
get_expensive_tasks with binlog_file and top_number: 10
get_expensive_projects with binlog_file, top_number: 10, sortByExclusive: true

The get_expensive_projects tool supports:

  • excludeTargets: Array of target names to exclude (e.g., ['Copy', 'CopyFilesToOutputDirectory'])
  • sortByExclusive: Sort by exclusive time (true) or inclusive time (false)

Analyze Roslyn Analyzers

If compilation is slow, check analyzer performance:

code
get_expensive_analyzers with binlog_file and top_number: 10

Returns aggregated analyzer data with execution count, total/average/min/max durations.

For specific Csc task analyzer details:

code
get_task_analyzers with binlog_file, projectId, targetId, taskId

Available Tools Reference

Binlog Loading

ToolDescription
load_binlogLoad a binlog file (required before other tools)

Diagnostic Analysis

ToolDescription
get_diagnosticsExtract errors/warnings with optional filtering

Search Analysis

ToolDescription
search_binlogPowerful freetext search with MSBuild Log Viewer syntax

Project Analysis

ToolDescription
list_projectsList all projects in the build
get_expensive_projectsGet N most expensive projects
get_project_build_timeGet build time for a specific project
get_project_target_listList all targets for a project
get_project_target_timesGet all target times for a project in one call

Target Analysis

ToolDescription
get_expensive_targetsGet N most expensive targets
get_target_info_by_idGet target details by ID (more efficient)
get_target_info_by_nameGet target details by name
search_targets_by_nameFind all executions of a target across projects

Task Analysis

ToolDescription
get_expensive_tasksGet N most expensive tasks
get_task_infoGet detailed task invocation info
list_tasks_in_targetList all tasks in a target
search_tasks_by_nameFind all invocations of a task

Analyzer Analysis

ToolDescription
get_expensive_analyzersGet N most expensive Roslyn analyzers/generators
get_task_analyzersGet analyzer data from a specific Csc task

Evaluation Analysis

ToolDescription
list_evaluationsList all evaluations for a project
get_evaluation_global_propertiesGet global properties for an evaluation
get_evaluation_properties_by_nameGet specific properties by name
get_evaluation_items_by_nameGet items by type (Compile, PackageReference, etc.)

File Analysis

ToolDescription
list_files_from_binlogList embedded source files
get_file_from_binlogGet content of an embedded file

Timeline Analysis

ToolDescription
get_node_timelineGet active/inactive time for build nodes

Common Analysis Workflows

Build Error Investigation

  1. load_binlog - Load the binlog
  2. get_diagnostics with includeErrors: true - Get all errors
  3. search_binlog with the error code - Find context around the error
  4. list_projects - Identify which projects are involved
  5. get_file_from_binlog - View source files embedded in the binlog

Performance Investigation

  1. load_binlog - Load the binlog
  2. get_expensive_targets - Find slow targets
  3. get_expensive_tasks - Find slow tasks
  4. get_expensive_analyzers - Check if analyzers are slow
  5. search_targets_by_name - Find all executions of a specific target
  6. get_node_timeline - Analyze parallelism and node utilization

Dependency/Evaluation Issues

  1. load_binlog - Load the binlog
  2. list_projects - See all projects in the build
  3. list_evaluations - Check for multiple evaluations (indicates overbuilding)
  4. get_evaluation_global_properties - Compare properties between evaluations
  5. get_evaluation_items_by_name - Inspect PackageReference, Compile items

Query Language Reference

The search_binlog tool supports powerful query syntax from MSBuild Structured Log Viewer:

Basic Search

QueryDescription
textFind nodes containing text
"exact phrase"Exact string matching
term1 term2Multiple terms (AND logic)

Node Type Filtering

QueryDescription
$task TaskNameFind tasks by name
$target TargetNameFind targets by name
$project ProjectNameFind project nodes
$cscShortcut for $task Csc
$rarShortcut for $task ResolveAssemblyReference

Property Matching

QueryDescription
name=valueMatch nodes where name equals value
value=textMatch nodes where value equals text

Hierarchical Search

QueryDescription
under($project X)Find nodes under project X
notunder($target Y)Exclude nodes under target Y
project($query)Find nodes within matching projects
not($query)Exclude matching nodes

Time-based Filtering

QueryDescription
start<"2023-01-01 09:00:00"Nodes started before time
start>"2023-01-01 09:00:00"Nodes started after time
end<"datetime"Nodes ended before time
end>"datetime"Nodes ended after time

Special Properties

QueryDescription
skipped=trueFind skipped targets
skipped=falseFind executed targets
height=N or height=maxFilter by tree depth
$123Find node by index

Result Enhancement

QueryDescription
$time or $durationInclude timing in results
$start or $starttimeInclude start time
$end or $endtimeInclude end time

Tips

  • The binlog contains embedded source files - use list_files_from_binlog and get_file_from_binlog to view them
  • Use maxResults parameter to limit large result sets
  • Use get_target_info_by_id instead of get_target_info_by_name when you have the ID for better performance
  • Use get_project_target_times to get all target times in one call instead of querying individually
  • Results from get_expensive_projects and get_project_build_time are cached for performance
  • The binlog captures the complete build state, making it ideal for reproducing and diagnosing issues