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/format: leading spaces aren't inserted for comments on first pass #63642

Closed
lfolger opened this issue Oct 20, 2023 · 3 comments
Closed

go/format: leading spaces aren't inserted for comments on first pass #63642

lfolger opened this issue Oct 20, 2023 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@lfolger
Copy link
Contributor

lfolger commented Oct 20, 2023

What version of Go are you using (go version)?

go1.21 (discovered on tip)

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

Whatever play.go.dev is using.

What did you do?

package main

import (
	"fmt"
	"go/format"

	"github.com/google/go-cmp/cmp"
)

var in = []byte(`package main
		//some comment
		var x string`)

func main() {
	first_format, err := format.Source(in)
	if err != nil {
		fmt.Println(err)
		return
	}
	second_format, err := format.Source(first_format)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(cmp.Diff(string(first_format), string(second_format)))
}

https://go.dev/play/p/b4foaIeTooZ

What did you expect to see?

I expected no diff between formatting the file once and formatting the input twice.

What did you see instead?

The first formatting removed the identation but does not add a space between // and some comment.
The second formatting adds a space between // and some comment.
As far as I can tell go/format.Source only adds spaces between // and the comment text if these comment is not indented.

@seankhliao seankhliao changed the title go/format.Source is not idempotent go/format: leading spaces aren't inserted for comments on first pass Oct 20, 2023
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 20, 2023
@seankhliao
Copy link
Member

cc @griesemer @mvdan

see also #62559

@bcmills bcmills added Tools This label describes issues relating to any tools in the x/tools repository. compiler/runtime Issues related to the Go compiler and/or runtime. labels Oct 23, 2023
@mknyszek mknyszek added this to the Backlog milestone Nov 1, 2023
@mknyszek
Copy link
Contributor

mknyszek commented Nov 1, 2023

Should we close this as a duplicate of #62559?

@mknyszek
Copy link
Contributor

mknyszek commented Nov 1, 2023

Optimistically closing, let me know if we should reopen.

@mknyszek mknyszek closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants