Skip to content

Strip trivia from enclosing type parameter lists in Options validation generator - #136881

Closed
Manish-1kumar wants to merge 2 commits into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/options-gen-conditional-generics-cs1027
Closed

Manish-1kumar wants to merge 2 commits into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/options-gen-conditional-generics-cs1027

Conversation

@Manish-1kumar

Copy link
Copy Markdown
Contributor

Fixes #133455

Description

When an enclosing generic type declaration contains conditional compilation directives crossing its generic parameter list:
csharp public partial class OptionsOuterTypeParameters< #if TRIVIA_BRANCH T> #else T> #endif { [OptionsValidator] public sealed partial class Validator : IValidateOptions<OptionsOuterTypeParametersModel> { } }
parent.TypeParameterList.ToString() captured the opening #if TRIVIA_BRANCH while dropping the matching #endif that lay outside the <...> syntax span. This emitted unmatched preprocessor directives into Validators.g.cs, causing compiler error CS1027: #endif directive expected.

Changes

  • Reconstructed enclosing type parameter lists by joining variance keywords and identifiers directly (), avoiding trivia preservation.
  • Added regression tests in SourceGeneration.Unit.Tests verifying that conditional preprocessor directives across enclosing generic type headers produce clean generated source that compiles without errors.

…n generator

When an enclosing generic type declaration contains conditional compilation directives across its generic parameter list, stringifying parent.TypeParameterList captures internal preprocessor directives without capturing matching closing directives outside the span. This emitted unmatched preprocessor directives into Validators.g.cs, causing error CS1027.

Format type parameter lists cleanly by joining variance keywords and identifiers without trivia.

Fixes dotnet#133455
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 2, 2026
@azure-pipelines

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

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.

…on generator

Restore parent.ConstraintClauses and spacing in Parser.cs so generic constraints on enclosing classes are retained in generated code and trailing whitespace matches generator expectations.
@svick
svick requested review from mrek-msft, rosebyte and svick November 3, 2026 11:42
@Manish-1kumar

Copy link
Copy Markdown
Contributor Author

Closing this as #136966 fixed #133455 with a more complete, symbol-based approach (including regression tests for the same conditional-directive scenario). Thanks!

@svick

svick commented Nov 5, 2026

Copy link
Copy Markdown
Member

@Manish-1kumar Thanks for understanding that we didn't take this PR.

For future reference, if an issue has an assignee, it's best to ask before working on it (even if it looks like it hasn't been touched in months).

@Manish-1kumar

Copy link
Copy Markdown
Contributor Author

Thanks @svick, understood. I'll check for assignees and ask on the issue first from now on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Extensions-Options community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Options validation generator produces CS1027 for conditional enclosing generic headers

2 participants