rust-lang/cargo · error

parsing `{}` requires `-Zscript`

Error message

parsing `{}` requires `-Zscript`

What it means

When compiling a unit whose manifest is 'embedded' (a single-file package, e.g. a .rs file with an embedded `cargo` manifest / frontmatter), prepare_rustc (mod.rs:812-818) requires the nightly-only `-Zscript` feature to be enabled on the Cargo invocation. Without it, Cargo bails with 'parsing `<path>` requires `-Zscript`'. The script feature unlocks single-file packages and passes extra rustc crate-attrs for frontmatter parsing.

Source

Thrown at src/compiler/mod.rs:814

/// Prepares flags and environments we can compute for a `rustc` invocation
/// before the job queue starts compiling any unit.
///
/// This builds a static view of the invocation. Flags depending on the
/// completion of other units will be added later in runtime, such as flags
/// from build scripts.
fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilder> {
    let gctx = build_runner.bcx.gctx;
    let is_primary = build_runner.is_primary_package(unit);
    let is_workspace = build_runner.bcx.ws.is_member(&unit.pkg);

    let mut base = build_runner
        .compilation
        .rustc_process(unit, is_primary, is_workspace)?;
    build_base_args(build_runner, &mut base, unit)?;
    if unit.pkg.manifest().is_embedded() {
        if !gctx.cli_unstable().script {
            anyhow::bail!(
                "parsing `{}` requires `-Zscript`",
                unit.pkg.manifest_path().display()
            );
        }
        base.arg("-Z").arg("crate-attr=feature(frontmatter)");
        base.arg("-Z").arg("crate-attr=allow(unused_features)");
    }

    base.inherit_jobserver(&build_runner.jobserver);
    build_deps_args(&mut base, build_runner, unit)?;
    add_cap_lints(build_runner.bcx, unit, &mut base);
    if let Some(args) = build_runner.bcx.extra_args_for(unit) {
        base.args(args);
    }
    base.args(&unit.rustflags);
    if gctx.cli_unstable().binary_dep_depinfo {
        base.arg("-Z").arg("binary-dep-depinfo");
    }

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Use a nightly toolchain: `cargo +nightly build -Zscript`.
  2. Ensure `nightly_features_allowed` is true (run on the `nightly` or `dev` channel).
  3. If you did not intend a single-file package, convert it to a normal Cargo project (Cargo.toml + src/main.rs) so it is no longer detected as embedded.

Example fix

# before
cargo build script.rs
# after
cargo +nightly build -Zscript script.rs
Defensive patterns

Strategy: validation

Validate before calling

# Before building a single-file package, verify nightly + flag:
if cargo +nightly -Zscript --version >/dev/null 2>&1; then
  cargo +nightly build -Zscript script.rs
else
  echo "requires nightly cargo with -Zscript"; exit 1
fi

Prevention

When it happens

Trigger: Running `cargo build`/`cargo run` against a `.rs` file (or a package detected as embedded) without passing `-Zscript`. prepare_rustc checks `gctx.cli_unstable().script` and bails when false.

Common situations: Trying the single-file package RFC on stable Cargo; forgetting the `-Zscript` flag; switching toolchains away from nightly where unstable flags are allowed.

Related errors


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