golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.42k stars 17.71k forks source link

x/tools/cmd/goimports: adds redundant alias to package paths containing hyphen/dash #46635

Open perrydunn opened 3 years ago

perrydunn commented 3 years ago

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

$ go version
go version go1.14.14 linux/amd64

Does this issue reproduce with the latest release?

Yes (compiled with go1.16.5 too)

$ git log -n 1
commit 1225b6f53f675b0732bdf025af585b173de322ec (HEAD -> master, origin/master, origin/HEAD)

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/peregrine/.cache/go-build"
GOENV="/home/peregrine/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/peregrine/go:/home/peregrine/core3:/home/peregrine/core3/src/plz-out/go:/home/peregrine/core3/src/plz-out/gen/third_party/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/tm/tools/go/1.14.14/usr/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/tm/tools/go/1.14.14/usr/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="cc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/peregrine/go/src/golang.org/x/tools/go.mod"
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-build730235714=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/8VOphLYHxto

What did you expect to see?

No redundant alias

What did you see instead?

Redundant alias added

cherrymui commented 3 years ago

cc @heschi @bradfitz

heschi commented 3 years ago

goimports has a hardcoded rule for go- prefixes; I suppose it could expand to cover golang-. Other than that, this is working as intended.

perrydunn commented 3 years ago

Thanks. Will these rules be documented (so that, for example, people coming up with names can bear them in mind)? If they're already documented, I apologise, though I haven't been able to find such references.

heschi commented 3 years ago

It's not documented anywhere, and I suppose it wouldn't hurt if there's a reasonable place to put it. None occur to me offhand though. Is there somewhere in particular you would have expected to find a description?

perrydunn commented 3 years ago

Perhaps in the godoc overview?

dsh2dsh commented 1 year ago

Wow! At least I found the answer, why I'm getting

import (
    "log"

    dotenv "github.com/dsh2dsh/expx-dotenv"
)

instead of

import (
    "log"

    "github.com/dsh2dsh/expx-dotenv"
)

It really should be documented somewhere. Hmm... now I have a question. Why does it add redundant alias? I thought Go imports github.com/dsh2dsh/expx-dotenv and makes it accessible as dotenv by default? Yes, I just tried and can confirm it works without alias. Could somebody explain the reason or give a link to an explanation? Thanks.

heschi commented 1 year ago

See #28428.