Skip to content

x/tools/cmd/goimports: inserts unexpected blank space after an inline comment #26246

@ncw

Description

@ncw

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

go version go1.10.1 linux/amd64

with

goimports latest 16f8f9b

Does this issue reproduce with the latest release?

Yes with the latest version of goimports

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ncw/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/ncw/go"
GORACE=""
GOROOT="/opt/go/go1.10"
GOTMPDIR=""
GOTOOLDIR="/opt/go/go1.10/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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-build952400509=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I saved this program into a file https://play.golang.org/p/soaLshUrvTf

package main

import (
	"fmt"
	"os"

	"github.com/ncw/rclone/vfs/vfsflags"
	"github.com/spf13/cobra"
	"golang.org/x/net/context" // switch to "context" when we stop supporting go1.8
	"golang.org/x/net/webdav"
)

func main() {
	var (
		_ context.Context
		_ *os.File
		_ webdav.File
		_ cobra.Command
		_ = vfsflags.Opt
	)
	fmt.Println("Hello")
}

I then ran goimports -d on it

What did you expect to see?

I expected to see no output

What did you see instead?

I saw that goimports wanted to add an extra blank line in.

diff -u goimportstest.go.orig goimportstest.go
--- goimportstest.go.orig	2018-07-06 09:19:48.722405604 +0100
+++ goimportstest.go	2018-07-06 09:19:48.722405604 +0100
@@ -7,6 +7,7 @@
 	"github.com/ncw/rclone/vfs/vfsflags"
 	"github.com/spf13/cobra"
 	"golang.org/x/net/context" // switch to "context" when we stop supporting go1.8
+
 	"golang.org/x/net/webdav"
 )
 

Discussion

goimports didn't used to add this blank line in, so I used git bisect to discover where the behaviour changed, and I discovered it was introduced in b23eb62 by @Gnouc as part of fixing #23709

I haven't worked out the exact situation that causes the problem - it seems quite sensitive to exactly which import lines are present. For example if you remove the vfsflags import then it doesn't insert that extra blank line. If you remove the comment // switch to ... then it doesn't exhibit the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.help wanted

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions