fix(codegen): escape reserved C# keywords in generated metadata namespace - #11349
akashchamp wants to merge 1 commit into
Conversation
…pace The generated metadata source's namespace was built from the sanitized assembly name without checking whether the result is a C# reserved keyword (e.g. an assembly named `default`), producing an unescaped namespace segment and a syntax error in the generated source. Apply the existing EscapeIdentifier() convention, already used elsewhere in the generator, to the sanitized assembly name where it is used as a bare namespace segment in MetadataSourceOutputGenerator. Fixes dotnet#9290. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
@akashchamp please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
Fixes #9290.
Problem
When an assembly's name is a C# reserved keyword (for example
default), the source generator emits an invalid, unescaped namespace segment for the generated metadata source, e.g.:This is a syntax error and breaks the build for any project whose assembly name collides with a C# keyword.
Root cause
MetadataSourceOutputGenerator.CreateMetadataSourceOutputbuilds the generated metadata namespace asOrleansCodeGen.<sanitized assembly name>.Identifier.SanitizeIdentifierNamestrips invalid characters and prefixes a leading digit, but it does not check whether the resulting identifier is a C# reserved keyword, so a bare keyword likedefaultpasses straight through intoSyntaxFactory.ParseName(...)as an unescaped namespace segment.The codebase already has an established convention for this exact situation:
StringExtensions.EscapeIdentifier(), which prefixes an identifier with@whenIdentifier.IsCSharpKeywordreturns true. It's already used throughoutOrleans.CodeGenerator(e.g.SymbolExtensions,ProxyGenerator,SerializableTypeDescription) to escape identifiers derived from type/member names.Fix
Apply
.EscapeIdentifier()to the sanitized assembly name at the one call site that uses it as a bare, standalone namespace segment (MetadataSourceOutputGenerator.cs). This producesOrleansCodeGen.@default, which is valid C# and matches the escaping convention used elsewhere in the generator.The fix is intentionally scoped to this call site rather than to
Identifier.SanitizeIdentifierNameitself: the other two callers ofSanitizeIdentifierName(MetadataGenerator.cs's"Metadata_" + ...class name, andProxyGenerator.cs's"Proxy_" + ...name) concatenate the sanitized name with a fixed prefix, so their results are never bare keywords and don't need escaping; escaping inside the shared helper would incorrectly insert@mid-identifier for those (e.g.Metadata_@default, which is invalid).Testing
Added
ReservedIdentifierNamespaceTests(test/Orleans.CodeGenerator.Tests/ReservedIdentifierNamespaceTests.cs), a source-generator characterization test that compiles a small project under assembly namesdefault,class, andnamespace, runsOrleansSerializationSourceGenerator, and asserts:OrleansCodeGen.@<assemblyName>.Verified the test fails with the original, pre-fix
CS1001/CS1514/CS1513syntax errors fromParseText/GetDiagnosticsreproducing the exact errors in the issue report, and passes once the fix is applied.Ran:
dotnet build test/Orleans.CodeGenerator.Tests/Orleans.CodeGenerator.Tests.csproj— succeeds, 0 warnings/errors.dotnet test --project test/Orleans.CodeGenerator.Tests/Orleans.CodeGenerator.Tests.csproj --framework net10.0 --minimum-expected-tests 1 --max-parallel-test-modules 1— fullOrleans.CodeGenerator.Testssuite passes, including the new tests.Microsoft Reviewers: Open in CodeFlow