Skip to content
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

cmd/go: default GOBIN to GOPATH[0]/bin when outside of GOPATH? #23439

Open
sslavic opened this issue Jan 13, 2018 · 10 comments
Open

cmd/go: default GOBIN to GOPATH[0]/bin when outside of GOPATH? #23439

sslavic opened this issue Jan 13, 2018 · 10 comments
Milestone

Comments

@sslavic
Copy link

@sslavic sslavic commented Jan 13, 2018

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

1.9.2

Does this issue reproduce with the latest release?

Yes

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

Linux, amd64

What did you do?

$ docker run -it --rm --name test golang:1 bash
$ go get github.com/nats-io/go-nats-streaming
$ go install /go/src/github.com/nats-io/go-nats-streaming/examples/stan-pub.go

What did you expect to see?

/go/bin/stan-pub binary installed, consistent with the requirements when go install <package> is used, just GOPATH is set and that should be enough, GOBIN should not be required.

What did you see instead?

Error message:
go install: no install location for .go files listed on command line (GOBIN not set)

@cznic

This comment has been minimized.

Copy link
Contributor

@cznic cznic commented Jan 13, 2018

Arguments of go install are documented to be packages, not files. Try

$ go get github.com/nats-io/go-nats-streaming
$ go install github.com/nats-io/go-nats-streaming/examples
$ 

or

$ go build /go/src/github.com/nats-io/go-nats-streaming/examples/stan-pub.go
@sslavic

This comment has been minimized.

Copy link
Author

@sslavic sslavic commented Jan 13, 2018

Thanks @cznic for your comment. What I understand from documentation and implementation is that the name of the argument is packages, but .go files being passed is documented as special case (it's not documented that it works only if GOBIN is set, and I would like that to be fixed not to require GOBIN). I.e. see https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies

$ go install --help
usage: install [build flags] [packages]

Install compiles and installs the packages named by the import paths,
along with their dependencies.

For more about the build flags, see 'go help build'.
For more about specifying packages, see 'go help packages'.

See also: go build, go get, go clean.

Notice there go help packages reference.
See https://golang.org/cmd/go/#hdr-Description_of_package_lists

$ go help packages
Many commands apply to a set of packages:

	go action [packages]

...

As a special case, if the package list is a list of .go files from a
single directory, the command is applied to a single synthesized
package made up of exactly those files, ignoring any build constraints
in those files and ignoring any other files in the directory.

...

If one go install github.com/nats-io/go-nats-streaming/examples, it results in unwanted behaviour, just first main/app in directory with 3 apps gets installed to /go/bin, stan-bench, with examples as binary name. Yes, example apps are preferred by convention to be in dedicated packages/dirs, but they aren't - I've logged improvement ticket for that (see https://github.com/nats-io/go-nats-streaming/issues/164). Even without them being in dedicated packages/dirs, by the docs go file name should be enough, it should be used as package name and resulting binary name. Since the special case docs does not mention that it additionally requires GOBIN to be set, implementation should be fixed not to require it, or docs could be updated to mention GOBIN is required in this case - I prefer former.

Second suggestion, to go build /go/src/github.com/nats-io/go-nats-streaming/examples/stan-pub.go builds stan-pub binary into current working directory, which makes it a partial workaround to go install inconsistency issue, but that does not fix the inconsistency.

This ticket is intended to fix the inconsistency in go install, inconsistency affecting many e.g. see https://stackoverflow.com/q/25216765/381140.

@titanous titanous added the NeedsFix label Jan 16, 2018
@titanous titanous added this to the Go1.11 milestone Jan 16, 2018
@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Go1.12 Jul 2, 2018
@gopherbot gopherbot removed the NeedsFix label Oct 2, 2018
@rsc

This comment has been minimized.

Copy link
Contributor

@rsc rsc commented Oct 24, 2018

The use of GOPATH/bin only applies to packages stored in GOPATH.
In general GOPATH is a list of tree roots, and a package in one root gets installed to the corresponding bin directory.

go install /other/file.go is not considered to be in any GOPATH, so it can only use GOBIN.

In the long term, with modules, we expect that people will stop setting GOPATH and then GOBIN may be more important (or maybe it will increase pressure to default to GOPATH[0]/bin).

@rsc

This comment has been minimized.

Copy link
Contributor

@rsc rsc commented Oct 24, 2018

FWIW I would put the examples in subdirectories and then you can go install blah/blah/examples/...

@rsc

This comment has been minimized.

Copy link
Contributor

@rsc rsc commented Oct 25, 2018

Note that if we do make GOBIN effectively default to GOPATH[0]/bin, then we could make 'go env GOBIN' show that default.

@bcmills bcmills modified the milestones: Go1.12, Go1.13 Jan 18, 2019
@bcmills bcmills changed the title cmd/go: GOBIN inconsistently not required for installing package but required for .go file cmd/go: default GOBIN to GOPATH[0]/bin when outside of GOPATH Jan 18, 2019
@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Jan 18, 2019

See also #29005.

@bcmills bcmills changed the title cmd/go: default GOBIN to GOPATH[0]/bin when outside of GOPATH cmd/go: default GOBIN to GOPATH[0]/bin when outside of GOPATH? Jan 18, 2019
@newtorn

This comment was marked as spam.

Copy link

@newtorn newtorn commented May 22, 2019

@andybons

This comment was marked as off-topic.

Copy link
Member

@andybons andybons commented May 22, 2019

@newtorn stop posting links to your repository here and in other issues.

@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Feb 5, 2020

@jayconrod, @matloob: I think we should address this in Go 1.15. The inability to rely on $(go env GOBIN) when setting PATH makes new-user setup more complex than it needs to be.

@bcmills bcmills modified the milestones: Unplanned, Go1.15 Feb 5, 2020
@matloob

This comment has been minimized.

Copy link
Contributor

@matloob matloob commented Feb 5, 2020

sounds good to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
10 participants
You can’t perform that action at this time.