Skip to content

Commit a7e875c

Browse files
committed
cmd/buildlet: use inherited GOROOT_BOOTSTRAP as fallback
Currently the buildlet ignores any GOROOT_BOOTSTRAP defined on the host, assuming it'll always come from the coordinator. Relax that a bit to make it easier to migrate people to buildlet from the older builder. This doesn't change behavior for any existing builder. Updates golang/go#21191 Change-Id: I3c3ef77b073d7cecf41855e6d060773b8558935b Reviewed-on: https://go-review.googlesource.com/53492 Reviewed-by: Yuval Pavel Zholkover <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent cf481f4 commit a7e875c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

cmd/buildlet/buildlet.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,13 @@ func main() {
217217
}
218218
}
219219

220+
var inheritedGorootBootstrap string
221+
220222
func initGorootBootstrap() {
223+
// Remember any GOROOT_BOOTSTRAP to use as a backup in handleExec
224+
// if $WORKDIR/go1.4 ends up not existing.
225+
inheritedGorootBootstrap = os.Getenv("GOROOT_BOOTSTRAP")
226+
221227
// Default if not otherwise configured in dashboard/builders.go:
222228
os.Setenv("GOROOT_BOOTSTRAP", filepath.Join(*workDir, "go1.4"))
223229

@@ -845,6 +851,13 @@ func handleExec(w http.ResponseWriter, r *http.Request) {
845851

846852
env := append(baseEnv(goarch), r.PostForm["env"]...)
847853
env = envutil.Dedup(runtime.GOOS == "windows", env)
854+
855+
// Prefer buildlet process's inherited GOROOT_BOOTSTRAP if
856+
// there was one and the one we're about to use doesn't exist.
857+
if v := getEnv(env, "GOROOT_BOOTSTRAP"); v != "" && inheritedGorootBootstrap != "" && pathNotExist(v) {
858+
env = envutil.Dedup(runtime.GOOS == "windows", append(env,
859+
"GOROOT_BOOTSTRAP="+inheritedGorootBootstrap))
860+
}
848861
env = setPathEnv(env, r.PostForm["path"], *workDir)
849862

850863
cmd := exec.Command(absCmd, r.PostForm["cmdArg"]...)
@@ -890,6 +903,32 @@ func handleExec(w http.ResponseWriter, r *http.Request) {
890903
log.Printf("[%p] Run = %s, after %v", cmd, state, time.Since(t0))
891904
}
892905

906+
// pathNotExist reports whether path does not exist.
907+
func pathNotExist(path string) bool {
908+
_, err := os.Stat(path)
909+
return os.IsNotExist(err)
910+
}
911+
912+
func getEnv(env []string, key string) string {
913+
for _, kv := range env {
914+
if len(kv) <= len(key) || kv[len(key)] != '=' {
915+
continue
916+
}
917+
if runtime.GOOS == "windows" {
918+
// Case insensitive.
919+
if strings.EqualFold(kv[:len(key)], key) {
920+
return kv[len(key)+1:]
921+
}
922+
} else {
923+
// Case sensitive.
924+
if kv[:len(key)] == key {
925+
return kv[len(key)+1:]
926+
}
927+
}
928+
}
929+
return ""
930+
}
931+
893932
// setPathEnv returns a copy of the provided environment with any existing
894933
// PATH variables replaced by the user-provided path.
895934
// These substitutions are applied to user-supplied path elements:

0 commit comments

Comments
 (0)