-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: build panics or prints warning twice when import path pattern matches 0 packages #8165
Labels
Milestone
Comments
The panic is very easy to fix, that panic stack trace points directly to the problem (`p := pkgs[0]` is done, but there's no check if `len(pkgs) == 0`). However, I'm not sure what's a better correct behavior for `go build -o ./out ...does_not_exist`. There are two choices. Either a warning and no output like the other commands. Alternatively, maybe the best thing to do for `go build -o ./out ...does_not_exist` is the same as for `go build -o ./out ...matches_multiple_pkgs`, which is: fatalf("go build: cannot use -o with multiple packages") It's a design decision of, if the user specifies -o option, should the command always write _some_ output and fail with error if it can't (i.e. either matches multiple packages, or no packages, or build error, etc.). Or should it write output if pattern matches one package, but do nothing when pattern matches no packages. Thinking about it more, I think I would prefer it to be more consistent and fail if pattern matches multiple or 0 packages, which can be done by changing: if len(pkgs) > 1 { fatalf("go build: cannot use -o with multiple packages") } to: if len(pkgs) != 1 { fatalf("go build: cannot use -o with multiple packages") } else if len(pkgs) < 1 { fatalf("go build: cannot use -o with no packages") } Or something along those lines. That way, if someone has a script that executes `go build -o binary_name ...some_pkg`, that command will only return success status if ./binary_name is successfully written, and hence it's more reliable. |
The panic is very easy to fix, that panic stack trace points directly to the problem (`p := pkgs[0]` is done, but there's no check if `len(pkgs) == 0`). However, I'm not sure what's a better correct behavior for `go build -o ./out ...does_not_exist`. There are two choices. Either a warning and no output like the other commands. Alternatively, maybe the best thing to do for `go build -o ./out ...does_not_exist` is the same as for `go build -o ./out ...matches_multiple_pkgs`, which is: fatalf("go build: cannot use -o with multiple packages") It's a design decision of, if the user specifies -o option, should the command always write _some_ output and fail with error if it can't (i.e. either matches multiple packages, or no packages, or build error, etc.). Or should it write output if pattern matches one package, but do nothing when pattern matches no packages. Thinking about it more, I think I would prefer it to be more consistent and fail if pattern matches multiple or 0 packages, which can be done by changing: if len(pkgs) > 1 { fatalf("go build: cannot use -o with multiple packages") } to: if len(pkgs) > 1 { fatalf("go build: cannot use -o with multiple packages") } else if len(pkgs) < 1 { fatalf("go build: cannot use -o with no packages") } Or something along those lines. That way, if someone has a script that executes `go build -o binary_name ...some_pkg`, that command will only return success status if ./binary_name is successfully written, and hence it's more consistent and reliable. |
CL https://golang.org/cl/107140043 mentions this issue. |
This issue was closed by revision 3e80141. Status changed to Fixed. |
wheatman
pushed a commit
to wheatman/go-akaros
that referenced
this issue
Jun 25, 2018
Fixes golang#8165. After this change, the panic is replaced by a message: $ go build -o out ...doesntexist warning: "...doesntexist" matched no packages no packages to build The motivation to return 1 exit error code is to allow -o flag to be used to guarantee that the output binary is written to when exit status is 0. If someone uses an import path pattern to specify a single package and suddenly that matches no packages, it's better to return exit code 1 instead of silently doing nothing. This is consistent with the case when -o flag is given and multiple packages are matched. It's also somewhat consistent with the current behavior with the panic, except that gave return code 2. But it's similar in that it's also non-zero (indicating failure). I've changed the language to be similar to output of go test when an import path pattern matches no packages (it also has a return status of 1): $ go test ...doesntexist warning: "...doesntexist" matched no packages no packages to test LGTM=adg R=golang-codereviews, josharian, gobot, adg CC=golang-codereviews https://golang.org/cl/107140043
wheatman
pushed a commit
to wheatman/go-akaros
that referenced
this issue
Jul 9, 2018
Fixes golang#8165. After this change, the panic is replaced by a message: $ go build -o out ...doesntexist warning: "...doesntexist" matched no packages no packages to build The motivation to return 1 exit error code is to allow -o flag to be used to guarantee that the output binary is written to when exit status is 0. If someone uses an import path pattern to specify a single package and suddenly that matches no packages, it's better to return exit code 1 instead of silently doing nothing. This is consistent with the case when -o flag is given and multiple packages are matched. It's also somewhat consistent with the current behavior with the panic, except that gave return code 2. But it's similar in that it's also non-zero (indicating failure). I've changed the language to be similar to output of go test when an import path pattern matches no packages (it also has a return status of 1): $ go test ...doesntexist warning: "...doesntexist" matched no packages no packages to test LGTM=adg R=golang-codereviews, josharian, gobot, adg CC=golang-codereviews https://golang.org/cl/107140043
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: