LinuxCommandLibrary

cargo-locate-project

Find a Cargo project manifest (Cargo.toml)

TLDR

Display the JSON object with full path to the Cargo.toml manifest

$ cargo locate-project
copy

Display the project path in the specified format
$ cargo locate-project --message-format [plain|json]
copy

Display the Cargo.toml manifest located at the root of the workspace as opposed to the current workspace member
$ cargo locate-project --workspace
copy

Display the Cargo.toml manifest of a specific directory
$ cargo locate-project --manifest-path [path/to/Cargo.toml]
copy

SYNOPSIS

cargo locate-project [PATH] [--message-format FORMAT] [--workspace] [-Z FLAG] [-h|--help] [-V|--version]

PARAMETERS

PATH
    Directory to start search from (defaults to current working directory)

--message-format FORMAT
    Output format: json (default, structured JSON) or plain (path as text)

--workspace
    Emit workspace root Cargo.toml path if available, else project root

-Z FLAG
    Pass unstable (nightly-only) flags to Cargo

-h, --help
    Print help information

-V, --version
    Print Cargo version information

DESCRIPTION

cargo locate-project is a Cargo subcommand that finds the path to the closest ancestor directory containing a Cargo.toml file, defining a Rust project or workspace root. Ideal for scripts, IDEs, or tools operating within project subdirectories, it reliably identifies the project root without manual traversal.

By default, it outputs JSON: {"root": "/path/to/Cargo.toml"}, perfect for programmatic parsing. Use --message-format plain for simple text output. The --workspace flag returns the workspace root if applicable, aiding multi-crate projects.

This command enhances automation in Rust workflows, integrating with build tools, linters, or custom scripts needing context-aware paths. It searches upward from the given PATH (or current directory), stopping at the first valid project.

EXAMPLES

cargo locate-project
{"root": "/home/user/myproj/Cargo.toml"}

cargo locate-project --message-format plain --workspace
/home/user/workspace/Cargo.toml

OUTPUT NOTES

Always points to Cargo.toml file, not directory. JSON includes trailing newline; plain does not.

HISTORY

Introduced as unstable in Cargo 1.40.0 (2020-05-11), stabilized in Cargo 1.52.0 (2021-04-22) for reliable project root discovery in Rust tooling.

SEE ALSO

Copied to clipboard