-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: document that go mod download -json uses exit code 1 when at least one JSON entry contains non-zero Error #35380
Comments
I checked scenario 3 just now, and the error it emits goes to stderr. Its stdout is empty, which is a valid sequence of JSON objects (and so it will not produce an error when trying to JSON decode it). |
I guess the remaining usability problem is that it's still necessary for caller to parse as JSON even when exit code is 1. And exit code 1 may mean "there are JSON objects to be read and processed", or it could mean "there was a fatal error that prevented go mod download from starting", and it's not easy to tell those apart. |
Related: #34485. |
@dmitshur, the easy way to tell those two cases apart should be to check whether the output to |
I wanted to follow up here with an update. After reading the relevant source code very closely: go/src/cmd/go/internal/modcmd/download.go Lines 160 to 171 in d2de9bd
I was able to actually simplify the code that consumed output from While I still think a different behavior when |
I think returning exit status The reason it is the right thing to do is that, in case of A counter example is
Another counter example is my wrapper of |
go mod download -json
documentation says:What did you do?
I tried to execute
go mod download -json
and interpret its results programmatically.What did you expect to see?
I expected non-zero exit code to mean a fatal problem occurred and that I should not expect valid JSON output.
What did you see instead?
There are 3 possible scenarios:
When there are no errors,
go mod download -json
emits exit code 0 and prints JSON.When there are errors downloading some modules,
go mod download -json
emits exit code 1 and prints JSON that has some entries with non-empty Error fields.Example
An example of such an error is when GOPROXY is set to off; it results in "module lookup disabled by GOPROXY=off" errors:
When there are some other type of errors,
go mod download -json
emits exit code 1 and does not print JSON.Example
Examples of other errors include a syntax error in the current go.mod file, or a replace directive pointing to a directory without a go.mod file.
In two of the scenarios the exit code is 1. One of them emits JSON. That is challenging for a tool that needs to parse the output; it's hard to be sure it's safe to expect/parse JSON on stdout when exit code is non-zero.
/cc @jayconrod @bcmills @marwan-at-work
The text was updated successfully, but these errors were encountered: