8000 cmd/gofmt not inserting empty line between "package" and "import" when canonical import is used · Issue #33413 · golang/go · GitHub
[go: up one dir, main page]

Skip to content

cmd/gofmt not inserting empty line between "package" and "import" when canonical import is used #33413

@kolyshkin

Description

@kolyshkin

TL;DR: in case package foo line is having "canonical import" comment added to it (as per https://golang.org/doc/go1.4#canonicalimports), gofmt fails to insert an empty line between "package" and "import" lines. See example below.

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

$ go version
go version go1.12.7 linux/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/kir/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/kir/go"
GOPROXY=""
GORACE=""
GOROOT="/snap/go/4098"
GOTMPDIR=""
GOTOOLDIR="/snap/go/4098/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build408267095=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$ cat << EOF >> a.go
> package foo // import "foo.com/go-foo"
> import "sync"
> var m sync.Mutex
> EOF

$ gofmt -d a.go
diff -u a.go.orig a.go
--- a.go.orig	2019-08-01 10:45:50.092931444 -0700
+++ a.go	2019-08-01 10:45:50.092931444 -0700
@@ -1,3 +1,4 @@
 package foo // import "foo.com/go-foo"
 import "sync"
+
 var m sync.Mutex

Now, if we remove the canonical import statement, it works as expected:

$ sed -i 's| // .*||' a.go

$ cat a.go
package foo
import "sync"
var m sync.Mutex

$ gofmt -d a.go
diff -u a.go.orig a.go
--- a.go.orig	2019-08-01 10:47:12.961603804 -0700
+++ a.go	2019-08-01 10:47:12.961603804 -0700
@@ -1,3 +1,5 @@
 package foo
+
 import "sync"
+
 var m sync.Mutex

What did you expect to see?

 package foo
+
 import "sync"
+
 var m sync.Mutex

What did you see instead?

 package foo // import "foo.com/go-foo"
 import "sync"
+
 var m sync.Mutex

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0