Validate C# identifiers in DurableTask source generator#578
Conversation
- Add DiagnosticDescriptors for invalid task and event names - Add IsValidCSharpIdentifier helper using SyntaxFacts.IsValidIdentifier - Update DurableTaskTypeInfo and DurableEventTypeInfo to track name locations - Report diagnostics for invalid identifiers and skip code generation - Add comprehensive tests for various invalid identifier scenarios - All 59 tests passing Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
|
@copilot address comments |
- Change diagnostic IDs from DURABLE1001/1002 to DURABLE3001/3002 to avoid conflicts with existing analyzer IDs - Add AnalyzerReleases.Shipped.md and AnalyzerReleases.Unshipped.md for source generator diagnostics - Update tests to use new diagnostic IDs - Resolves RS2008 warnings about analyzer release tracking - All 61 tests passing Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
Addressed the issues:
All 61 tests passing ✅ (commit 59e8011) |
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds validation for C# identifiers used in [DurableTask] and [DurableEvent] attribute names, preventing the source generator from producing invalid code. When an invalid identifier is detected (e.g., containing dots, hyphens, or spaces), the generator reports a diagnostic error and skips code generation for that item.
Key Changes:
- Added identifier validation using Roslyn's
SyntaxFacts.IsValidIdentifier() - Introduced diagnostic rules DURABLE3001 (invalid task names) and DURABLE3002 (invalid event names)
- Extended type tracking to capture attribute name locations for precise error reporting
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Generators/DurableTaskSourceGenerator.cs | Added validation logic, diagnostic descriptors, and filtering to prevent code generation for invalid identifiers |
| test/Generators.Tests/InvalidIdentifierTests.cs | Comprehensive test coverage for invalid/valid identifiers across tasks, orchestrators, and events |
| src/Generators/AnalyzerReleases.Shipped.md | Added placeholder for tracking shipped analyzer releases |
| src/Generators/AnalyzerReleases.Unshipped.md | Documented new diagnostic rules DURABLE3001 and DURABLE3002 |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Changed foreach loops to use simple if statements instead of extracting .Where() filters - This makes the code more straightforward and easier to read - All 61 tests passing Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
|
@copilot update pr description to follow .github/PULL_REQUEST_TEMPLATE.md |
Updated PR description to follow the template in |
|
@copilot resolve the conflict |
- Resolved merge conflict in DurableTaskSourceGenerator.cs - Kept validation logic for invalid identifiers - Adopted new DetermineIsDurableFunctions method from main - All 71 tests passing (61 original + 10 new from main) Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
Resolved the merge conflict with main branch (commit 62d3af4). The conflict was in
|
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>

Summary
What changed?
[DurableTask]and[DurableEvent]attributesDetermineIsDurableFunctionsmethodWhy is this change needed?
When invalid C# identifiers are used in
[DurableTask(name)]attributes (e.g.,"Foo.Bar"), the source generator would generate invalid code likeCallFoo.BarAsync(), causing compilation errors. This change prevents that by validating names and reporting clear diagnostic errors to users.Issues / work items
Project checklist
AI-assisted code disclosure (required)
Was an AI tool used? (select one)
If AI was used:
src/Generators/DurableTaskSourceGenerator.cs- Added validation logic, diagnostic descriptors, resolved merge conflictstest/Generators.Tests/InvalidIdentifierTests.cs- Comprehensive test coveragesrc/Generators/AnalyzerReleases.Shipped.mdandAnalyzerReleases.Unshipped.md- DocumentationDetermineIsDurableFunctionsmethodAI verification (required if AI was used):
Testing
Automated tests
Manual validation (only if runtime/behavior changed)
[DurableTask("Foo.Bar")]attributeerror DURABLE3001: The task name 'Foo.Bar' is not a valid C# identifier. Task names must start with a letter or underscore and contain only letters, digits, and underscores.Notes for reviewers
DetermineIsDurableFunctionsmethod while preserving validation logicOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.