rust-lang/cargo · error · anyhow::Error

the -Zhost-config flag requires the…

Error message

the -Zhost-config flag requires the -Ztarget-applies-to-host flag to be set

What it means

Thrown by get_target_applies_to_host() when the -Z host-config unstable flag is enabled but -Z target-applies-to-host is not. These nightly-only flags are interdependent: host-config allows defining per-host-target configuration, but target-applies-to-host must be set to control whether [target] tables apply to host targets.

Solutions

  1. Add -Z target-applies-to-host to the cargo invocation alongside -Z host-config
  2. Set target-applies-to-host in config.toml under the unstable table if using config-based activation

Example fix

# before
cargo build -Z host-config

# after
cargo build -Z host-config -Z target-applies-to-host
Defensive patterns

Strategy: validation

Validate before calling

// Validate unstable flag combinations before invoking cargo
fn validate_unstable_flags(flags: &[String]) -> Result<(), String> {
    let has_host_config = flags.iter().any(|f| f == "host-config");
    let has_target_applies = flags.iter().any(|f| f == "target-applies-to-host");
    if has_host_config && !has_target_applies {
        return Err("-Z host-config requires -Z target-applies-to-host".into());
    }
    Ok(())
}

Prevention

When it happens

Trigger: Running cargo with -Z host-config without also passing -Z target-applies-to-host.

Common situations: Configuring cross-compilation host-specific overrides without enabling the prerequisite flag. Following partial documentation or examples that only mention host-config.

Related errors


AI-assisted analysis of rust-lang/cargo@98a09e7e7d (2026-08-11). Data as JSON: /api/errors/a356f02b04838aef. Report an issue: GitHub.

Appendix: source

Thrown at src/context/target.rs:91

                    other_key, key
                ))?;
            }
            result.push((key, cfg));
        }
    }
    Ok(result)
}

/// Returns true if the `[target]` table should be applied to host targets.
pub(super) fn get_target_applies_to_host(gctx: &GlobalContext) -> CargoResult<bool> {
    if gctx.cli_unstable().target_applies_to_host {
        if let Ok(target_applies_to_host) = gctx.get::<bool>(["target-applies-to-host"]) {
            Ok(target_applies_to_host)
        } else {
            Ok(!gctx.cli_unstable().host_config)
        }
    } else if gctx.cli_unstable().host_config {
        anyhow::bail!(
            "the -Zhost-config flag requires the -Ztarget-applies-to-host flag to be set"
        );
    } else {
        Ok(true)
    }
}

/// Loads a single `[host]` table for the given tuple.
pub(super) fn load_host_triple(gctx: &GlobalContext, triple: &str) -> CargoResult<TargetConfig> {
    if gctx.cli_unstable().host_config {
        let host_triple_prefix = ["host", triple];
        let host_triple_key = ConfigKey::from_parts(host_triple_prefix);
        let host_prefix = match gctx.get_cv(&host_triple_key)? {
            Some(_) => host_triple_prefix.as_slice(),
            None => &["host"],
        };
        load_config_table(gctx, host_prefix)
    } else {

View on GitHub (pinned to 98a09e7e7d)