golang/go · error

GOROOT not found

Error message

GOROOT not found

What it means

Returned by findGoroot in misc/go_android_exec/main.go when both runtime.GOROOT() returns empty (binary built with -trimpath or GOROOT unset at build time) and the fallback `go env GOROOT` also returns empty. Without a GOROOT path, the tool cannot locate bin/go to drive the cross-build, so it aborts.

Source

Thrown at misc/go_android_exec/main.go:508

		gorootPath = runtime.GOROOT()
		if gorootPath != "" {
			return
		}

		// runtime.GOROOT is empty — perhaps go_android_exec was built with
		// -trimpath and GOROOT is unset. Try 'go env GOROOT' as a fallback,
		// assuming that the 'go' command in $PATH is the correct one.

		cmd := exec.Command("go", "env", "GOROOT")
		cmd.Stderr = os.Stderr
		out, err := cmd.Output()
		if err != nil {
			gorootErr = fmt.Errorf("%v: %w", cmd, err)
		}

		gorootPath = string(bytes.TrimSpace(out))
		if gorootPath == "" {
			gorootErr = errors.New("GOROOT not found")
		}
	})

	return gorootPath, gorootErr
}

func goTool() (string, error) {
	goroot, err := findGoroot()
	if err != nil {
		return "", err
	}
	return filepath.Join(goroot, "bin", "go"), nil
}

var (
	gorootOnce sync.Once
	gorootPath string
	gorootErr  error

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Ensure a Go toolchain is installed and `go` is on $PATH: `which go` must succeed before running go_android_exec.
  2. Set the GOROOT environment variable explicitly: export GOROOT=/usr/local/go (or the install path).
  3. Rebuild go_android_exec without -trimpath so runtime.GOROOT() returns the build-time path.
  4. If using a version manager (asdf/gvm), activate the Go shim in the same shell that runs go_android_exec.

Example fix

# before
go run misc/go_android_exec/main.go # binary built -trimpath, no go on PATH -> throws

# after
export PATH="/usr/local/go/bin:$PATH"
export GOROOT="$(go env GOROOT)"
go run misc/go_android_exec/main.go
Defensive patterns

Strategy: validation

Validate before calling

if goroot, err := exec.Command("go", "env", "GOROOT").Output(); err != nil || strings.TrimSpace(string(goroot)) == "" {
  log.Fatal("GOROOT not found: install Go or set GOROOT env var")
}

Prevention

When it happens

Trigger: go_android_exec binary built with -trimpath and no `go` on $PATH; running in an environment where the `go` binary is absent or not on PATH (stripped container, minimal docker image); PATH misconfigured so `go env GOROOT` fails or returns empty.

Common situations: CI image that ships go_android_exec built with -trimpath but does not include a go toolchain on PATH; distroless containers; developers using asdf/gvm where the go shim is not active in the subprocess; running the binary standalone outside a Go installation.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/54e076202c7779f0. Report an issue: GitHub.