Skip to content

Fix WriteSbaUsageReport crash on project templates in text-only packages - #9987

Merged
ViktorHofer merged 1 commit into
mainfrom
viktorhofer/fix-sba-usage-report-templates
Oct 6, 2026
Merged

ViktorHofer merged 1 commit into
mainfrom
viktorhofer/fix-sba-usage-report-templates

Conversation

@ViktorHofer

Copy link
Copy Markdown
Member

Problem

Since #9906 added the .NET 11 web and McpServer project-template text-only packages, every source-build leg has logged this warning from eng/finish-source-only.proj:

warning SBA001: Failed to generate SBA usage report: System.ArgumentOutOfRangeException: length ('-6') must be a non-negative value.
   at WriteSbaUsageReport.ReadSbaPackages(...) WriteSbaUsageReport.cs:line 230

When this happens, artifacts/log/sbaPackageUsage.json is not produced. Example: all 11 SB legs in build 1624624.

It shows up as a warning rather than an error because the task call uses ContinueOnError="WarnAndContinue" and SBA001 is listed in MSBuildWarningsNotAsErrors (#4769). That is why it went unnoticed.

Root cause

ReadSbaPackages recursively enumerated every *.csproj under the package roots. It then sliced the package name assuming the <id>/<version>/<Id>.<version>.csproj layout. The new template packages contain template projects under content/, which don't follow that layout. Examples:

  • .../microsoft.dotnet.web.projecttemplates.11.0/<ver>/content/.../BlazorWasm.ServiceDefaults1.csproj
  • .../microsoft.mcpserver.projecttemplates.11.0/<ver>/content/McpServer-CSharp/local/McpServer-CSharp.csproj

Depending on enumeration order, the task either throws or silently tracks bogus packages.

Fix

  • Only enumerate project files at the <id>/<version>/ level.
  • Check the .<version> filename suffix. If it doesn't match, log SBA001 and continue rather than throwing, so the report is still produced.

Validation

I ran the task end to end against the real src/source-build-assets/src tree on main:

Original Fixed
Result SBA001 ArgumentOutOfRangeException (-6) No warnings
sbaPackageUsage.json not written written, 147 packages
Template packages n/a Microsoft.DotNet.Web.ProjectTemplates.11.0 and Microsoft.McpServer.ProjectTemplates.11.0 tracked correctly

All 147 package-level projects (118 referencePackages, 9 targetPacks, 20 textOnlyPackages) follow the naming convention. The 15 nested template projects are now skipped.

ReadSbaPackages recursively enumerated every *.csproj under the SBA package
roots and assumed the <id>/<version>/<id>.<version>.csproj layout. The .NET 11
web and McpServer project template text-only packages (added in #9906) contain
template .csproj files under content/, which caused an ArgumentOutOfRangeException
when slicing the package name. The exception surfaces as warning SBA001 and the
sbaPackageUsage.json report is not produced.

Only enumerate project files at the <id>/<version> level and validate the
naming convention instead of throwing.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 1a609b5e-5df1-40ff-8cd7-cbe2e0e3ff01
@ViktorHofer
ViktorHofer requested review from a team as code owners October 6, 2026 11:00
Copilot AI balanced review requested due to automatic review settings October 6, 2026 11:00
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The focused change correctly excludes nested template projects while preserving all 147 package-level projects.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes SBA usage report generation for text-only packages containing nested template projects.

Changes:

  • Restricts package discovery to <id>/<version>/.
  • Validates project filenames before extracting package IDs.
File Description
eng/​tools/​tasks/​Microsoft.DotNet.UnifiedBuild.Tasks/​WriteSbaUsageReport.cs Safely discovers package-level projects and skips malformed names.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@ViktorHofer
ViktorHofer requested a review from mthalman October 6, 2026 11:04
@ViktorHofer

Copy link
Copy Markdown
Member Author

@mthalman PTAL. Not sure if that's the right direction. Curious to hear your opinion. Just want to get rid of the build warnings.

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.

3 participants