golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.97k stars 17.67k forks source link

go/printer: CL 609077 causes unintended formatting changes #69382

Open hoeppi-google opened 2 months ago

hoeppi-google commented 2 months ago

go.dev/cl/609077 causes a change in formatting. Reading the CL description and the test cases leads me to believe that this change is unintended.

Reproducer:

package main

import (
    "bytes"
    "fmt"
    "go/parser"
    "go/printer"
    "go/token"
)

const content = `package foo

type S struct {
//- comment
Name *string 
}`

func main() {
    fset := token.NewFileSet()
    file, err := parser.ParseFile(fset, "", content, parser.ParseComments)
    if err != nil {
        panic(err)
    }
    var out bytes.Buffer
    if err := (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(&out, fset, file); err != nil {
        panic(err)
    }
    fmt.Println(out.String())
}

Before go.dev/cl/609077, this printed:

package foo

type S struct {
        // - comment
        Name *string
}

After go.dev/cl/609077, this prints:

package foo

type S struct {
        //- comment
        Name *string
}
gabyhelp commented 2 months ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

seankhliao commented 2 months ago

cc @mateusz834

mateusz834 commented 2 months ago

That is true that:

type S struct {
//- comment
Name *string 
}

was formatted to this:

type S struct {
    // - comment
    Name *string 
}

but, formatting:

type S struct {
    //- comment
    Name *string 
}

formats it to this:

type S struct {
    //- comment
    Name *string 
}

There is no change.

Before CL 609077 the godoc formatting was performed for all comments, that start at the first Column of a line, this is wrong (e.g. for comments at the first column inside of a func declaration). I think that the fix is right.

Maybe there should be a separate discussion to always start adding a space before a comment?

gopherbot commented 2 months ago

Change https://go.dev/cl/612137 mentions this issue: go/printer: revert "do not treat comments inside a ast.Decl as godoc"

mateusz834 commented 2 months ago

CC @griesemer

mateusz834 commented 1 month ago

This change was reverted, without any further discussion here with a comment:

it turned out to also introduce a change to the formatting as described in issue #69382, which wasn't intended.

This formatting change was intended, see my comment above: https://github.com/golang/go/issues/69382#issuecomment-2341595364 and the test case, with a similar case (go doc formatting in not executed).

https://github.com/golang/go/blob/90391c2e8ad8da167aed53bad5857008a410d0c1/src/go/printer/printer_test.go#L919-L935

Would like to hear how we should now move forward with this change.

griesemer commented 1 month ago

@mateusz834 Apologies for the revert w/o external discussion. W/o going details, this change broke some Google internal tests and in order to unblock this we decided to revert this change for now. The correct fix may well be that we need to make some changes to the internal code, but we haven't had the time to look into it. If we decide to do that, we may be able to simply reapply your CL. Thanks.