From 70d803c1ff3073e7c660f7de4a22cf552c1ab509 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Wed, 28 Feb 2024 12:34:48 -0800 Subject: [PATCH] Make `make` fail better with bad inputs Ultimately, we're hitting places where bash is just hard to get right. Instead of: ``` $ make WHAT=nonexist stat /home/thockin/src/kubernetes/nonexist: directory not found !!! [0228 12:07:12] Call tree: !!! [0228 12:07:12] 1: /home/thockin/src/kubernetes/hack/lib/golang.sh:930 kube::golang::normalize_go_targets(...) !!! [0228 12:07:12] 2: hack/make-rules/build.sh:27 kube::golang::build_binaries(...) +++ [0228 12:07:12] Building go targets for linux/amd64 (non-static) no Go files in /home/thockin/src/kubernetes !!! [0228 12:07:12] Call tree: !!! [0228 12:07:12] 1: /home/thockin/src/kubernetes/hack/lib/golang.sh:820 kube::golang::build_some_binaries(...) !!! [0228 12:07:12] 2: /home/thockin/src/kubernetes/hack/lib/golang.sh:974 kube::golang::build_binaries_for_platform(...) !!! [0228 12:07:12] 3: hack/make-rules/build.sh:27 kube::golang::build_binaries(...) !!! [0228 12:07:12] Call tree: !!! [0228 12:07:12] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...) !!! [0228 12:07:12] Call tree: !!! [0228 12:07:12] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...) make: *** [Makefile:95: all] Error 1 ``` We now get: ``` $ make WHAT=nonexist +++ [0228 12:33:49] Building go targets for linux/amd64 ./nonexist (non-static) stat /home/thockin/src/kubernetes/nonexist: directory not found !!! [0228 12:33:49] Call tree: !!! [0228 12:33:49] 1: /home/thockin/src/kubernetes/hack/lib/golang.sh:823 kube::golang::build_some_binaries(...) !!! [0228 12:33:49] 2: /home/thockin/src/kubernetes/hack/lib/golang.sh:976 kube::golang::build_binaries_for_platform(...) !!! [0228 12:33:49] 3: hack/make-rules/build.sh:27 kube::golang::build_binaries(...) !!! [0228 12:33:49] Call tree: !!! [0228 12:33:49] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...) !!! [0228 12:33:49] Call tree: !!! [0228 12:33:49] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...) make: *** [Makefile:95: all] Error 1 $ make test WHAT=nonexist +++ [0228 12:33:53] Set GOMAXPROCS automatically to 6 +++ [0228 12:33:53] Running tests without code coverage and with -race stat /home/thockin/src/kubernetes/nonexist: directory not found make: *** [Makefile:190: test] Error 1 ``` --- hack/lib/golang.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/hack/lib/golang.sh b/hack/lib/golang.sh index bd4be75a6ca..81e054efeaa 100755 --- a/hack/lib/golang.sh +++ b/hack/lib/golang.sh @@ -417,6 +417,9 @@ kube::golang::best_guess_go_targets() { # be Go-style names (e.g. example.com/foo/bar or ./foo/bar) or just local paths # (e.g. foo/bar) and produces a respective list (on stdout) of Go package # names. +# +# If this cannot find (go list -find -e) one or more inputs, it will emit the +# them on stdout, so callers can at least get a useful error. kube::golang::normalize_go_targets() { local targets=() kube::util::read-array targets < <(kube::golang::best_guess_go_targets "$@") @@ -430,7 +433,7 @@ kube::golang::normalize_go_targets() { local tst tst="$(basename "${target}")" local pkg - pkg="$(go list -find "${dir}")" + pkg="$(go list -find -e "${dir}")" echo "${pkg}/${tst}" continue fi @@ -438,11 +441,11 @@ kube::golang::normalize_go_targets() { local dir dir="$(dirname "${target}")" local pkg - pkg="$(go list -find "${dir}")" + pkg="$(go list -find -e "${dir}")" echo "${pkg}/..." continue fi - go list -find "${target}" + go list -find -e "${target}" done }