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: `gofmt -d` output confuses `diffstat` #18932

Closed
LukeShu opened this issue Feb 4, 2017 · 5 comments
Closed

cmd/gofmt: `gofmt -d` output confuses `diffstat` #18932

LukeShu opened this issue Feb 4, 2017 · 5 comments

Comments

@LukeShu
Copy link

@LukeShu LukeShu commented Feb 4, 2017

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

go version go1.7.5 linux/386

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

GOARCH="386"
GOBIN=""
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_386"
CC="gcc"
GOGCCFLAGS="-fPIC -m32 -pthread -fmessage-length=0 -fdebug-prefix-map=/run/user/1000/tmpdir/go-build106664000=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

Given a non-gofmt-formatted .go file (or files), I piped the output of gofmt -d to diffstat. Rather than seeing the name of the file in the diffstat output, I see a garbage temporary filename.

For the below examples, this the input:

$ cat hello.go
package main

import (
"fmt"
)

func main() {
        fmt.Println("Hello, playground")
}

What did you expect to see?

$ gofmt -d hello.go
diff hello.go gofmt/hello.go
--- a/hello.go        2017-02-03 19:13:00.280468375 -0500
+++ b/hello.go        2017-02-03 19:13:00.280468375 -0500
@@ -1,7 +1,7 @@
 package main
 
 import (
-"fmt"
+       "fmt"
 )
 
 func main() {

$ gofmt -d hello.go | diffstat
 hello.go |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

What did you see instead?

$ gofmt -d hello.go
diff hello.go gofmt/hello.go
--- /run/user/1000/tmpdir/gofmt316145376        2017-02-03 19:13:00.280468375 -0500
+++ /run/user/1000/tmpdir/gofmt617882815        2017-02-03 19:13:00.280468375 -0500
@@ -1,7 +1,7 @@
 package main
 
 import (
-"fmt"
+       "fmt"
 )
 
 func main() {

$ gofmt -d hello.go | diffstat
 gofmt254562456 |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
@bradfitz bradfitz added this to the Go1.9Maybe milestone Feb 4, 2017
@bradfitz bradfitz added the help wanted label Feb 4, 2017
@gopherbot
Copy link

@gopherbot gopherbot commented Feb 4, 2017

CL https://golang.org/cl/36374 mentions this issue.

@haya14busa
Copy link
Contributor

@haya14busa haya14busa commented Feb 4, 2017

I tried to fix this problem in https://golang.org/cl/36374 by simply replacing temporary filename with actual one.
It seems it works as expected with diffstat now.

$ gofmt -d cmd/gofmt/testdata/import.input | diffstat
 import.input |   47 +++++++++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 26 deletions(-)
@LukeShu
Copy link
Author

@LukeShu LukeShu commented Feb 4, 2017

That's exactly what I expected the fix to look like!

@haya14busa
Copy link
Contributor

@haya14busa haya14busa commented Feb 8, 2017

we can also use "patch" command if it will be properly fixed.

we can use gofmt -w in most cases, but I think it's useful to keep in mind that output of gofmt -d can be applyed by patch command.

@gopherbot gopherbot closed this in ee7fdc2 Feb 8, 2017
@gopherbot
Copy link

@gopherbot gopherbot commented Feb 9, 2017

CL https://golang.org/cl/36644 mentions this issue.

gopherbot pushed a commit to golang/tools that referenced this issue Feb 9, 2017
Port of gofmt change at golang/go#18932 with golang/go#18999 fix

Change-Id: I8999d6a3e04946086dd159201dbc4b09338bf1c0
Reviewed-on: https://go-review.googlesource.com/36644
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Feb 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.