2

I've written a simple C# code generator that implements Microsoft.CodeAnalysis.ISourceGenerator that inspects an enum declaration and generates code based on members that are decorated with certain attributes. It is working as expected except that I cannot retrieve—in the ISyntaxContextReceiver that I register—any XML documentation comments.

I've tried the solution found in this SO post, but whether I inspect the leading trivia on the enum constant's syntax model or call GetDocumentationCommentXml() on the enum constant's semantic model, there's never any XML documentation. (The former technique always yields a trivia collection with a single WhitespaceTrivia item; the latter always returns an empty string.)

I suspect that this is because the GeneratorExecutionContext passed to the generator's Execute method has ParseOptions.DocumentationMode set to DocumentationMode.None, but I can't find any way to change ParseOptions. Does anyone know how this can be done or what else might enable retrieval of XML documentation nodes?

4
  • Can you give a full minimal reproducible example please? Commented Mar 14, 2022 at 18:59
  • 2
    The generator consumer should have <GenerateDocumentationFile>true</GenerateDocumentationFile> property in csproj. This controls whether the compiler parses the documentation comments or not. Commented Mar 18, 2022 at 12:26
  • @Youssef13: Thanks; that worked! But not without side effects: The (new) presence of the documentation file (.xml) could be ignored easily enough, but I had to disable no fewer than eight warning-message codes to suppress the hundreds of documentation warnings that appeared. I fear that I know the answer, but I'll ask anyway: Is there no other way to enable parsing of the documentation comments? Commented Mar 22, 2022 at 15:40
  • I don't think there is. You should also take a more careful look to the warnings you disabled. Most of the time the warnings indicate an issue. Commented Mar 22, 2022 at 19:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.