{"record":{"id":"101dad83b6438b9d","repo":"rust-lang/cargo","slug":"library-cannot-set-the-crate-type-of-both-dy","errorCode":null,"errorMessage":"library `{}` cannot set the crate type of both `dylib` and `cdylib`","messagePattern":"library `(.+?)` cannot set the crate type of both `dylib` and `cdylib`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/workspace/parser/targets.rs","lineNumber":247,"sourceCode":"\n    let path = lib.path.as_ref().expect(\"previously normalized\");\n    let path = package_root.join(&path.0);\n\n    // Per the Macros 1.1 RFC:\n    //\n    // > Initially if a crate is compiled with the `proc-macro` crate type\n    // > (and possibly others) it will forbid exporting any items in the\n    // > crate other than those functions tagged #[proc_macro_derive] and\n    // > those functions must also be placed at the crate root.\n    //\n    // A plugin requires exporting plugin_registrar so a crate cannot be\n    // both at once.\n    let crate_types = match (lib.crate_types(), lib.proc_macro()) {\n        (Some(kinds), _)\n            if kinds.contains(&CrateType::Dylib.as_str().to_owned())\n                && kinds.contains(&CrateType::Cdylib.as_str().to_owned()) =>\n        {\n            anyhow::bail!(format!(\n                \"library `{}` cannot set the crate type of both `dylib` and `cdylib`\",\n                name_or_panic(lib)\n            ));\n        }\n        (Some(kinds), _) if kinds.contains(&\"proc-macro\".to_string()) => {\n            warnings.push(format!(\n                \"library `{}` should only specify `proc-macro = true` instead of setting `crate-type`\",\n                name_or_panic(lib)\n            ));\n            if kinds.len() > 1 {\n                anyhow::bail!(\"cannot mix `proc-macro` crate type with others\");\n            }\n            vec![CrateType::ProcMacro]\n        }\n        (Some(kinds), _) => kinds.iter().map(|s| s.into()).collect(),\n        (None, Some(true)) => vec![CrateType::ProcMacro],\n        (None, _) => vec![CrateType::Lib],\n    };","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/rust-lang/cargo/blob/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/src/workspace/parser/targets.rs#L229-L265","documentation":"A library target's `crate-type` may not list both `dylib` (a Rust-ABI dynamic library loaded by other Rust crates) and `cdylib` (a C-ABI dynamic library for FFI). They produce incompatible symbol/export layouts, so the match arm at targets.rs:247-252 rejects the combination outright before any compilation.","triggerScenarios":"Cargo.toml has `[lib] crate-type = [\"dylib\", \"cdylib\"]` (in any order). The guard `kinds.contains(&Dylib) && kinds.contains(&Cdylib)` matches and bails.","commonSituations":"Wanting both a Rust-loadable and a C-callable artifact from one crate and naively listing both. Copy-pasting crate-type lists between unrelated projects. Misunderstanding that cdylib already produces a loadable `.so`/`.dylib`/`.dll`.","solutions":["Choose `cdylib` if the consumer is C/FFI; choose `dylib` if the consumer is another Rust crate.","If you truly need both ABIs, split into two crates or two `[lib]` targets with distinct names and paths.","Use `cargo build` to confirm the single type resolves."],"exampleFix":"# before\n[lib]\ncrate-type = [\"dylib\", \"cdylib\"]\n\n# after (C/FFI consumer)\n[lib]\ncrate-type = [\"cdylib\"]","handlingStrategy":"validation","validationCode":"fn lib_has_conflicting_crate_types(crate_types: &[String]) -> bool {\n    crate_types.iter().any(|t| t == \"dylib\")\n        && crate_types.iter().any(|t| t == \"cdylib\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Choose one dynamic crate type per lib target.","Split dual-ABI needs into separate crates.","Review `crate-type` lists during code review."],"tags":["cargo","config","library","crate-type","ffi"],"backgroundTag":null,"analyzedSha":"eb98b54bc9f3c74519f43d066cb3fd02ebc88df0","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}