| 1 | // Package gotoolchain pins the offline Go trees shared by checks and specialists. |
| 2 | package gotoolchain |
| 3 | |
| 4 | import ( |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "io/fs" |
| 10 | "os" |
| 11 | "strconv" |
| 12 | ) |
| 13 | |
| 14 | func Target(path string) bool { return path == "/opt/go" || path == "/opt/go-mod" } |
| 15 | |
| 16 | // Environment is the shared offline Go environment. MAKEFLAGS passes |
| 17 | // GOTOOLCHAIN=local to make as a command-line variable: a Makefile that |
| 18 | // assigns GOTOOLCHAIN (export GOTOOLCHAIN := go1.x) overrides the |
| 19 | // environment but not the command line, and its go commands would |
| 20 | // otherwise ask for a release that only the network has. |
| 21 | func Environment(provider ...string) map[string]string { |
| 22 | tmp := "/tmp" |
| 23 | if len(provider) > 0 && provider[0] == "claude" { |
| 24 | tmp += "/claude-" + strconv.Itoa(os.Getuid()) |
| 25 | } |
| 26 | 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"} |
| 27 | if len(provider) > 0 && provider[0] == "claude" { |
| 28 | env["CLAUDE_CODE_TMPDIR"] = "/tmp" |
| 29 | } |
| 30 | if len(provider) == 0 || provider[0] == "" { |
| 31 | env["XDG_CONFIG_HOME"] = "/tmp/xdg" |
| 32 | env["PATH"] = "/opt/go/bin:/usr/bin:/bin" |
| 33 | } |
| 34 | return env |
| 35 | } |
| 36 | |
| 37 | // Digest includes every path, mode and file byte. No symlink or special file |
| 38 | // can introduce content outside the approved Go root. The caller retains the |
| 39 | // root descriptor and reuses this digest for the launch. |
| 40 | func Digest(root string) (string, error) { |
| 41 | dir, err := os.OpenRoot(root) |
| 42 | if err != nil { |
| 43 | return "", err |
| 44 | } |
| 45 | defer dir.Close() |
| 46 | h := sha256.New() |
| 47 | err = fs.WalkDir(dir.FS(), ".", func(path string, d fs.DirEntry, err error) error { |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | info, err := d.Info() |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | fmt.Fprintf(h, "%s\x00%o\x00", path, info.Mode()) |
| 56 | if d.IsDir() { |
| 57 | return nil |
| 58 | } |
| 59 | if !info.Mode().IsRegular() { |
| 60 | return fmt.Errorf("go tree contains a non-regular entry: %s", path) |
| 61 | } |
| 62 | f, err := dir.Open(path) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | sum := sha256.New() |
| 67 | _, err = io.Copy(sum, f) |
| 68 | closeErr := f.Close() |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if closeErr != nil { |
| 73 | return closeErr |
| 74 | } |
| 75 | fmt.Fprintf(h, "%x\n", sum.Sum(nil)) |
| 76 | return nil |
| 77 | }) |
| 78 | if err != nil { |
| 79 | return "", err |
| 80 | } |
| 81 | return hex.EncodeToString(h.Sum(nil)), nil |
| 82 | } |
| 83 | |