golang/go · error

error adding target to cache: %w

Error message

error adding target to cache: %w

What it means

After successfully opening the preprocessed PGO target, pgoActor.Act calls cache.Default().Put to store it under the action ID. If the cache's Put returns an error (disk full, cache dir unwritable, hash mismatch), it is wrapped and returned.

Source

Thrown at src/cmd/go/internal/work/action.go:519

	a.built = a.Target

	if !cfg.BuildN {
		// Cache the output.
		//
		// N.B. We don't use updateBuildID here, as preprocessed PGO profiles
		// do not contain a build ID. updateBuildID is typically responsible
		// for adding to the cache, thus we must do so ourselves instead.

		r, err := os.Open(a.Target)
		if err != nil {
			return fmt.Errorf("error opening target for caching: %w", err)
		}

		c := cache.Default()
		outputID, _, err := c.Put(a.actionID, r)
		r.Close()
		if err != nil {
			return fmt.Errorf("error adding target to cache: %w", err)
		}
		if cfg.BuildX {
			sh.ShowCmd("", "%s # internal", joinUnambiguously(str.StringList("cp", a.Target, c.OutputFile(outputID))))
		}
	}

	return nil
}

type coverProvider struct {
	// name of static metadata file fragment emitted by the cover
	// tool as part of the package cover action, for selected
	// "go test -cover" runs.
	covMetaFileName string

	// coverageConfig is the path to the json-serialized covcmd.CoverPkgConfig
	// provided to the cover tool. The config is created by coverConfig.
	coverageConfig string

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Run 'go clean -cache' to reset the cache index.
  2. Point GOCACHE at a directory on a volume with adequate free space and write permission.
  3. Check 'df -h' and 'quota' for the cache volume.
  4. If reproducible, run with GODEBUG=gocachehash=1 to diagnose cache write failures.

Example fix

// before
$ go build -pgoprofile=cpu.pprof ./...
// error: error adding target to cache: write /small-tmpfs/...: no space left on device

// after
$ export GOCACHE=$HOME/.cache/go-build
$ go clean -cache
$ go build -pgoprofile=cpu.pprof ./...
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the cache dir is writable and has space before building.
func checkCacheWritable(dir string) error {
    f, err := os.CreateTemp(dir, ".probe")
    if err != nil { return fmt.Errorf("cache not writable: %w", err) }
    _ = f.Close()
    _ = os.Remove(f.Name())
    return nil
}

Prevention

When it happens

Trigger: PGO profile caching fails due to GOCACHE being read-only, full, on a corrupted index, or hit by an OS-level quota. Distinct from 1167: the file was opened fine, the cache write itself failed.

Common situations: CI containers with small tmpfs for GOCACHE; user ran 'chmod -R a-w' on the cache; disk quota exceeded; concurrent cache eviction racing with the Put.

Related errors


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