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.
dotnet buildExpected 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')'.
dotnet runOpen 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.
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.
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.
- .NET SDK: 10.0.203
Microsoft.AspNetCore.App: 10.0.7