Skip to content

x/tools/cmd/goimports: goimports removes valid import path #40148

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

Closed
GrigoriyMikhalkin opened this issue Jul 10, 2020 · 2 comments
Closed

x/tools/cmd/goimports: goimports removes valid import path #40148

GrigoriyMikhalkin opened this issue Jul 10, 2020 · 2 comments
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@GrigoriyMikhalkin
Copy link
Contributor

GrigoriyMikhalkin commented Jul 10, 2020

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

$ go version
go version go1.14.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/gmikhalkin/.cache/go-build"
GOENV="/home/gmikhalkin/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/gmikhalkin/.gvm/pkgsets/go1.14.3/global"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/gmikhalkin/.gvm/gos/go1.14.3"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/gmikhalkin/.gvm/gos/go1.14.3/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
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-build597149176=/tmp/go-build -gno-record-gcc-switches"

What did you do?

After i run goimports -w on file https://github.com/GrigoriyMikhalkin/sqlboiler-paginate/blob/master/examples/simple_net_example.go it removes valid import "github.com/grigoriymikhalkin/sqlboiler-paginate/v4". It's important to note that package name(paginate) differs from module name.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Jul 10, 2020
@gopherbot gopherbot added this to the Unreleased milestone Jul 10, 2020
@heschi
Copy link
Contributor

heschi commented Jul 10, 2020

This is a small bug but mostly working as intended. I'm assuming you were working outside of GOPATH. The examples directory is not a module, so goimports can't find its dependencies outside of GOPATH, and therefore can't determine that the package's name is paginate. If you run it inside of GOPATH, or make the examples directory a module with valid dependencies, goimports will work fully.

Ideally the dependency would be kept if its name is unknown but goimports is already complicated enough and people generally can compile the code they're working on. Once you run it once the import will be given an explicit name and goimports will understand it's in use.

@heschi heschi closed this as completed Jul 10, 2020
@csterritt
Copy link

For anyone trying to solve this later, the fix is simple. Change an import like:

import "gopkg.in/olahol/melody.v1"

That goimports will remove (because the package name is melody) with:

import melody "gopkg.in/olahol/melody.v1"

@golang golang locked and limited conversation to collaborators Nov 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants