Skip to content

Go: after a failed generation inside an interface, later functions on the same generator lose func and their body #359

Closed

Description

@matt-edmondson

What's wrong

GoGenerator.GenerateInterface (Coder/Languages/GoGenerator.cs:919 and :932) sets insideInterface = true; before the member loop and insideInterface = false; after it, with no try/finally. If any member throws, the flag stays true on that generator instance.

This is the same class of bug that #342 (62530e4) fixed for insideType in the C, C++ and Rust generators. #357 tracks the Rust insideTrait / insideTraitImplementation flags. Go's insideInterface is not covered by either.

Why it matters

Generators are singletons, and the editor keeps regenerating on the same instance after a preview fails (#285). While insideInterface is wrongly true:

  • GenerateFunctionShape (~L1040) writes only the signature, e.g. Answer() int, with no func, no receiver and no body.
  • GenerateFieldDeclaration (~L1461) replaces every package-level field with the "a Go interface holds methods" note.
  • The abstract-method guard (~L991) and WritesTypeParametersDown (~L1029) take the interface branch.

The generator stays broken until some later interface finishes generating cleanly.

Reproduction (from the code path)

  1. Generate an interface (ClassDeclaration, Kind = Interface) with a method whose parameter is IsOptional = true and has DefaultValue = new BinaryExpression(Literal.Number(1), (BinaryOperator)999, Literal.Number(2)). That is the malformed operator FailedGenerationStateTests already uses. GenerateParameter (~L1347) calls AppendDefaultValueComment, which generates the default and throws NotSupportedException while insideInterface is true.
  2. On the same GoGenerator, generate a file with a free function int Answer() { return 1; }.
  3. Expected: func Answer() int {\n\treturn 1\n}. Actual: the signature only, which does not compile.

Suggested fix

Save the old value and restore it in finally, the way the C, C++ and Rust fixes for #342 do:

bool outer = insideInterface;
insideInterface = true;
try { /* member loop */ }
finally { insideInterface = outer; }

Acceptance criteria

  • A GoGenerator case in Coder.Test/Languages/FailedGenerationStateTests.cs: an interface whose method has the bad default, followed by a document with a free function. The second output matches what a fresh generator produces.

Activity

  1. added
    bug
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions