Go: x/tools/cmd/goimports: doesn't handle packages named "v1", etc

Created on 30 Nov 2018  ·  10Comments  ·  Source: golang/go

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

$ go version
go version go1.11 linux/amd64

I'm seeing a bad format with goimports at commit: 1c3d964395ce8f04f3b03b30aaed0b096c08c3c6

You can see the bad formatting turned into a PR here: https://github.com/knative/pkg/pull/176

The comment run is what's in the PR description (minus the -s, it's WIP).

cc @bradfitz

FrozenDueToAge

Most helpful comment

Don't name your packages v1, I guess?

All 10 comments

I see this with the head of release-branch.go1.11 as well.

Don't name your packages v1, I guess?

Yeah this blows up on anything touching K8s :P

Was this caused by https://go-review.googlesource.com/122616? (https://github.com/golang/tools/commit/893c2b1ff5959a5b546f24ed09627c6d72e3255a)

Curious whether/how people think we should fix this.

@rsc @heschik @bcmills @dmitshur @kevinburke @Gnouc @josharian @mvdan @fraenkel @haya14busa

See also #28435.

Curious whether/how people think we should fix this.

If v1 is used as the Expr in a SelectorExpr, then goimports should not remove packages that end with the suffix v1. (It's fine if it doesn't add them, though: v1 is a terrible name for a package.)

We can compute the set of “names that might be package names” in an O(N) scan over the source file.

As a workaround, what happens if you rename the package explicitly on import?

That is, change

import (
    "k8s.io/api/core/v1"
)

to

import (
    v1 "k8s.io/api/core/v1"
)

?

I haven't looked at this closely but I believe Bryan's suggestion will fix it, and my fix for #28428 will add the v1 automatically.

Confirmed that tip goimports now adds the v1 name to the import rather than deleting it.

Was this page helpful?
0 / 5 - 0 ratings