cargo-outdir

Crates.iocargo-outdir
lib.rscargo-outdir
version0.2.2
created_at2022-01-07 07:42:04.443015+00
updated_at2025-06-29 00:19:30.042388+00
descriptionA cargo subcommand to get the $OUT_DIR environment variable from build scripts.
homepage
repositoryhttps://github.com/Techcable/cargo-out
max_upload_size
id509315
size60,842
(Techcable)

documentation

README

cargo-outdir crates.io

A cargo subcommand to fetch the $OUT_DIR variable from build scripts.

This is extremely useful to inspect the output of automatically generated code, like bindgen or parol.

This can be seen as an extension to cargo metadata, except it requires that cargo check succeeds.

Due to recent changes in cargo check, this will not invalidate cached outputs, so the build scripts won't be re-run unless they need to :)

It is effectively a workaround to rust-lang/cargo#7546

Examples

$ cargo out

Assuming your current crate is named current-crate, this will output something like:

current-crate /Users/techcable/git/current-crate/target/debug/build/current-crate-82e5bb1cb82b68a7/out

Alternatively, to omit the name you can use --no-names. This is useful in shell scripts as $(cargo out --no-names)

This command works with any crate name (as long as it has a build.rs file).

$ cargo out syn indexmap

syn /Users/techcable/git/current-crate/target/debug/build/syn-2bbc24a01fc81726/out
indexmap /Users/techcable/git/current-crate/target/debug/build/indexmap-376e9f234cf30ee8/out

These are output in the order specified on the command line, separated by newlines.

If the package doesn't have an out dir, the output will be "<MISSING OUT_DIR>" exactly and the exit code will be 2.

You might want to consider json output as well.

$ cargo out --json --all

{
    "syn": "/Users/techcable/git/current-crate/target/debug/build/syn-2bbc24a01fc81726/out",
    "indexmap": "/Users/techcable/git/current-crate/target/debug/build/indexmap-376e9f234cf30ee8/out",
    "libc:0.1.12": null,
    "libc:0.2.109": "/Users/techcable/git/current-crate/target/debug/build/libc-1c95e0902b980b08/out",
    // other_crates here
}

If packages don't have an $OUT_DIR (because they don't have a build script), then the value for the specified key will be null.

This can be used along with jq for easy processing in scripts.

How it works

This runs cargo check --message-format=json to extract the necessary information.

Historically this has been an issue for IDEs, both intellij-rust and rust-analyser have struggled to support this.

Do to recent cargo changes, this will always output the proper $OUT_DIR variables for all packages that have one.

If multiple packages have the same name, then a version will be added. More precisely, the json keys will be the minimal cargo pkgid needed to disambiguate them. This is actually somewhat difficult to do.

License

Licensed under either the Apache 2.0 License or MIT License at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 20

cargo fmt