golang / go

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

cmd/go: executables are sometimes named after their module's last element, not directory #27283

Closed spenczar closed 5 years ago

spenczar commented 5 years ago

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

go1.11

Does this issue reproduce with the latest release?

yes

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

darwin amd64

What did you do?

In a package main's directory (specifically, github.com/twitchtv/twirp/protoc-gen-twirp), I had a go.mod like this:

module github.com/twitchtv/twirp/protoc-gen-twirp/v5

I then ran GO111MODULE=on go build . from within $GOPATH/src/github.com/twitchtv/twirp/protoc-gen-twirp.

I expected an executable named protoc-gen-twirp, because that is the last element of the directory. This is the documented behavior in https://tip.golang.org/cmd/go/#hdr-Compile_packages_and_dependencies:

When compiling a single main package, build writes the resulting executable to an output file named after the first source file ('go build ed.go rx.go' writes 'ed' or 'ed.exe') or the source code directory ('go build unix/sam' writes 'sam' or 'sam.exe').

Instead, an executable was produced named v5.

However, if I invoke go build with a full path, I get the expected name. GO111MODULE=on go build github.com/twitchtv/twirp/protoc-gen-twirp produces an executable named protoc-gen-twirp.

This is either a documentation error, or a bug.

mvdan commented 5 years ago

cc @bcmills @rsc @myitcv

myitcv commented 5 years ago

Confirmed.

export GOPATH=$(mktemp -d)
cd $(mktemp -d)
mkdir hello
cd hello
go mod init example.com/hello/v6
cat <<EOD > main.go

package main

func main() {}
EOD
go build .

Now:

$ ls
go.mod  main.go  v6

But:

$ go install
$ ls $GOPATH/bin
hello

go install was fixed in https://github.com/golang/go/issues/24667; sounds like a similar fix needed for go build.

gopherbot commented 5 years ago

Change https://golang.org/cl/140863 mentions this issue: cmd/go: fix the default build output name for versioned binaries

FiloSottile commented 5 years ago

If this does not land before RC1 (in the next couple of days), open a backport issue if it should be cherry-picked into 1.12.

/cc @hyangah

gopherbot commented 5 years ago

Change https://golang.org/cl/157798 mentions this issue: cmd/go: fix the default build output name for versioned binaries

robfordww commented 5 years ago

Modules with name 'example.com/mymodule.git' ,where the .git extension is used to trigger the local VCS command, get a binary named mymodule.git.exe. Shouldn't '.git' be stripped?

chappjc commented 5 years ago

If this does not land before RC1 (in the next couple of days), open a backport issue if it should be cherry-picked into 1.12.

/cc @hyangah

Looks like it didn't make 1.12. I don't know how a backport issue is started, but if this could be cherry-picked into 1.12 I'd be very happy.

mvdan commented 5 years ago

@gopherbot please backport to 1.12

gopherbot commented 5 years ago

Backport issue(s) opened: #30266 (for 1.12).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

ianlancetaylor commented 5 years ago

Are we trying to get https://golang.org/cl/140863 into 1.12? @hyangah @bcmills

mvdan commented 5 years ago

@ianlancetaylor from the "backport" wording earlier, I presume 1.12.1 was meant.

bcmills commented 5 years ago

@mvdan is correct: we're targeting 1.12.1.

gopherbot commented 5 years ago

Change https://golang.org/cl/167384 mentions this issue: [release-branch.go1.12] cmd/go: fix the default build output name for versioned binaries

gopherbot commented 5 years ago

Change https://golang.org/cl/167617 mentions this issue: Revert "[release-branch.go1.12] cmd/go: fix the default build output name for versioned binaries"

gopherbot commented 5 years ago

Change https://golang.org/cl/167503 mentions this issue: cmd/go: fix the default build output name for versioned binaries

dmitshur commented 5 years ago

Reopening because the original CL 140863 was reverted on master branch via CL 168402.

gopherbot commented 5 years ago

Change https://golang.org/cl/168958 mentions this issue: [release-branch.go1.12] cmd/go: fix the default build output name for versioned binaries