Skip to content

cmd/gofmt: Not idempotent for top level comments with lists and sublists #73958

Open
@thatcode

Description

@thatcode

Go version

go version go1.24.3 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build693116709=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/root/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

This is probably a duplicate of a gofmt bug I couldn't find, but this is a fairly small reproduction so I thought it worth sending to you.

root [ /usr/src/myapp ]# cat test.go
package main

  // - A test list.
  //   - A test sublist.
  // - A test 2nd list.
func testFunc() int {                                                                                                                               return 1
}
root [ /usr/src/myapp ]# xxd test.go
00000000: 7061 636b 6167 6520 6d61 696e 0a0a 2020  package main..
00000010: 2f2f 202d 2041 2074 6573 7420 6c69 7374  // - A test list
00000020: 2e0a 2020 2f2f 2020 202d 2041 2074 6573  ..  //   - A tes
00000030: 7420 7375 626c 6973 742e 0a20 202f 2f20  t sublist..  //
00000040: 2d20 4120 7465 7374 2032 6e64 206c 6973  - A test 2nd lis
00000050: 742e 0a66 756e 6320 7465 7374 4675 6e63  t..func testFunc
00000060: 2829 2069 6e74 207b 0a09 7265 7475 726e  () int {..return
00000070: 2031 0a7d 0a                              1.}.
root [ /usr/src/myapp ]# cat test.go | md5sum
3e950c42add0730b4b32b0ffc85fb9e4  -
root [ /usr/src/myapp ]# gofmt test.go
package main

// - A test list.
//   - A test sublist.
// - A test 2nd list.
func testFunc() int {                                                                                                                               return 1
}
root [ /usr/src/myapp ]# gofmt test.go | md5sum
d9b974a85814175cb8218b9bc6ca87d8  -
root [ /usr/src/myapp ]# gofmt test.go | gofmt
package main

// - A test list.
//   - A test sublist.
//
// - A test 2nd list.
func testFunc() int {                                                                                                                               return 1
}
root [ /usr/src/myapp ]# gofmt test.go | gofmt | md5sum
53a6eea170dc1329d2f96d31fb3cc083  -
  1. Have a file with a list indented when it shouldn't be.
  2. Run gofmt - it removes the indent.
  3. Run gofmt again - it adds a blank comment line.

Interestingly, it appears differently on https://go.dev/play/p/zg1E32OiO9x - it does get to the final result in a single press of Format.

test.go.txt

What did you see happen?

An unwanted blank comment line was added on the 2nd run of gofmt.

What did you expect to see?

No blank comment line added.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions