{"id":"ff649403a1a8c5d3","repo":"rust-lang/rust","slug":"couldn-t-open-rlib","errorCode":null,"errorMessage":"couldn't open rlib","messagePattern":"couldn't open rlib","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_codegen_llvm/src/back/lto.rs","lineNumber":96,"sourceCode":"\n    // __llvm_profile_counter_bias is pulled in at link time by an undefined reference to\n    // __llvm_profile_runtime, therefore we won't know until link time if this symbol\n    // should have default visibility.\n    symbols_below_threshold.push(c\"__llvm_profile_counter_bias\".to_owned());\n\n    // LTO seems to discard this otherwise under certain circumstances.\n    symbols_below_threshold.push(c\"rust_eh_personality\".to_owned());\n\n    // If we're performing LTO for the entire crate graph, then for each of our\n    // upstream dependencies, find the corresponding rlib and load the bitcode\n    // from the archive.\n    //\n    // We save off all the bytecode and LLVM module ids for later processing\n    // with either fat or thin LTO\n    let mut upstream_modules = Vec::new();\n    for path in each_linked_rlib_for_lto {\n        let archive_data = unsafe {\n            Mmap::map(std::fs::File::open(&path).expect(\"couldn't open rlib\"))\n                .expect(\"couldn't map rlib\")\n        };\n        let archive = ArchiveFile::parse(&*archive_data).expect(\"wanted an rlib\");\n        let metadata_link = rmeta_link::read(&archive, &archive_data, &path).unwrap();\n        let obj_files = archive\n            .members()\n            .filter_map(|child| {\n                child\n                    .ok()\n                    .and_then(|c| std::str::from_utf8(c.name()).ok().map(|name| (name.trim(), c)))\n            })\n            .filter(|&(name, _)| metadata_link.rust_object_files.iter().any(|f| f == name));\n        for (name, child) in obj_files {\n            info!(\"adding bitcode from {}\", name);\n            match get_bitcode_slice_from_object_data(\n                child.data(&*archive_data).expect(\"corrupt rlib\"),\n                cgcx,\n            ) {","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_codegen_llvm/src/back/lto.rs#L78-L114","documentation":"Thrown by an `.expect(\"couldn't open rlib\")` on `std::fs::File::open(&path)` in `with_symbols_below_threshold` (lto.rs:96) while loading upstream dependency rlib bitcode for fat/thin LTO. The operating system refused to open the rlib file at the recorded path, so the compiler aborts (panics) before it can mmap and parse the archive. Because LTO reads every upstream rlib on disk, this surfaces as an Internal-Compiler-Error-style crash during the link/codegen phase rather than a normal diagnostic.","triggerScenarios":"Triggered only when `-C lto=thin|fat` (or `-C lto=y`) is set AND the compiler iterates `each_linked_rlib_for_lto` to load a dependency's rlib whose path cannot be opened: file deleted mid-build, wrong `--sysroot`, relocated target dir, read permission denied, or a stale `Cargo.lock`/build cache pointing at a missing artifact.","commonSituations":"Running `cargo clean` (or `rm -rf target`) concurrently with a second build invoking LTO; switching toolchains so `--sysroot` rlibs no longer match; read-only container/CI filesystem lacking permissions to the rlib; interrupted previous build leaving `target` half-populated; vendored rlib moved out from its recorded path.","solutions":["Re-run `cargo clean` then rebuild with the same LTO flags so the rlib artifacts are regenerated consistently.","Check the file/path in the panic backtrace: verify it exists (`ls -l <path>`) and is readable by the current user.","Ensure the toolchain is not mid-switch (`rustup show`, `rust-toolchain.toml`) and that `--sysroot` points at an installed toolchain whose lib rlibs are intact.","If running in CI/containers, confirm the `target/` directory and sysroot are on a readable filesystem and not concurrently mutated by parallel jobs.","Disable LTO (`-C lto=off` / remove `lto` from `[profile.release]`) to confirm the rlib open is LTO-specific, then re-enable once artifacts are stable."],"exampleFix":"# before: intermittent LTO crash with stale target dir\ncargo build --release  # (lto=true in profile) -> panic: couldn't open rlib\n\n# after: clean and rebuild so LTO reads fresh, consistent rlibs\ncargo clean\ncargo build --release","handlingStrategy":"validation","validationCode":"fn ensure_rlib_openable(path: &std::path::Path) -> std::io::Result<()> {\n    let _f = std::fs::File::open(path)?;\n    std::fs::metadata(path)?;\n    Ok(())\n}\n// Call before invoking LTO: ensure_rlib_openable(&rlib_path)?;","typeGuard":null,"tryCatchPattern":"let r = std::panic::catch_unwind(|| invoke_lto());\nif r.is_err() { /* path missing -> clean target dir, restore dep, rebuild */ }","preventionTips":["Run `cargo clean` before LTO builds so stale/missing rlibs are regenerated.","Do not delete or move files out of target/ while a build is running.","Verify CARGO_TARGET_DIR is on a mounted, readable filesystem with correct permissions.","Pin dependency versions in Cargo.lock so expected rlib paths always exist."],"tags":["rustc","lto","codegen","filesystem","linker"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}