Skip to content

A class whose member has an invalid doc comment (a line break in a param/see-also name) is still written up to that member, then throws (#164 again, through DocComment) #188

Open

Description

@matt-edmondson

What's wrong

ClassTemplate.Validate is meant to refuse a bad class before any of it is written (#169). It checks the kind, the members and the nested classes, but none of the doc comments:

internal override void Validate()
{
_ = TypeKindKeywords.For(Kind);
foreach (MemberTemplate member in Members)
{
member.Validate();
}
foreach (ClassTemplate nestedClass in NestedClasses)
{
nestedClass.Validate();
}
}

The doc-comment checks (EnsureAttributesFitOnOneLine, Templates/DocComment.cs around lines 111 and 192-223) run only inside DocComment.WriteTo, so a bad doc comment fails partway through writing the class.

Reproduction

  • Inside one class. A class has members A and B, and B.Documentation.Params contains a name with a line break ("a\nb"). AddClass throws ArgumentException after the CodeBlocker already holds:
    public class C
    {
    	void A() { }
    
    }
    
    The output has a closing brace but has lost B and everything after it.
  • Across a source file. AddSourceFile with two classes, where the second class's Documentation.SeeAlso contains "A\nB". It throws after writing the namespace, the usings and all of class First. SourceFileTemplate validates every template up front (SourceFileTemplate.cs around line 62) precisely to avoid this kind of partial output.

This is distinct from #183 (an empty expression body) and #176 (a BodyFactory that throws).

Suggested fix / acceptance criteria

  • Move the doc-comment checks into a Validate that runs before writing. Call it from TemplateBase.Validate for the template's own Documentation, so ClassTemplate.Validate covers the class itself, every member and every nested class.
  • Add tests: both cases above throw ArgumentException and leave the CodeBlocker's output unchanged (empty, or exactly what it held before the call).

Activity

  1. matt-edmondson commented on Nov 5, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Category: Bug
    Priority: Medium. It produces partial output and then throws, which is exactly what #169's up-front validation and SourceFileTemplate's pre-check were meant to prevent. It needs malformed doc-comment input (a line break in a param or see-also name), so typical callers won't hit it.

    Area / suggested assignment: Templates/DocComment.cs and TemplateBase.Validate, with ClassTemplate.Validate and SourceFileTemplate as callers. This is the "check before writing" series.

    Duplicates / in progress

    Next steps: Extract EnsureAttributesFitOnOneLine and the related checks into DocComment.Validate(). Call it from TemplateBase.Validate, so class, member and nested-class validation all cover it. Add the two "output unchanged" tests.


    Generated by Claude Code

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

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions