Skip to content

x/tools/cmd/goimports: goimports does not handle multiple duplicate packages #28213

@p-kozlowski

Description

@p-kozlowski

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

go version go1.9 linux/amd64

Does this issue reproduce with the latest release?

No

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/piotr/code"
GORACE=""
GOROOT="/home/piotr/.gvm/gos/go1.9"
GOTOOLDIR="/home/piotr/.gvm/gos/go1.9/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build852677388=/tmp/go-build -gno-record-gcc-switches"
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"

What did you do?

Take this code example: https://play.golang.org/p/Cw_JLKi807F

package main

 import (
"fmt"
"goimports_bug/package1"
"goimports_bug/package1"
"goimports_bug/package2"
"goimports_bug/package2"
"goimports_bug/package3"
"goimports_bug/package3"
)

func main() {

package1.Print()
package2.Print()
package3.Print()

}

run goimports file.go

What did you expect to see?


import (
	"goimports_bug/package1"
	"goimports_bug/package2"
	"goimports_bug/package3"
)

func main() {

	package1.Print()
	package2.Print()
	package3.Print()

}

What did you see instead?

package main

import (
	"goimports_bug/package1"
	"goimports_bug/package2"
	"goimports_bug/package3"

	"goimports_bug/package3"
)

func main() {

	package1.Print()
	package2.Print()
	package3.Print()

}

Additional analysis

Essentially, this bug reproduces when you have at least 3 duplicated packages, the more duplicated packages (ie. package4 x 2, package5 x 2) the more spectacular the results. Running goimports multiple times (O(logn)) eventually fixes the file and removes all duplicates.

It seems this problem was introduced in the following commit: https://go.googlesource.com/tools/+/12a7c317e894c0a622b5ed27f0a102fcdd56d1ad%5E%21/#F0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions