Running code before cargo does anything

Is there a step in the build process where I can run some rust before Cargo.toml is parsed?

In my never ending struggles to get sane management of/synchronise 3rd party dependencies in my workspace, I am considering having a single properties file with the versions in and then doing something clever in build.rs to set the versions in the various Cargo.tomls in the workspace.

I could do this outside of build.rs and alias cargo to call my thing, but build.rs seems neater.

Unfortunately not.

You're going to need to wrap it in some other build system (e.g. make), or hack it hard in build.rs.

There's RUSTC_WRAPPER that may let you do some horrible hacks, but that will happen after Cargo.toml is parsed.

1 Like

Also consider writing an xtask, such as cargo xtask fix-deps

Cargo can be extended.
https://doc.rust-lang.org/book/ch14-05-extending-cargo.html
You could make a program or "bash script" to do what you want and call it cargo-before.

js@flower:~/learn/rust/comli$ cat ~/bin/cargo-before
#!/bin/bash
echo "It works!"
cargo b
js@flower:~/learn/rust/comli$ cargo before
It works!
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s

And if in a directory with no Cargo.toml you will notice the "It works!" is before the Cargo.toml parse error.

js@flower:~$ cargo before
It works!
error: could not find `Cargo.toml` in `/home/js` or any parent directory
1 Like

thanks all

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.