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

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

Closed
p-kozlowski opened this issue Oct 15, 2018 · 8 comments
Closed

Comments

@p-kozlowski
Copy link

@p-kozlowski p-kozlowski commented Oct 15, 2018

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

@gopherbot gopherbot added this to the Unreleased milestone Oct 15, 2018
@FiloSottile
Copy link
Member

@FiloSottile FiloSottile commented Oct 15, 2018

@saheienko
Copy link

@saheienko saheienko commented Oct 15, 2018

I've tried this on previous commits and results are the same.
Problem is here: https://github.com/golang/tools/blob/master/imports/sortimports.go#L142 (in this block)

@gopherbot
Copy link

@gopherbot gopherbot commented Oct 15, 2018

Change https://golang.org/cl/142257 mentions this issue: imports: remove duplicate packages

@josharian
Copy link
Contributor

@josharian josharian commented Oct 16, 2018

The code in question is also in gofmt. Does it have the same problem?

@p-kozlowski
Copy link
Author

@p-kozlowski p-kozlowski commented Oct 16, 2018

@josharian I run gofmt on the file in question, the result was correct (no duplicates). It may be an issue that I use gofmt installed with go some time ago (around Sep 17). I'll be happy to provide more information here, but I'm not sure if I should update gofmt to a newer version. Do you know how to do it safely? Will go get do the job? Or should I update the whole go distribution instead?

It worked with goimports installed several weeks (months?) ago. I first noticed this issue in a CI server that does builds in clean environments: it installs go & goimports (download https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz, go get -u golang.org/x/tools/cmd/goimports). Last successful build completed at 2018-10-11 11:21, the first failure happened at 2018-10-15 08:28.

I managed to reproduce this issue locally after updating goimports (go get -u -v golang.org/x/tools/cmd/goimports). Before that it worked fine.

@saheienko
Copy link

@saheienko saheienko commented Oct 16, 2018

I've tried it again and this issue wasn't before. Sorry for confusion.

@mikebaldry
Copy link

@mikebaldry mikebaldry commented May 28, 2019

Confirmed no longer an issue on go version go1.11 darwin/amd64

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented May 30, 2019

I can't reproduce either.

@bradfitz bradfitz closed this May 30, 2019
@golang golang locked and limited conversation to collaborators May 29, 2020
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
7 participants
You can’t perform that action at this time.