Add production-ready incremental source generator for automatic Description attributes from XML comments#899
Conversation
stephentoub
left a comment
There was a problem hiding this comment.
@copilot, please also add a test project for testing the source generator
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
Added
All tests passing. |
|
@copilot, the code at https://github.com/dotnet/aspnetcore/tree/main/src/OpenApi/Gen provides a production-ready implementation of a similar concept. Is there anything to learn or adapt from that implementation that should be used here as well? |
Reviewed the ASP.NET Core OpenAPI generator and applied key production-ready patterns: Structural improvements:
Robustness improvements:
All tests passing (8 source generator tests + 800 existing tests). |
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
tests/ModelContextProtocol.Analyzers.Tests/XmlToDescriptionGeneratorTests.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
tests/ModelContextProtocol.Analyzers.Tests/XmlToDescriptionGeneratorTests.cs
Show resolved
Hide resolved
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
|
@copilot, there are also some build failures in CI: |
... Fixed the CI build failures. The issue was a direct
... Fixed the CI build failures. The issue was a direct |
|
@copilot, CI is still failing all legs. Please look at the failures and fix it. |
Fixed in c337fd2. The issue was that the ProjectReference to SourceGenerators needed When All 992 tests passing locally. |
a6ce1e0 to
32619c0
Compare
|
@eiriktsarpalis, @halter73, I overhauled this and it's ready for review. |
src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs
Outdated
Show resolved
Hide resolved
cb5ed32 to
ea6fb05
Compare
| }); | ||
|
|
||
| // Combine with compilation to get well-known type symbols. | ||
| var compilationAndMethods = context.CompilationProvider.Combine(allMethods); |
There was a problem hiding this comment.
This is explicitly called out as an anti-pattern in the incremental generator guide: https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md#authoring-a-cache-friendly-generator.
Compilation information should be flowed into models representing each of the methods we're generating for.
There was a problem hiding this comment.
And we can also improve it later. I think what we have here is good enough to start. If you're adamant it be revised, please feel free to do so.
There was a problem hiding this comment.
In the past we've been receiving complaints from the IDE team because of source generators misbehaving like that.
…on tools/prompts/resources
ea6fb05 to
13175f1
Compare
|
@eiriktsarpalis, I need a sign-off on this PR. |
Latest Change
Removed the explicit generic type parameter
<MethodToGenerate?>fromCreateSyntaxProvideron line 24. The compiler can infer this type from the return type of the transform lambda, making the code cleaner.Before:
After:
The type inference works because the lambda returns
MethodToGenerate?explicitly, allowing the compiler to determine the generic parameter automatically.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.