shurcooL / binstale

binstale tells you whether the binaries in your GOPATH/bin are stale or up to date.
MIT License
146 stars 4 forks source link

No error/warning if $GOPATH is not set #2

Closed peggyl closed 8 years ago

peggyl commented 8 years ago

Suggestion: warn user or error if $GOPATH is not set. Otherwise, it defaults to "" (see https://golang.org/src/go/build/build.go#L293), and it's pretty unlikely that Go binaries exist in /bin/.

peggyli in ~ $ echo $GOPATH
/Users/peggyli/go
peggyli in ~ $ binstale binstale
  binstale
    up to date: github.com/shurcooL/binstale

peggyli in ~ $ unset GOPATH
peggyli in ~ $ echo $GOPATH

peggyli in ~ $ binstale binstale
warning: "binstale" matched no binaries in GOPATH/bin
dmitshur commented 8 years ago

Thanks for reporting this.

If your GOPATH is not set, that means you have 0 workspaces and it should not be looking anywhere. Definitely not in /bin/, so if it does that, it's a bug.

What does the go tool do? I'd rather mimic its behavior. I'll check...

peggyl commented 8 years ago

Oops, you're right; I was looking at the output (and assuming $GOPATH/bin => /bin), but the code seems to handle this correctly.

dmitshur commented 8 years ago

Okay, I don't think it looks in /bin/ when GOPATH is not set.

workspaces := filepath.SplitList(build.Default.GOPATH)
for _, workspace := range workspaces {

If build.Default.GOPATH is empty string, filepath.SplitList("") will return a slice with 0 elements, so it will not look anywhere. So that's great.

However, I tested how the go tool prints stuff when GOPATH is not set, and:

$ unset GOPATH
$ go list ...binstale
warning: "...binstale" matched no packages
$ go install github.com/shurcooL/binstale
can't load package: package github.com/shurcooL/binstale: cannot find package "github.com/shurcooL/binstale" in any of:
    /usr/local/go/src/github.com/shurcooL/binstale (from $GOROOT)
    ($GOPATH not set)

I could do something like the second output. Right now binstale just always says "GOPATH/bin" but not what the underlying dirs are (there are as many as you have workspaces).

dmitshur commented 8 years ago

Okay, the new output is a lot more like the output from go install:

~ $ binstale doesntexist
cannot find binary "doesntexist" in any of:
    /example/gopath0/bin/doesntexist (from $GOPATH)
    /example/gopath1/bin/doesntexist
    /example/gopath2/bin/doesntexist
~ $ binstale binstale
binstale
    up to date: github.com/shurcooL/binstale
~ $ unset GOPATH
~ $ binstale binstale
cannot find binary "binstale" in any of:
    ($GOPATH not set)
~ $ 

Similarly to go tool, it'll now exit with code 1 if any of the specified binaries are not found.