rust-lang/cargo · error

{}

Error message

{}

What it means

In `cargo tree`'s exec (tree.rs:158), the `--prefix` value is parsed via `cargo_tree::Prefix::from_str`. An unrecognized prefix string produces a static error message that is wrapped with `anyhow::anyhow!("{}", e)` and returned. The valid prefix values are typically `none`, `depth`, and indent-style prefixes defined by the `cargo_tree` crate.

Source

Thrown at src/bin/cargo/commands/tree.rs:158

pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
    if args.flag("version") {
        let verbose = args.verbose() > 0;
        let version = cli::get_version_string(verbose);
        cargo::drop_print!(gctx, "{}", version);
        return Ok(());
    }
    let prefix = if args.flag("no-indent") {
        gctx.shell()
            .warn("the --no-indent flag has been changed to --prefix=none")?;
        "none"
    } else if args.flag("prefix-depth") {
        gctx.shell()
            .warn("the --prefix-depth flag has been changed to --prefix=depth")?;
        "depth"
    } else {
        args.get_one::<String>("prefix").unwrap().as_str()
    };
    let prefix = cargo_tree::Prefix::from_str(prefix).map_err(|e| anyhow::anyhow!("{}", e))?;

    let no_dedupe = args.flag("no-dedupe") || args.flag("all");
    if args.flag("all") {
        gctx.shell().print_report(
            &[Level::WARNING
                .secondary_title(
                    "the `cargo tree` --all flag has been changed to --no-dedupe, \
                    and may be removed in a future version",
                )
                .element(Level::HELP.message(
                    "if you are looking to display all workspace members, use the --workspace flag",
                ))],
            false,
        )?;
    }

    let targets = if args.flag("all-targets") {
        gctx.shell()

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Use a valid `--prefix` value: `none` or `depth` (or omit the flag for the default).
  2. Run `cargo tree --help` to see accepted values for your Cargo version.
  3. If you relied on legacy behavior, note `--no-indent` maps to `--prefix=none` and `--prefix-depth` maps to `--prefix=depth`.

Example fix

// before
$ cargo tree --prefix=compact
error: invalid prefix

// after
$ cargo tree --prefix=none
Defensive patterns

Strategy: validation

Validate before calling

let valid_prefixes = ["none", "depth"]; // per cargo-tree::Prefix
if !valid_prefixes.contains(&prefix.as_str()) {
    return Err(format!("invalid --prefix `{prefix}`; expected one of {valid_prefixes:?}"));
}

Type guard

fn is_valid_prefix(p: &str) -> bool {
    matches!(p, "none" | "depth")
}

Prevention

When it happens

Trigger: Passing `cargo tree --prefix=foo` where `foo` is not one of the accepted prefix values (`none`, `depth`, or the default indentation variants).

Common situations: Misspelling `--prefix=depth` or `--prefix=none`; passing a value carried over from a different tool's docs; using a value only valid in a newer/older cargo-tree.

Related errors


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