{"record":{"id":"f16b45d91fa9c5b9","repo":"golang/go","slug":"corrupt-single-package-index","errorCode":null,"errorMessage":"corrupt single-package index","messagePattern":"corrupt single-package index","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modindex/read.go","lineNumber":345,"sourceCode":"\t\treturn nil, errCorrupt\n\t}\n\n\tm = &Module{\n\t\tmoddir,\n\t\td,\n\t\tn,\n\t}\n\treturn m, nil\n}\n\n// packageFromBytes returns a *IndexPackage given the encoded representation.\nfunc packageFromBytes(modroot string, data []byte) (p *IndexPackage, err error) {\n\tm, err := fromBytes(modroot, data)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif m.n != 1 {\n\t\treturn nil, fmt.Errorf(\"corrupt single-package index\")\n\t}\n\treturn m.pkg(0), nil\n}\n\n// pkgDir returns the dir string of the i'th package in the index.\nfunc (m *Module) pkgDir(i int) string {\n\tif i < 0 || i >= m.n {\n\t\tpanic(errCorrupt)\n\t}\n\treturn m.d.stringAt(12 + 8 + 8*i)\n}\n\n// pkgOff returns the offset of the data for the i'th package in the index.\nfunc (m *Module) pkgOff(i int) int {\n\tif i < 0 || i >= m.n {\n\t\tpanic(errCorrupt)\n\t}\n\treturn m.d.intAt(12 + 8 + 8*i + 4)","sourceCodeStart":327,"sourceCodeEnd":363,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modindex/read.go#L327-L363","documentation":"Returned by packageFromBytes when the encoded index blob parsed successfully (fromBytes did not return errCorrupt) but the package count field m.n is not exactly 1. packageFromBytes is the single-package accessor and is only valid on blobs that index exactly one package; any other count means the file is logically corrupt for this API.","triggerScenarios":"Calling packageFromBytes on a multi-package index file or a zero-package file. Indicates a mismatch between the caller's expectation and the index writer, or a corrupt cache entry that fromBytes did not structurally catch.","commonSituations":"Index file partially regenerated, version skew between a `go` binary that wrote the index and one that reads it, or third-party tampering with the cache.","solutions":["Delete the affected module's index entry: `go clean -modcache`.","Re-download the module: `go mod download <module>`.","Ensure the same Go toolchain version writes and reads the cache (avoid mixing toolchain versions against one GOMODCACHE)."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"// Prefer Module.Walk / Module.Package instead of packageFromBytes for arbitrary blobs.\n// If you must read a single-package blob, validate the count first.\nfunc safeSinglePackage(modroot string, data []byte) (*modindex.IndexPackage, error) {\n    m, err := modindex.FromBytes(modroot, data)\n    if err != nil { return nil, err }\n    // (illustrative — Module.n is internal; this mirrors the runtime check)\n    return m.Package(\".\") // returns a not-found style error rather than corrupt-single-package\n}","typeGuard":null,"tryCatchPattern":"// packageFromBytes errors are fatal-cache; refresh and retry once.\npkg, err := modindex.PackageFromBytes(modroot, data)\nif err != nil && strings.Contains(err.Error(), \"corrupt single-package index\") {\n    _ = os.Remove(indexPath)\n    data, _ = os.ReadFile(regenerateIndex(modroot))\n    pkg, err = modindex.PackageFromBytes(modroot, data)\n}","preventionTips":["Avoid calling packageFromBytes on user-supplied data.","Treat single-package blobs as produced and consumed by the same toolchain version."],"tags":["module-index","cache","corruption"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T11:17:21.771Z"}