golang/go · error

loading cached file %s: %w

Error message

loading cached file %s: %w

What it means

On the build-cache hit path, findCachedObjdirFile calls cache.GetFile to restore an intermediate object-directory file (e.g. a generated _cgo_*.go) keyed by a subkey of the action ID. If the cache lookup fails, the file's logical name and the wrapped error are returned. The cause is usually a missing, corrupted, or inaccessible cache entry.

Source

Thrown at src/cmd/go/internal/work/exec.go:1024

		return b.Shell(a).reportCmd("", "", msg, err)
	}
	return nil
}

func (b *Builder) cacheObjdirFile(a *Action, c cache.Cache, name string) error {
	f, err := os.Open(a.Objdir + name)
	if err != nil {
		return err
	}
	defer f.Close()
	_, _, err = c.Put(cache.Subkey(a.actionID, name), f)
	return err
}

func (b *Builder) findCachedObjdirFile(a *Action, c cache.Cache, name string) (string, error) {
	file, _, err := cache.GetFile(c, cache.Subkey(a.actionID, name))
	if err != nil {
		return "", fmt.Errorf("loading cached file %s: %w", name, err)
	}
	return file, nil
}

func (b *Builder) loadCachedObjdirFile(a *Action, c cache.Cache, name string) error {
	cached, err := b.findCachedObjdirFile(a, c, name)
	if err != nil {
		return err
	}
	return b.Shell(a).CopyFile(a.Objdir+name, cached, 0666, true)
}

func (b *Builder) cacheSrcFiles(a *Action, srcfiles []string) {
	c := cache.Default()
	var buf bytes.Buffer
	for _, file := range srcfiles {
		if !strings.HasPrefix(file, a.Objdir) {
			// not generated

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Run `go clean -cache` and rebuild
  2. Verify GOCACHE points to a writable local directory with free space
  3. Ensure no concurrent go build processes write the same cache
  4. Check filesystem permissions on the cache directory

Example fix

// before (stale/corrupt cache)
go build ./...
// after
go clean -cache && go build ./...
Defensive patterns

Strategy: retry

Validate before calling

// Verify the cache dir is writable and has space before building
cacheDir := os.Getenv("GOCACHE")
if fi, err := os.Stat(cacheDir); err != nil || !fi.IsDir() {
    return fmt.Errorf("GOCACHE unavailable: %v", err)
}

Try / catch

// On a cache-read error, clean and retry once
out, err := cmd.CombinedOutput()
if err != nil && bytes.Contains(out, []byte("loading cached file")) {
    exec.Command("go", "clean", "-cache").Run()
    out, err = cmd.CombinedOutput()
}

Prevention

When it happens

Trigger: Fires in findCachedObjdirFile when cache.GetFile(c, cache.Subkey(a.actionID, name)) returns an error, during a cache-hit rebuild that needs to restore objdir artifacts.

Common situations: Cache corruption from concurrent builds sharing one GOCACHE, a full disk, eviction mid-build, a GOCACHE path on a flaky network filesystem, or stale permissions on the cache directory.

Related errors


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