golang / go

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

cmd/go: should `go list -json` imply `-e`? #30755

Open marwan-at-work opened 5 years ago

marwan-at-work commented 5 years ago

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

$ go version
go 1.12 darwin

Does this issue reproduce with the latest release?

Yes

What did you do?

GO111MODULE=on go list -m -versions -json github.com/golang/nonexistentrepo@latest

What did you expect to see?

A valid json output with a clear error in the Error field. Something similar to when go mod download has an error as such:

{
  "Error": "repository not found"
}

What did you see instead?

A non json output as such (modified):

go list -m github.com/golang/nonexistentrepo: git ls-remote -q origin in /Users/me/go/pkg/mod/cache/vcs/xxx-long-sha: exit status 128:
    remote: Repository not found.
    fatal: repository 'https://github.com/golang/nonexistentrepo/' not found
bcmills commented 5 years ago

Is that on stdout, or on stderr?

(See previously https://github.com/golang/go/issues/26218#issuecomment-402916995.)

CC @jayconrod

marwan-at-work commented 5 years ago

@bcmills stderr I believe. Regarding the comment you linked, with the same exact "error" go mod download has an almost opposite behavior: nothing logged to stderr, and you get a valid json with an Error field inside of it. Though the error field is inaccurate (different, but related issue).

For example

GO111MODULE=on go mod download -json github.com/golang/nonexistentrepo@latest

returns

{
    "Path": "github.com/golang/nonexistentrepo",
    "Version": "latest",
    "Error": "invalid version \"latest\""
}
bcmills commented 5 years ago

If the non-JSON output is going to stderr, then the behavior may be unfortunate but it is not a bug. Tools that expect structured output must separate stdout from stderr.

go list -e -m -versions -json github.com/golang/nonexistentrepo@latest should certainly suppress the stderr output and place the error in the Error field instead, but it isn't at all obvious to me whether that behavior is appropriate without the -e flag.

Does -json imply -e for other kinds of errors in general?

marwan-at-work commented 5 years ago

From a consistency standpoint, it makes sense to have go list -m -versions imply -e since go mod download -json certainly implies -e (even though it doesn't have an -e flag to begin with).

I'm not sure about other go commands so it might be worth a scan.

Thanks!

rsc commented 5 years ago

Most people using go list -json should also add -e. The first problem is that -e does not do everything it should right now. Let's fix that. I am not 100% sure that -json without -e is so useless that it should be disallowed entirely. It seems better to keep them orthogonal. Even if -json reported errors in JSON, you might still prefer to not say -e so that you can get an exit code about overall success.