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

parsing `{}` requires `-Zscript`

Error message

parsing `{}` requires `-Zscript`

What it means

Thrown by `read_toml_string` at src/workspace/parser/mod.rs:179. When the manifest path is detected as an embedded manifest (a single-file cargo script, typically a `.rs` file with a TOML frontmatter block or a leading shebang), cargo must expand it via `embedded::expand_manifest`, and that code path is gated behind the nightly-only `-Z script` feature. Without it, parsing bails.

Source

Thrown at src/workspace/parser/mod.rs:179

    mut document: toml::Spanned<toml::de::DeTable<'_>>,
) -> toml::Spanned<toml::de::DeTable<'static>> {
    document.get_mut().make_owned();
    // SAFETY: `DeTable::make_owned` ensures no borrows remain and the lifetime does not affect
    // layout
    unsafe {
        std::mem::transmute::<
            toml::Spanned<toml::de::DeTable<'_>>,
            toml::Spanned<toml::de::DeTable<'static>>,
        >(document)
    }
}

#[tracing::instrument(skip_all)]
fn read_toml_string(path: &Path, is_embedded: bool, gctx: &GlobalContext) -> CargoResult<String> {
    let mut contents = paths::read(path).map_err(|err| ManifestError::new(err, path.into()))?;
    if is_embedded {
        if !gctx.cli_unstable().script {
            anyhow::bail!("parsing `{}` requires `-Zscript`", path.display());
        }
        contents = embedded::expand_manifest(&contents)
            .map_err(|e| emit_frontmatter_diagnostic(e, &contents, path, gctx))?;
    }
    Ok(contents)
}

#[tracing::instrument(skip_all)]
fn parse_document(contents: &str) -> Result<toml::Spanned<toml::de::DeTable<'_>>, toml::de::Error> {
    toml::de::DeTable::parse(&contents)
}

#[tracing::instrument(skip_all)]
fn deserialize_toml(
    document: &toml::Spanned<toml::de::DeTable<'_>>,
) -> Result<manifest::TomlManifest, toml::de::Error> {
    let mut unused = BTreeSet::new();
    let deserializer = toml::de::Deserializer::from(document.clone());

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Use a nightly toolchain and enable the feature: `cargo +nightly -Z script run script.rs`.
  2. If the feature has stabilized on your toolchain, upgrade cargo.
  3. Otherwise convert the script into a normal crate with a `Cargo.toml`.

Example fix

# before (on stable)
cargo run script.rs
# after
cargo +nightly -Z script run script.rs
Defensive patterns

Strategy: validation

Validate before calling

let needs_script = path.extension().and_then(|e| e.to_str()) == Some("rs")
    || contents.starts_with("#!/");
if needs_script && !unstable.script {
    return Err("enable -Z script (nightly) to parse embedded manifests".into());
}

Prevention

When it happens

Trigger: Running `cargo run script.rs` (or `cargo build` on a `.rs` with frontmatter) on stable, or nightly without `-Z script` enabled. `is_embedded` is true but `gctx.cli_unstable().script` is false.

Common situations: Trying single-file packages (`cargo-eval`-style scripts) before the feature stabilized; CI pinned to stable; documentation examples that assume the feature.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/f4a22a574dad5e24.json. Report an issue: GitHub.