Skip to content

x/tools/cmd/goimports: neighboring files cause wrong import decision #23001

@rogpeppe

Description

@rogpeppe

go version devel +9a13f8e Tue Nov 28 06:47:50 2017 +0000 linux/amd64

If I run goimports bar.go with the following files under $GOPATH, it imports a/y, not b/y, even though only b/y exports the function G. It seems that the import in foo.go is causing goimports to ignore the constraints that the calls are imposing. I would expect the imported package to provide all the required names - that is, even though neighboring files might be good as a starting hint, I think that goimports should still verify that the required names exist in that package, perhaps only falling back to the starting hint only if no package at all is found that satisfies the requirements (because then it's likely to be a spelling mistake).

This is a particular issue when running goimports on a file in a directory with lots of random independent main packages (e.g. my $HOME/src directory where I keep hundreds of tiny scratch Go programs).

==> src/x/bar.go <==
package x

func Q() {
	y.G()
}

==> src/x/foo.go <==
package x

import (
	"a/y"
)

func P() {
	y.F()
}

==> src/b/y/y.go <==
package y

func G() {
}

==> src/a/y/y.go <==
package y

func F() {
}

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