{"record":{"id":"e2b215bf1b30214e","repo":"rust-lang/rust","slug":"failed-to-find-foreign-module","errorCode":null,"errorMessage":"failed to find foreign module","messagePattern":"failed to find foreign module","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs","lineNumber":450,"sourceCode":"}\n\npub(in crate::rmeta) fn provide(providers: &mut Providers) {\n    provide_cstore_hooks(providers);\n    providers.queries = rustc_middle::query::Providers {\n        allocator_kind: |tcx, ()| CStore::from_tcx(tcx).allocator_kind(),\n        alloc_error_handler_kind: |tcx, ()| CStore::from_tcx(tcx).alloc_error_handler_kind(),\n        is_private_dep: |_tcx, LocalCrate| false,\n        native_library: |tcx, id| {\n            tcx.native_libraries(id.krate)\n                .iter()\n                .filter(|lib| native_libs::relevant_lib(tcx.sess, lib))\n                .find(|lib| {\n                    let Some(fm_id) = lib.foreign_module else {\n                        return false;\n                    };\n                    let map = tcx.foreign_modules(id.krate);\n                    map.get(&fm_id)\n                        .expect(\"failed to find foreign module\")\n                        .foreign_items\n                        .contains(&id)\n                })\n        },\n        native_libraries: native_libs::collect,\n        foreign_modules: foreign_modules::collect,\n        externally_implementable_items: eii::collect,\n\n        // Returns a map from a sufficiently visible external item (i.e., an\n        // external item that is visible from at least one local module) to a\n        // sufficiently visible parent (considering modules that re-export the\n        // external item to be parents).\n        visible_parent_map: |tcx, ()| {\n            use std::collections::hash_map::Entry;\n            use std::collections::vec_deque::VecDeque;\n\n            let mut visible_parent_map: DefIdMap<DefId> = Default::default();\n            // This is a secondary visible_parent_map, storing the DefId of","sourceCodeStart":432,"sourceCodeEnd":468,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs#L432-L468","documentation":"Fires in the `native_library` query provider (cstore_impl.rs:450). When resolving a foreign (FFI) item, the provider looks up the item's `foreign_module` id in the crate's `foreign_modules` map and `.expect(\"failed to find foreign module\")` panics if it is absent. The invariant is that every foreign item's module id appears in the collected `foreign_modules` map for its crate.","triggerScenarios":"An FFI item (`extern \"C\" { ... }` or `extern { ... }`) references a `foreign_module` id that was not emitted by `foreign_modules::collect` for that crate; mismatch between `native_libraries` and `foreign_modules` collection; metadata for a dependency built with a different native-module encoding.","commonSituations":"A `-L native=...` / `--extern` linking change reusing stale metadata; cross-version reuse of a crate that does FFI; build-script-generated `cargo:rustc-link-lib` directives changed while `target/` was reused; incomplete native-lib collection due to cfg differences.","solutions":["`cargo clean` and rebuild so `native_libraries` and `foreign_modules` are collected consistently for the current cfg/link config.","Re-run build scripts (`touch build.rs` or `cargo clean -p <crate>`) so `cargo:rustc-link-lib`/`native=` directives are regenerated.","Verify the FFI block's `#[link(name = ...)]` / `extern \"C\"` module matches a library actually passed via `-L native`.","Clean-build ICE -> file an issue; a foreign item's module id was not present in the foreign_modules map."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"# Regenerate native-library/foreign-module collection after link changes\ntouch build.rs 2>/dev/null || true\ncargo clean -p <crate-with-ffi>\ncargo build","typeGuard":null,"tryCatchPattern":"cargo build || { cargo clean -p $(cargo metadata --no-deps --format-version=1 | jq -r '.packages[].name') && cargo build; }","preventionTips":["Rerun build scripts (`touch build.rs` or `cargo clean -p <crate>`) after changing `cargo:rustc-link-lib` / `native=` directives.","Keep the `#[link(name = ...)]` on FFI blocks consistent with the libraries supplied via `-L native`.","`cargo clean` after altering the native-link configuration.","Do not reuse `target/` across cfg changes that affect which foreign modules are collected."],"tags":["metadata","ffi","linking","native-libraries","ice"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}