Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hoeppi-google opened this issue Sep 10, 2024 · 7 comments
Open

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

hoeppi-google opened this issue Sep 10, 2024 · 7 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@hoeppi-google
Copy link
Contributor

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
Copy link

Related Issues and Documentation

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

@seankhliao
Copy link
Member

cc @mateusz834

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 10, 2024
@dmitshur dmitshur added this to the Backlog milestone Sep 10, 2024
@mateusz834
Copy link
Member

mateusz834 commented Sep 10, 2024

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
Copy link
Contributor

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

@mateusz834
Copy link
Member

CC @griesemer

@mateusz834 mateusz834 changed the title go/printer: go.dev/cl/609077 causes unintended formatting changes go/printer: CL 609077 causes unintended formatting changes Sep 10, 2024
gopherbot pushed a commit that referenced this issue Sep 11, 2024
This reverts commit CL 609077.

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

For #69382.

Change-Id: Id8f36e5503e63e7586c8afe7c7d3dc25fd56ed94
Reviewed-on: https://go-review.googlesource.com/c/go/+/612137
Reviewed-by: Christian Höppner <hoeppi@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
@mateusz834
Copy link
Member

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: #69382 (comment) and the test case, with a similar case (go doc formatting in not executed).

{
src: `package main
func main() {
//go:directive
// test
type a struct{}
}
`,
want: `package main
func main() {
//go:directive
// test
type a struct{}
}
`,

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

@griesemer
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

7 participants