From 75fc29f3efe9b1e8fc677dc1bc9e747292918c0f Mon Sep 17 00:00:00 2001 From: "zuoxiu.jm" <291271447@qq.com> Date: Tue, 12 Mar 2019 18:04:14 +0800 Subject: [PATCH] ensure GOROOT env var on running openapi-gen --- cmd/apiserver-boot/boot/build/generate.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cmd/apiserver-boot/boot/build/generate.go b/cmd/apiserver-boot/boot/build/generate.go index 587a88010a..339f09785f 100644 --- a/cmd/apiserver-boot/boot/build/generate.go +++ b/cmd/apiserver-boot/boot/build/generate.go @@ -22,6 +22,7 @@ import ( "log" "os" "os/exec" + "path" "path/filepath" "regexp" "strings" @@ -29,7 +30,7 @@ import ( "github.com/kubernetes-incubator/apiserver-builder-alpha/cmd/apiserver-boot/boot/util" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/util/sets" - "path" + "k8s.io/klog" ) var versionedAPIs []string @@ -189,6 +190,19 @@ func RunGenerate(cmd *cobra.Command, args []string) { "--report-filename", "violations.report", "--output-package", filepath.Join(util.Repo, "pkg", "openapi"))..., ) + + // HACK: ensure GOROOT env var + c.Env = os.Environ() + if len(os.Getenv("GOROOT")) == 0 { + if p, err := exec.Command("which", "go").CombinedOutput(); err == nil { + // The returned string will have some/path/bin/go, so remove the last two elements. + c.Env = append(c.Env, + fmt.Sprintf("GOROOT=%s", filepath.Dir(filepath.Dir(strings.Trim(string(p), "\n"))))) + } else { + klog.Warningf("Warning: $GOROOT not set, and unable to run `which go` to find it: %v\n", err) + } + } + fmt.Printf("%s\n", strings.Join(c.Args, " ")) out, err := c.CombinedOutput() if err != nil {