Skip to content

About

Minimal repro for ASP.NET Core ValidationsGenerator CS8785 when AddValidation() is called from multiple sites.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

AddValidation repro: CS8785 in ValidationsGenerator

Minimal repro for CSC : warning CS8785: Generator 'ValidationsGenerator' failed to generate source when services.AddValidation() is called from more than one site in the same project.

Reproduce

dotnet build

Expected output includes:

CSC : warning CS8785: Generator 'ValidationsGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName 'ValidatableInfoResolver.g.cs' of the added source file must be unique within a generator. (Parameter 'hintName')'.

See the runtime impact

dotnet run

Open the URL Kestrel reports (default http://localhost:5xxx/). The page hosts a tiny form that POSTs to /widgets. Submit an empty value or a 101+ character value:

  • Generator healthy: HTTP 400 with validation errors.
  • Generator aborted (this repro's default): HTTP 200, the invalid payload is echoed back. Validation never runs, the failure is silent.

Root cause

In src/Validation/gen/ValidationsGenerator.cs, the Initialize method registers:

var addValidation = context.SyntaxProvider.CreateSyntaxProvider(
    predicate: FindAddValidation,
    transform: TransformAddValidation
);
// validatableTypes is .Collect()ed
var emitInputs = addValidation.Combine(validatableTypes);
context.RegisterSourceOutput(emitInputs, ...);

CreateSyntaxProvider returns IncrementalValuesProvider<T> (plural). Combining plural with a single-value collection yields plural. RegisterSourceOutput therefore fires once per AddValidation() call site. Each invocation calls context.AddSource("ValidatableInfoResolver.g.cs", ...) in src/Validation/gen/Emitters/ValidationsGenerator.Emitter.cs. The second and subsequent attempts throw.

Proposed fix

Add .Collect() on addValidation before .Combine(validatableTypes), and adjust Emit to consume an ImmutableArray<InterceptableLocation?> instead of a single InterceptableLocation?. One emission, all call sites consolidated.

Versions

  • .NET SDK: 10.0.203
  • Microsoft.AspNetCore.App: 10.0.7

About

Minimal repro for ASP.NET Core ValidationsGenerator CS8785 when AddValidation() is called from multiple sites.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages