How to see what cargo does

Hi all,

I am trying to understand how cargo invokes rustc, and what it exactly does when I type "cargo build"...

So, I created a new project with "cargo new hello", I did "cd hello", and then I typed "cargo build -vv". It said:

luca@luca64:/tmp/Test/hello$ cargo build -vv
   Compiling hello v0.1.0 (/tmp/Test/hello)
     Running `CARGO_PKG_HOMEPAGE= CARGO_PKG_REPOSITORY= CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_NAME=hello CARGO_PKG_VERSION=0.1.0 CARGO_PKG_AUTHORS='lucabe72@gmail.com' CARGO=/usr/bin/cargo CARGO_MANIFEST_DIR=/tmp/Test/hello CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PRIMARY_PACKAGE=1 CARGO_PKG_VERSION_PRE= LD_LIBRARY_PATH='/tmp/Test/hello/target/debug/deps:/usr/lib' CARGO_PKG_DESCRIPTION= rustc --edition=2018 --crate-name hello src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=24a97ba19af841c7 -C extra-filename=-24a97ba19af841c7 --out-dir /tmp/Test/hello/target/debug/deps -C incremental=/tmp/Test/hello/target/debug/incremental -L dependency=/tmp/Test/hello/target/debug/deps`
    Finished dev [unoptimized + debuginfo] target(s) in 0.32s

and it generated the "target/debug/hello" executable.

However, if I do "rm -rf target" and then I type

CARGO_PKG_HOMEPAGE= CARGO_PKG_REPOSITORY= CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_NAME=hello CARGO_PKG_VERSION=0.1.0 CARGO_PKG_AUTHORS='lucabe72@gmail.com' CARGO=/usr/bin/cargo CARGO_MANIFEST_DIR=/tmp/Test/hello CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PRIMARY_PACKAGE=1 CARGO_PKG_VERSION_PRE= LD_LIBRARY_PATH='/tmp/Test/hello/target/debug/deps:/usr/lib' CARGO_PKG_DESCRIPTION= rustc --edition=2018 --crate-name hello src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=24a97ba19af841c7 -C extra-filename=-24a97ba19af841c7 --out-dir /tmp/Test/hello/target/debug/deps -C incremental=/tmp/Test/hello/target/debug/incremental -L dependency=/tmp/Test/hello/target/debug/deps

I get an error:

error: error writing dependencies to `/tmp/Test/hello/target/debug/deps/hello-24a97ba19af841c7.d`: No such file or directory (os error 2)

error: aborting due to previous error

and the name of the generated executable is not "target/debug/hello"... So, cargo obviously did something more than what it declared when I asked "build -vv"... Does anyone know if it is possible to see exactly what cargo does when building my project?

Thanks,
Luca

Cargo is not a shell script, so "what it does" cannot be boiled down to a series of shell commands. If you want to know what it's doing, you'd probably have to invoke it with strace, and see what syscalls it's making.

cliff

    January 15

Cargo is not a shell script, so "what it does" cannot be boiled down to a series of shell commands.

Yes, I know cargo is not a shell script... But I got the impression that "cargo build" is similar to "make"... And "make" is not a shell script too, but provides ways to see what it does.

If you want to know what it's doing, you'd probably have to invoke it with strace, and see what syscalls it's making.

Well, if reverse engineering is the only way to see what cargo does, I'll try (but maybe strace is not even needed: it is pretty obvious that before invoking rustc cargo creates some directories, and after invoking rustc cargo copies the generated executable in "target/debug/Test:)... My question was mainly if the cargo behaviour is documented somewhere or if cargo provides some "verbose" (or "debug") mode that prints what it does.

Thanks,

Luca

You could try CARGO_LOG=trace cargo build -vv.

2 Likes

Hi Bjorn,

this worked, thanks!

Luca

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.