Skip to content

Fix custom BuildCheck registration in multi-project builds - #15555

Merged
baronfel merged 2 commits into
dotnet:mainfrom
MattKotsenas:bugfix/buildcheck-duplicate-registration
Nov 17, 2026
Merged

baronfel merged 2 commits into
dotnet:mainfrom
MattKotsenas:bugfix/buildcheck-duplicate-registration

Conversation

@MattKotsenas

Copy link
Copy Markdown
Member

Related issue(s): #15554 tracks a separate, pre-existing configuration-ordering bug that this PR does not fix.

Context

Projects can reference the same custom BuildCheck package through ordinary PackageReference items.
When multiple projects acquire the package in one build, each acquisition currently appends another
callback registration. A two-project build reports the first project's violation once and the second
project's violation twice. Earlier registrations can also retain stale severity or enablement settings.

At build completion, tracing collects telemetry from those registrations. Constructing the tracing
dictionary throws for repeated rule IDs, stopping the build with MSB4017 and a duplicate-key exception.

Changes Made

  • Reuse the existing custom check factory context for each loaded check type, applying the acquiring
    project's configuration without registering its callbacks again.
  • Aggregate repeated telemetry rule IDs using the existing merge operation, consistent with
    BuildCheckTracingData.MergeIn.
  • Add regressions for repeated acquisition, delayed enablement, warning-to-error changes,
    disablement, and telemetry aggregation.

Compatibility

No public API, telemetry serialization format, assembly-loading policy, or configuration format changes.
Per-project severity and enablement remain configurable. The existing requirement for consistent custom
initialization configuration across projects remains in place.

Testing

BuildCheck tests pass in Debug and Release, including the added regressions. Multi-project integration
controls pass. The full patched Release run completed with 13 failed tests; its unchanged-source
comparison was interrupted by Defender quarantine and remains incomplete.

Copilot AI balanced review requested due to automatic review settings November 15, 2026 08:58

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.

🔵 Needs a closer look

The reported Release suite has 13 failing tests, and the interrupted unchanged-source comparison requires human validation to determine whether they are baseline failures or regressions before approval.

No open issues 🎉

What changed in this PR

Fixes duplicate custom BuildCheck registrations in multi-project builds and prevents repeated telemetry rule IDs from causing build-completion failures. The separate configuration-ordering bug tracked by #15554 remains out of scope.

Changes:

  • Reuses custom check registrations by loaded check type while applying each acquiring project’s configuration.
  • Merges telemetry entries with repeated rule IDs using the existing aggregation logic.
  • Adds regressions for repeated acquisition, enablement and severity changes, and telemetry aggregation.
File Description
src/​Framework/​BuildCheck/​BuildCheckTracingData.cs Merges repeated telemetry rule IDs instead of throwing on duplicate keys.
src/​Framework.UnitTests/​BuildCheckTracingEventArgs_Tests.cs Tests telemetry aggregation and preservation of infrastructure statistics.
src/​BuildCheck.UnitTests/​BuildCheckManagerProviderTests.cs Tests repeated acquisition and project-specific severity and enablement changes.
src/​Build/​BuildCheck/​Infrastructure/​BuildCheckManagerProvider.cs Reuses custom check factory contexts by type to avoid duplicate callback registration.

Review effort: Balanced

@baronfel baronfel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clean, targeted fix - LGTM!

Reuse the existing factory context for each loaded custom check type, while applying every acquiring project's configuration. This avoids duplicate callbacks and stale severities without changing rule identity or adding a second registry.

Add callback regressions for repeated acquisition, delayed enablement, warning-to-error changes, and disabling a previously enabled check.
@MattKotsenas
MattKotsenas force-pushed the bugfix/buildcheck-duplicate-registration branch from 53d58ea to 7d39616 Compare November 17, 2026 17:55
@baronfel
baronfel enabled auto-merge (squash) November 17, 2026 18:00
@baronfel
baronfel merged commit 3e68eee into dotnet:main Nov 17, 2026
13 checks passed
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