{"id":"583072560ae3ea01","repo":"rust-lang/rust","slug":"failed-to-open-lto-bitcode-file","errorCode":null,"errorMessage":"failed to open LTO bitcode file `{}`: {}","messagePattern":"failed to open LTO bitcode file `(.+?)`: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_codegen_ssa/src/back/lto.rs","lineNumber":56,"sourceCode":"    }\n}\n\npub struct ThinShared<B: WriteBackendMethods> {\n    pub data: B::ThinData,\n    pub modules: Vec<SerializedModule<B::ModuleBuffer>>,\n    pub module_names: Vec<CString>,\n}\n\npub enum SerializedModule<M: ModuleBufferMethods> {\n    Local(M),\n    FromRlib(Vec<u8>),\n    FromUncompressedFile(Mmap),\n}\n\nimpl<M: ModuleBufferMethods> SerializedModule<M> {\n    pub fn from_file(bc_path: &Path) -> Self {\n        let file = fs::File::open(&bc_path).unwrap_or_else(|e| {\n            panic!(\"failed to open LTO bitcode file `{}`: {}\", bc_path.display(), e)\n        });\n\n        let mmap = unsafe {\n            Mmap::map(file).unwrap_or_else(|e| {\n                panic!(\"failed to mmap LTO bitcode file `{}`: {}\", bc_path.display(), e)\n            })\n        };\n        SerializedModule::FromUncompressedFile(mmap)\n    }\n\n    pub fn data(&self) -> &[u8] {\n        match *self {\n            SerializedModule::Local(ref m) => m.data(),\n            SerializedModule::FromRlib(ref m) => m,\n            SerializedModule::FromUncompressedFile(ref m) => m,\n        }\n    }\n}","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_codegen_ssa/src/back/lto.rs#L38-L74","documentation":"Thrown by SerializedModule::from_file when fs::File::open succeeds-or-panics on the given bitcode (.bc) path during LTO. If the file cannot be opened, rustc panics with the path and the underlying OS error instead of recovering, because a missing LTO bitcode input means incremental/LTO state is inconsistent and linking cannot proceed. A companion panic a few lines later covers the mmap step.","triggerScenarios":"Reached during ThinLTO/fatLTO when rustc tries to load a serialized bitcode module file (path passed to from_file) that does not exist, has been deleted, or is unreadable. The file is expected to be present from a prior codegen/LTO serialization step.","commonSituations":"Incremental compilation cache (target/incremental) was deleted or corrupted between sessions. A killed/interrupted prior build left LTO metadata referencing a .bc file that was never fully written. Permissions or disk-full conditions during a prior run. Manual cleanup of target/ that removed only some LTO artifacts. Concurrent builds clobbering shared incremental state.","solutions":["Run cargo clean and rebuild so all LTO bitcode is regenerated consistently.","Ensure the build is not interrupted mid-LTO and that target/ is not partially deleted between runs.","Check available disk space and write permissions on the target directory; LTO bitcode files are large.","Avoid pointing CARGO_INCREMENTAL / INCR_COMP_SHARED_DIR at storage shared by concurrent rustc invocations."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"use std::fs::File;\nuse std::io::Read;\n\nfn lto_bitcode_file_readable(path: &std::path::Path) -> bool {\n    let Ok(mut f) = File::open(path) else { return false; };\n    let mut magic = [0u8; 4];\n    if f.read_exact(&mut magic).is_err() { return false; }\n    // LLVM bitcode wrapper magic: 0xDE 0xC0 0x17 0x0B;\n    // raw bitcode: 'B' 'C' 0xC0 0xDE.\n    matches!(magic, [0xDE, 0xC0, 0x17, 0x0B] | [b'B', b'C', 0xC0, 0xDE])\n}\n\n// caller: assert!(lto_bitcode_file_readable(Path::new(\"foo.bc\")));","typeGuard":"fn is_valid_lto_bitcode(path: &std::path::Path) -> bool {\n    lto_bitcode_file_readable(path)\n}","tryCatchPattern":"let stderr = String::from_utf8_lossy(&output.stderr);\nif let Some(line) = stderr.lines().find(|l| l.contains(\"failed to open LTO bitcode file\")) {\n    // Pull the path + OS error from the message, then either\n    // rebuild the bitcode (stale) or surface a missing-file error.\n    return Err(LinkError::BitcodeOpen(line.into()));\n}","preventionTips":["Run `cargo clean` after toolchain upgrades to drop stale `.bc`/`.o` artifacts.","Verify bitcode files exist and are readable before invoking LTO (`-C lto=fat|thin`).","Pin `RUSTFLAGS=-C lto=off` when shipping to environments where bitcode files may be missing."],"tags":["lto","incremental","io","bitcode","codegen"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}