Skip to content

x/tools/cmd/goimports: inconsistency with go fmt #37584

@ghost

Description

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

$ go version
go version go1.14 freebsd/amd64

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ryan/.cache/go-build"
GOENV="/home/ryan/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="freebsd"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="freebsd"
GOPATH="/home/ryan/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/freebsd_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build464992684=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Used the tools go fmt and goimports on the following file (a minimal test case):

package main

func f() {
	var d = struct {
		Shorty int
		Short int
		VeryMuchLongerFieldName int
	}{
		Shorty: 5,
		Short: 5,
		VeryMuchLongerFieldName: 5,
	}
}

Go fmt converts it to:

package main

func f() {
	var d = struct {
		Shorty                  int
		Short                   int
		VeryMuchLongerFieldName int
	}{
		Shorty:                  5,
		Short:                   5,
		VeryMuchLongerFieldName: 5,
	}
}

Goimports converts it to:

package main

func f() {
	var d = struct {
		Shorty                  int
		Short                   int
		VeryMuchLongerFieldName int
	}{
		Shorty: 5,
		Short:  5,
		VeryMuchLongerFieldName: 5,
	}
}

The two tools disagree. This results in them always trying to reformat each others' code.

Goimports starts to agree with gofmt if Shorty and Short are in the opposite order, so that the field length progresses upward; or if VeryMuchLongerFieldName is reduced to MuchLongerFieldName.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeToolsThis label describes issues relating to any tools in the x/tools repository.WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions