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

cmd/gofmt: -r strips some comments when pattern matching #57574

Open
rski opened this issue Jan 4, 2023 · 1 comment
Open

cmd/gofmt: -r strips some comments when pattern matching #57574

rski opened this issue Jan 4, 2023 · 1 comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@rski
Copy link

rski commented Jan 4, 2023

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

$ go version
rski@rski ~/C/g/go (master)> go version                                                                  
go version go1.19.4 linux/amd64

Does this issue reproduce with the latest release?

yup, tried gofmt built from 9955a7e

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/rski/.cache/go-build"
GOENV="/home/rski/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/rski/Code/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/rski/Code/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/nix/store/a7875alzpnr46z6mv4ssymfdwmvr6xbq-go-1.19.4/share/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/nix/store/a7875alzpnr46z6mv4ssymfdwmvr6xbq-go-1.19.4/share/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build158369519=/tmp/go-build -gno-record-gcc-switches"

What did you do?

input file:

package main

import "errors"

func errorWithOneArg1(a func() error, v int) error { return a() }
func errorWithOneArg2(a func() error) error        { return a() }

func layer(f func() error, s string) error { return f() }

func anotherLayer() error {
	return layer(
		func() error {
			return errorWithOneArg1(func() error {
				// This comment is erased
				return errors.New("abc")
			}, 30)
		}, "test")
}

command and result:

gofmt -r 'errorWithOneArg1(a,b) -> errorWithOneArg2(a)' test.go                      
package main

import (
	"errors"
)

func errorWithOneArg1(a func() error, v int) error { return a() }
func errorWithOneArg2(a func() error) error        { return a() }

func layer(f func() error, s string) error { return f() }

func anotherLayer() error {
	return layer(
		func() error {
			return errorWithOneArg2(func() error {

				return errors.New("abc")
			})

		}, "test")
}

Note how the comment was replaced with a blank line.

This doesn't happen when not doing matching i.e.

gofmt -r 'errorWithOneArg1 -> errorWithOneArg2' test.go  | grep -En "return errorWith|com
ment"
15:			return errorWithOneArg2(func() error {
16:				// This comment is erased

the code is rewritten and the comment is kept

@rsc rsc changed the title src/cmd/gofmt: -r strips comments when pattern matching cmd/gofmt: -r strips comments when pattern matching Jan 4, 2023
@rsc rsc changed the title cmd/gofmt: -r strips comments when pattern matching cmd/gofmt: -r strips some comments when pattern matching Jan 4, 2023
@rsc
Copy link
Contributor

rsc commented Jan 4, 2023

This is not entirely surprising. @adonovan is working on a different comment representation that may work better.

@heschi heschi added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 4, 2023
@heschi heschi added this to the Backlog milestone Jan 4, 2023
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

3 participants