golang/go · error

corrupt archive

Error message

corrupt archive

What it means

Defined as errCorruptArchive in cmd/internal/archive and returned by the archive reader when a Unix-style ar archive (the container format used by Go object files, static .a libraries) has a structurally invalid header — e.g. a malformed entry header, a bad magic, or a name/size field that does not parse. The Go linker and cmd/go read these archives when linking; this error means the file is not a valid ar container at all (as opposed to truncated, which is a different sentinel).

Source

Thrown at src/cmd/internal/archive/archive.go:104

type GoObj struct {
	TextHeader []byte
	Arch       string
	Data
}

const (
	entryHeader = "%s%-12d%-6d%-6d%-8o%-10d`\n"
	// In entryHeader the first entry, the name, is always printed as 16 bytes right-padded.
	entryLen   = 16 + 12 + 6 + 6 + 8 + 10 + 1 + 1
	timeFormat = "Jan _2 15:04 2006"
)

var (
	archiveHeader = []byte("!<arch>\n")
	archiveMagic  = []byte("`\n")
	goobjHeader   = []byte("go objec") // truncated to size of archiveHeader

	errCorruptArchive   = errors.New("corrupt archive")
	errTruncatedArchive = errors.New("truncated archive")
	errCorruptObject    = errors.New("corrupt object file")
	errNotObject        = errors.New("unrecognized object file format")
)

type ErrGoObjOtherVersion struct{ magic []byte }

func (e ErrGoObjOtherVersion) Error() string {
	return fmt.Sprintf("go object of a different version: %q", e.magic)
}

// An objReader is an object file reader.
type objReader struct {
	a      *Archive
	b      *bio.Reader
	err    error
	offset int64
	limit  int64

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Run `go clean -cache` to purge the build cache and rebuild from scratch.
  2. If reading a specific .a file, verify it with `ar t file.a` (or `file file.a`) — if that also fails, the file is not a valid archive.
  3. Recompile the package producing the bad archive from source to regenerate it.
  4. Check disk health and disable any concurrent processes that write into GOPATH/pkg or the build cache.

Example fix

# before
$ go build ./...
# -> corrupt archive

# after
$ go clean -cache
$ go build ./...
Defensive patterns

Strategy: retry

Validate before calling

// Cheap sanity check before passing a file to the archive reader:
func looksLikeArchive(path string) error {
    f, err := os.Open(path); if err != nil { return err }
    defer f.Close()
    var hdr [8]byte
    if _, err := io.ReadFull(f, hdr[:]); err != nil { return err }
    if string(hdr[:]) != "!<arch>\n" { return errors.New("not an ar archive") }
    return nil
}

Type guard

func isArArchive(path string) bool { return looksLikeArchive(path) == nil }

Try / catch

// On corrupt/truncated archive errors, purge the build cache entry and retry once.
if err != nil && (errors.Is(err, archive.ErrCorruptArchive) || strings.Contains(err.Error(), "corrupt archive")) {
    _ = os.Remove(cachePath)
    // rebuild
}

Prevention

When it happens

Trigger: Reading a file as a Go object archive when it is actually something else (a random binary, an ELF executable, a text file). An archive whose "!<arch>\n" magic was corrupted or whose entry padding/magic backtick-newline is wrong. A truncated-then-overwritten file produced by a failed previous build.

Common situations: Build cache corruption from concurrent writers, disk full, or system crash mid-write. Manually editing or stripping object files. A third-party tool that writes .a files with a non-standard ar variant (e.g. GNU vs BSD long-name handling). Mixing object files from incompatible Go versions in rare cache-key collisions.

Related errors


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