{"id":"b0da7054d6d14665","repo":"rust-lang/cargo","slug":"several-matching-instances-of-target-cfg-li","errorCode":null,"errorMessage":"several matching instances of `target.'cfg(..)'.linker` in configurations\nfirst match `{}` located in {}\nsecond match `{}` located in {}","messagePattern":"several matching instances of `target\\.'cfg\\(\\.\\.\\)'\\.linker` in configurations\nfirst match `(.+?)` located in (.+?)\nsecond match `(.+?)` located in (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compiler/compilation.rs","lineNumber":591,"sourceCode":"\n    // With `target-applies-to-host = true`,\n    // host artifacts must fall through to pick up from [target]\n    // since this is the stable behavior\n    if kind.is_host() && !bcx.gctx.target_applies_to_host()? {\n        return Ok(None);\n    }\n\n    // Try target.'cfg(...)'.linker.\n    let target_cfg = bcx.target_data.info(kind).cfg();\n    let mut cfgs = bcx\n        .gctx\n        .target_cfgs()?\n        .iter()\n        .filter_map(|(key, cfg)| cfg.linker.as_ref().map(|linker| (key, linker)))\n        .filter(|(key, _linker)| CfgExpr::matches_key(key, target_cfg));\n    let matching_linker = cfgs.next();\n    if let Some((key, linker)) = cfgs.next() {\n        anyhow::bail!(\n            \"several matching instances of `target.'cfg(..)'.linker` in configurations\\n\\\n             first match `{}` located in {}\\n\\\n             second match `{}` located in {}\",\n            matching_linker.unwrap().0,\n            matching_linker.unwrap().1.definition,\n            key,\n            linker.definition\n        );\n    }\n    Ok(matching_linker.map(|(_k, linker)| linker.val.clone().resolve_program(bcx.gctx)))\n}\n\nfn explicit_host_kind(host: &str) -> CompileKind {\n    let target = CompileTarget::new(host, false).expect(\"must be a host tuple\");\n    CompileKind::Target(target)\n}\n","sourceCodeStart":573,"sourceCodeEnd":608,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/compiler/compilation.rs#L573-L608","documentation":"In target_linker (compilation.rs:581-600), Cargo selects a `target.'cfg(...)'.linker` by filtering all configured cfg-linker entries whose cfg key matches the current target. If more than one matches, the linker choice is ambiguous and Cargo bails, printing the first and second matching keys and the config-file locations (definition) that defined each.","triggerScenarios":"Defining multiple `[target.'cfg(...)'.linker]` entries (across `.cargo/config.toml`, `~/.cargo/config.toml`, or `--config`) whose cfg expressions both evaluate true for the build target — e.g. `[target.'cfg(unix)'.linker]` and `[target.'cfg(target_arch = \"x86_64\")'.linker]` both matching an x86_64 linux build.","commonSituations":"Overlapping linker config from global + project files; cross-compilation setups that add a broad cfg-linker that collides with an existing one; merging team config; combining an exact-triple linker with a cfg-based one that also matches.","solutions":["Keep only ONE matching cfg-linker entry; remove or narrow the broader expression.","Prefer an exact-triple key (`[target.<triple>.linker]`) over overlapping cfgs.","Run `cargo config get` / inspect layered config files for duplicate linker definitions.","Make cfg conditions mutually exclusive so they cannot both match the same target."],"exampleFix":"// before (.cargo/config.toml)\n[target.'cfg(unix)'.linker]\nlinker = \"clang\"\n[target.'cfg(linux)'.linker]\nlinker = \"gcc\"\n// both match linux -> error\n\n// after\n[target.'cfg(target_os = \"macos\")'.linker]\nlinker = \"clang\"\n[target.'cfg(target_os = \"linux\")'.linker]\nlinker = \"gcc\"","handlingStrategy":"validation","validationCode":"let matching: Vec<_> = target_cfgs.iter()\n    .filter(|(_, c)| c.linker.is_some())\n    .filter(|(k, _)| CfgExpr::matches_key(k, &target_cfg))\n    .collect();\nif matching.len() > 1 {\n    return Err(format!(\"{} target.'cfg(..)'.linker match; disambiguate\", matching.len()));\n}","typeGuard":"fn single_linker_match(entries: &[(&str, &Config)]) -> bool {\n    entries.iter().filter(|(_, c)| c.linker.is_some()).count() <= 1\n}\n","tryCatchPattern":null,"preventionTips":["Audit layered cargo config for linker duplicates.","Use exact-triple linker keys to avoid cfg overlap.","Run `cargo config get` to inspect merged config in CI."],"tags":["cargo","config","target","linker","cfg"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}