Skip to content

Block Comment Handling Regression in gogen (v1.19.7 → v1.20.2) Produces Invalid Go Code #589

Description

@luoliwoshang

Description

After upgrading github.com/goplus/gogen from v1.19.7 to v1.20.2, I noticed a regression in how block comments (/* ... */) are handled when attached to a function.

In the previous version (v1.19.7), the generated Go source code was valid and could compile normally.

However, in the new version (v1.20.2), the same input produces malformed output where an extra */ is inserted before the function declaration, resulting in Go code that cannot be compiled.


Environment

  • Package: github.com/goplus/gogen
  • Working version: v1.19.7
  • Broken version: v1.20.2
  • Go version: (please fill in if needed)

Reproduction Code

package main

import (
    "go/ast"
    "go/token"
    "os"
    "strings"

    "github.com/goplus/gogen"
)

func main() {
    pkg := gogen.NewPackage("", "main", nil)

    commentGroup := &ast.CommentGroup{
        List: []*ast.Comment{
            {
                Slash: token.NoPos,
                Text:  "/* returns the version of cJSON as a string */\n",
            },
        },
    }

    fn := pkg.NewFunc(nil, "main", nil, nil, false)
    fn.SetComments(pkg, commentGroup)

    fn.BodyStart(pkg).End()

    var buf strings.Builder
    pkg.WriteTo(&buf, "")

    os.WriteFile("./temp/test.go", []byte(buf.String()), os.ModePerm)
}

Output in v1.19.7 (Correct)

package main

/* returns the version of cJSON as a string */

func main() {
}

This file compiles successfully.


Output in v1.20.2 (Incorrect)

package main

/* returns the version of cJSON as a string */
*/func main() {
}

Notice the unexpected extra */ before the function declaration.


Compilation Error

Running the generated file:

go run test.go

Produces:

# command-line-arguments
./test.go:3:1: syntax error: non-declaration statement outside function body

So the generated Go file is no longer valid and fails to compile.


Expected Behavior

The comment should be emitted cleanly above the function declaration, as in v1.19.7, without introducing invalid tokens.


Actual Behavior

In v1.20.2, block comments passed through fn.SetComments are incorrectly terminated, producing malformed Go code.


Request

Could you please check if there was a recent change in comment formatting logic between v1.19.7 and v1.20.2?

This appears to be a regression that breaks code generation when using block comments.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions