Skip to content

Fix ValidationsGenerator CS8785 when AddValidation is called from multiple sites#66675

Merged
Youssef1313 merged 10 commits into
mainfrom
copilot/fix-validations-generator-issue
Jun 9, 2026
Merged

Fix ValidationsGenerator CS8785 when AddValidation is called from multiple sites#66675
Youssef1313 merged 10 commits into
mainfrom
copilot/fix-validations-generator-issue

Conversation

Copilot AI commented May 13, 2026

Copy link
Copy Markdown
Contributor

ValidationsGenerator threw CS8785 and discarded all generated output whenever AddValidation() appeared more than once in a project. The root cause: CreateSyntaxProvider returns an IncrementalValuesProvider<T> (one item per call site), so .Combine(validatableTypes) + RegisterSourceOutput fired once per call site — each attempting to AddSource("ValidatableInfoResolver.g.cs", ...), which is illegal.

Description

  • ValidationsGenerator.cs: Call .Collect() on the addValidation provider before .Combine(validatableTypes), collapsing all call sites into a single ImmutableArray so RegisterSourceOutput fires exactly once.
  • ValidationsGenerator.Emitter.cs: Update Emit to accept ImmutableArray<InterceptableLocation?>, filter nulls using .OfType<InterceptableLocation>(), bail if empty, and emit one [InterceptsLocation] attribute per call site on the single interceptor method (already declared AllowMultiple = true).
  • Regression test: CanGenerateWhenAddValidationCalledMultipleTimes — two consecutive AddValidation() calls still produce a single ValidatableInfoResolver.g.cs with two [InterceptsLocation] attributes, and the endpoint correctly returns HTTP 400 for invalid input.
  • Test infrastructure fix: Added System.ComponentModel.Primitives assembly reference to ValidationsGeneratorTestBase so the generator can resolve System.ComponentModel.DisplayNameAttribute during tests (required by the DisplayNameInfo support added in the main branch).
// Previously caused CS8785 — generator output silently discarded, endpoints never validated
builder.Services.AddValidation();
builder.Services.AddValidation();

@github-actions github-actions Bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label May 13, 2026
Copilot AI changed the title [WIP] Fix ValidationsGenerator CS8785 when AddValidation is called from multiple sites Fix ValidationsGenerator CS8785 when AddValidation is called from multiple sites May 13, 2026
Copilot AI requested a review from Youssef1313 May 13, 2026 17:20
@Youssef1313 Youssef1313 marked this pull request as ready for review May 14, 2026 10:31
@Youssef1313 Youssef1313 requested review from Copilot and oroztocil May 14, 2026 10:31

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.

Pull request overview

This PR fixes the ValidationsGenerator emitting duplicate sources (triggering CS8785 and discarding output) when AddValidation() is invoked multiple times in a project, by collapsing multiple call sites into a single emission and generating multiple [InterceptsLocation] attributes on the interceptor.

Changes:

  • Collect AddValidation call sites before combining with discovered validatable types so RegisterSourceOutput runs once.
  • Update the emitter to accept multiple interceptable locations and emit one interceptor method annotated for each call site.
  • Add a regression test and verified snapshot covering multiple AddValidation() invocations.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/Validation/gen/ValidationsGenerator.cs Collects all AddValidation call sites before combining with validatable types to prevent duplicate hint-name emissions.
src/Validation/gen/Emitters/ValidationsGenerator.Emitter.cs Emits multiple [InterceptsLocation] attributes for a single generated interceptor when multiple call sites exist.
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.NoOp.cs Adds regression test validating multiple AddValidation() calls still result in request validation (HTTP 400 on invalid payload).
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanGenerateWhenAddValidationCalledMultipleTimes#ValidatableInfoResolver.g.verified.cs Updates verified generator output to include multiple interceptor attributes for multiple call sites.

Comment thread src/Validation/gen/ValidationsGenerator.cs
Comment thread src/Validation/gen/Emitters/ValidationsGenerator.Emitter.cs Outdated

@oroztocil oroztocil 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.

Looks good. I'll leave it up to you if you want to apply the nit suggestions for null filtering.

@oroztocil oroztocil added feature-validation Issues related to model validation in minimal and controller-based APIs area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-blazor Includes: Blazor, Razor Components and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels May 18, 2026
@Youssef1313 Youssef1313 enabled auto-merge (squash) May 22, 2026 14:55
@Youssef1313

Copy link
Copy Markdown
Member

@copilot CanGenerateWhenAddValidationCalledMultipleTimes is failing. Update the test to pass.

…ComponentModel.Primitives reference and update snapshot

Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/20227e2b-a4e2-47de-b4c5-b3fdd1c71a61

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
auto-merge was automatically disabled May 24, 2026 18:22

Head branch was pushed to by a user without write access

Copilot AI commented May 24, 2026

Copy link
Copy Markdown
Contributor Author

@copilot CanGenerateWhenAddValidationCalledMultipleTimes is failing. Update the test to pass.

Fixed in dc82b23. The failure had two causes from the main branch merge: (1) the generator now looks up System.ComponentModel.DisplayNameAttribute via WellKnownTypes.Get (throws if missing), but System.ComponentModel.Primitives.dll wasn't in the test compilation's references — added MetadataReference.CreateFromFile(typeof(System.ComponentModel.DisplayNameAttribute).Assembly.Location) to ValidationsGeneratorTestBase; (2) the generator's emitter changed its output format (now uses displayNameInfo instead of displayName) — updated the snapshot accordingly.

Copilot AI requested a review from Youssef1313 May 24, 2026 18:23
@Youssef1313 Youssef1313 merged commit 9af09bb into main Jun 9, 2026
25 checks passed
@Youssef1313 Youssef1313 deleted the copilot/fix-validations-generator-issue branch June 9, 2026 10:48
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-validation Issues related to model validation in minimal and controller-based APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValidationsGenerator CS8785 when AddValidation is called from multiple sites in one project

4 participants