{"id":"eb3775ea10661801","repo":"rust-lang/cargo","slug":"cache-expected-4-bytes-for-index-schema-version","errorCode":null,"errorMessage":"cache expected 4 bytes for index schema version","messagePattern":"cache expected 4 bytes for index schema version","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/sources/registry/index/cache.rs","lineNumber":175,"sourceCode":"    pub versions: Vec<(Version, &'a [u8])>,\n    /// For cache invalidation, we tracks the index file version to determine\n    /// when to regenerate the cache itself.\n    pub index_version: &'a str,\n}\n\nimpl<'a> SummariesCache<'a> {\n    /// Deserializes an on-disk cache.\n    pub fn parse(data: &'a [u8]) -> CargoResult<SummariesCache<'a>> {\n        // NB: keep this method in sync with `serialize` below\n        let (first_byte, rest) = data\n            .split_first()\n            .ok_or_else(|| anyhow::format_err!(\"malformed cache\"))?;\n        if *first_byte != CURRENT_CACHE_VERSION {\n            bail!(\"looks like a different Cargo's cache, bailing out\");\n        }\n        let index_v_bytes = rest\n            .get(..4)\n            .ok_or_else(|| anyhow::anyhow!(\"cache expected 4 bytes for index schema version\"))?;\n        let index_v = u32::from_le_bytes(index_v_bytes.try_into().unwrap());\n        if index_v != INDEX_V_MAX {\n            bail!(\n                \"index schema version {index_v} doesn't match the version I know ({INDEX_V_MAX})\",\n            );\n        }\n        let rest = &rest[4..];\n\n        let mut iter = split(rest, 0);\n        let last_index_update = if let Some(update) = iter.next() {\n            str::from_utf8(update)?\n        } else {\n            bail!(\"malformed file\");\n        };\n        let mut ret = SummariesCache::default();\n        ret.index_version = last_index_update;\n        while let Some(version) = iter.next() {\n            let version = str::from_utf8(version)?;","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/sources/registry/index/cache.rs#L157-L193","documentation":"When parsing the on-disk `SummariesCache` for a crate (src/sources/registry/index/cache.rs:165), Cargo reads a 1-byte version header then expects at least 4 more bytes encoding the little-endian `u32` index schema version. If the file is truncated to fewer than 5 bytes total (`rest.get(..4)` is `None`), it bails. This indicates a corrupt or partially-written cache entry.","triggerScenarios":"A cache file under `~/.cargo/registry/index/<reg>/.cache/` that was truncated by a crash/power loss mid-write, a filesystem with 512-byte sector corruption, or an external tool that emptied/truncated the file. Reached on any operation that loads a cached crate summary.","commonSituations":"Build interrupted by SIGKILL/OOM while Cargo was rewriting the cache; antivirus or sync tools (Dropbox/OneDrive) truncating files in `~/.cargo`; disk full at write time; upgrading Cargo across a cache-format change with a half-written file.","solutions":["Clear the affected cache: `rm -rf ~/.cargo/registry/index/*/.cache/` and let Cargo regenerate it.","If it recurs, check for disk-full conditions, antivirus, or cloud-sync tools touching `~/.cargo`.","Run `cargo cache --autoclean` (if cargo-cache is installed) or a full `cargo clean`."],"exampleFix":"# before: corrupt cache blocks every build\nrm -rf ~/.cargo/registry/index/*/.cache/\ncargo fetch","handlingStrategy":"fallback","validationCode":"// Before trusting a cache file, sanity-check its length.\nfn cache_looks_complete(data: &[u8]) -> bool {\n    data.len() >= 5 // 1 version byte + 4 schema-version bytes\n}","typeGuard":null,"tryCatchPattern":"// On parse failure, delete and regenerate rather than crash.\nmatch SummariesCache::parse(&data) {\n    Ok(c) => c,\n    Err(_) => { fs::remove_file(&cache_path).ok(); /* force refetch */ bail!(\"cache corrupt, removed\"); }\n}","preventionTips":["Treat cache parse failures as recoverable: evict and refetch.","Keep cloud-sync/antivirus tools away from `~/.cargo`.","Ensure writes have enough disk space; avoid SIGKILL mid-build."],"tags":["cargo","registry","cache","corruption","validation"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}