golang / go

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

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

Open sslavic opened 6 years ago

sslavic commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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.

rsc commented 5 years ago

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 commented 5 years ago

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

rsc commented 5 years ago

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

bcmills commented 5 years ago

See also #29005.

newtorn commented 5 years ago

https://github.com/newtorn/newgo

andybons commented 5 years ago

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

bcmills commented 4 years ago

@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.

matloob commented 4 years ago

sounds good to me

gopherbot commented 4 years ago

Change https://golang.org/cl/232017 mentions this issue: cmd/go: default GOBIN to GOPATH[0]/bin for packages outside GOPATH

ianlancetaylor commented 3 years ago

The CL has been abandoned, so I'm guessing that this won't get done for 1.17. Should we change the milestone to 1.18 or Backlog?

jayconrod commented 3 years ago

Ah, should have changed that earlier. Moved to Backlog.

mvdan commented 2 years ago

I still feel the pain of https://github.com/golang/go/issues/23439#issuecomment-582483567 regularly when helping people relatively new to Go :)

What was the work that https://go.dev/cl/232017 was missing to be finished? Perhaps with that information, someone else with a bit of experience in cmd/go could pick up the patch.

bcmills commented 2 years ago

From what I can tell, the remaining issues were described in this comment: https://go-review.googlesource.com/c/go/+/232017/4#message-75b3b75fc59c83ddca9fba768d9a7ced203f4c29

mvdan commented 2 years ago

This may be an infeasible approach, but... what about just teaching go env that it can show the most reasonable default for GOBIN when it's in module mode? Internally, none of the logic needs to change for now. It won't quite be right for case 3 that Jay mentioned (When cross-compiling, by default, we install to GOPATH/bin/goos_goarch), but we could always teach go env about it too.

I imagine swapping the default internally too might be easier when we drop GOPATH mode entirely, which hopefully isn't too far away at this point. Two out of the four remaining issues that Jay mentioned are in GOPATH mode.

bcmills commented 2 years ago

I don't think a halfway approach in go env is worth the churn. (It's too easy to end up with a random UX regression, and if we're going to deal with the random regressions I'd rather we do a complete fix.)

dolmen commented 2 months ago
$ go help install
usage: go install [build flags] [packages]

Install compiles and installs the packages named by the import paths.

Executables are installed in the directory named by the GOBIN environment
variable, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH
environment variable is not set. Executables in $GOROOT
are installed in $GOROOT/bin or $GOTOOLDIR instead of $GOBIN.

So it looks like this issue has been resolved, isn't it?

What about going forward and exposing GOBIN (#68102)?

matloob commented 2 months ago

@dolmen This issue is still unresolved. We don't set GOBIN to $GOPATH/bin, but instead fall back from the value in GOBIN to $GOPATH/bin if GOBIN is unset. So the differences in behavior blocking Jay's CL are still there.

On the other hand, I don't know if this issue is worth fixing at this point. It's not really a problem in module mode. Most users don't set (and shouldn't set) GOBIN and instead just use $GOPATH/bin in their PATHs.

@dolmen What's the use case for your tool? Can you use the same logic we use? (use $GOPATH/bin unless $GOBIN is set)