// Package gotoolchain pins the offline Go trees shared by checks and specialists. package gotoolchain import ( "crypto/sha256" "encoding/hex" "fmt" "io" "io/fs" "os" "strconv" ) func Target(path string) bool { return path == "/opt/go" || path == "/opt/go-mod" } // Environment is the shared offline Go environment. MAKEFLAGS passes // GOTOOLCHAIN=local to make as a command-line variable: a Makefile that // assigns GOTOOLCHAIN (export GOTOOLCHAIN := go1.x) overrides the // environment but not the command line, and its go commands would // otherwise ask for a release that only the network has. func Environment(provider ...string) map[string]string { tmp := "/tmp" if len(provider) > 0 && provider[0] == "claude" { tmp += "/claude-" + strconv.Itoa(os.Getuid()) } env := map[string]string{"GOCACHE": tmp + "/go-build", "GOTMPDIR": tmp + "/go-tmp", "GOMODCACHE": "/opt/go-mod", "GOROOT": "/opt/go", "GOTOOLCHAIN": "local", "MAKEFLAGS": "GOTOOLCHAIN=local", "GOWORK": "off", "GOENV": "off", "GOFLAGS": "", "GOTELEMETRY": "off", "GOPROXY": "off", "GOSUMDB": "off", "CGO_ENABLED": "0", "PATH": "/opt/go/bin:/usr/bin:/bin:/opt"} if len(provider) > 0 && provider[0] == "claude" { env["CLAUDE_CODE_TMPDIR"] = "/tmp" } if len(provider) == 0 || provider[0] == "" { env["XDG_CONFIG_HOME"] = "/tmp/xdg" env["PATH"] = "/opt/go/bin:/usr/bin:/bin" } return env } // Digest includes every path, mode and file byte. No symlink or special file // can introduce content outside the approved Go root. The caller retains the // root descriptor and reuses this digest for the launch. func Digest(root string) (string, error) { dir, err := os.OpenRoot(root) if err != nil { return "", err } defer dir.Close() h := sha256.New() err = fs.WalkDir(dir.FS(), ".", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } info, err := d.Info() if err != nil { return err } fmt.Fprintf(h, "%s\x00%o\x00", path, info.Mode()) if d.IsDir() { return nil } if !info.Mode().IsRegular() { return fmt.Errorf("go tree contains a non-regular entry: %s", path) } f, err := dir.Open(path) if err != nil { return err } sum := sha256.New() _, err = io.Copy(sum, f) closeErr := f.Close() if err != nil { return err } if closeErr != nil { return closeErr } fmt.Fprintf(h, "%x\n", sum.Sum(nil)) return nil }) if err != nil { return "", err } return hex.EncodeToString(h.Sum(nil)), nil }