golang/go · error

writing metafiles file: %v

Error message

writing metafiles file: %v

What it means

After serializing the coverage meta-files collection, WriteCoverMetaFilesFile creates the action's objdir and writes the JSON to <objdir>/<MetaFilesFileName> via sh.writeFile. If the write fails (disk full, permission denied, missing parent dir, path too long), the error is wrapped and returned.

Source

Thrown at src/cmd/go/internal/work/cover.go:168

		collection.MetaFileFragments = append(collection.MetaFileFragments, metaFilesFile)
	}

	// Serialize it.
	data, err := json.Marshal(collection)
	if err != nil {
		return fmt.Errorf("marshal MetaFileCollection: %v", err)
	}
	data = append(data, '\n') // makes -x output more readable

	// Create the directory for this action's objdir and
	// then write out the serialized collection
	// to a file in the directory.
	if err := sh.Mkdir(a.Objdir); err != nil {
		return err
	}
	mfpath := a.Objdir + coverage.MetaFilesFileName
	if err := sh.writeFile(mfpath, data); err != nil {
		return fmt.Errorf("writing metafiles file: %v", err)
	}

	// We're done.
	return nil
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Check disk space and write permissions on TMPDIR and GOCACHE: 'df -h', 'touch <tmp>/x'.
  2. Move GOCACHE to a larger, writable local directory.
  3. Run 'go clean -cache' to reset a possibly corrupted objdir tree.
  4. On Windows, shorten module paths or enable long-path support.

Example fix

// before
$ go test -coverpkg=./... ./...
// error: writing metafiles file: open /tmp/.../b001/metafiles: no space left on device

// after
$ export GOCACHE=$HOME/.cache/go-build
$ export TMPDIR=$HOME/tmp
$ go test -coverpkg=./... ./...
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight: confirm TMPDIR/GOCACHE are writable with free space.
func preflightCache() error {
    for _, d := range []string{os.Getenv("GOCACHE"), os.Getenv("TMPDIR")} {
        if d == "" { continue }
        var stat syscall.Statfs_t
        if err := syscall.Statfs(d, &stat); err != nil { return err }
        if stat.Bavail*uint64(stat.Bsize) < 1<<30 {
            return fmt.Errorf("%s has < 1 GiB free", d)
        }
    }
    return nil
}

Prevention

When it happens

Trigger: Running 'go test -coverpkg' where the build objdir lives on a full or read-only filesystem, or where permission to create files is missing. The earlier sh.Mkdir succeeded, so the failure is specifically in the file write step.

Common situations: TMPDIR/GOCACHE on a small tmpfs; CI container with read-only mounted volume; SELinux denying writes; concurrent build evicting the objdir mid-write; path length limits on Windows.

Related errors


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