How to make `cargo build` fail on warnings to do with the `Cargo.toml` or `Cargo.lock` files?

I'd like to fail on cargo warnings that have to do with the Cargo.toml and Cargo.lock files, or any issue with the build process/setup itself.

For example, warnings such as:

dependency (XXX) specified without providing a local path, Git repository, version, or workspace dependency to use. This will be considered an error in future version

Or the warning about the resolver version 2.

I know that I can deny compiler warnings (rustc, clippy) with -D warnings but that does not seem to affect the warnings that I am trying to prevent from slipping into our main branch.

Is there a way to do this?

There is currently no way to configure cargo lints to be anything other than a warning, but may enable this in the future.

1 Like

Oh, I thought that RFC was about configuring the lints that can also be configured through the command line or RUSTFLAGS.

Anyway, I can always search the cargo build output for warning: in a CI step. It would be nice if that was built-in though.

My understanding is that the RFC will allow configuring lints for cargo in the future too but that this won't necessarily happen right when it stabilizes.

Using something like this now:

BUILD_FLAGS="--all-targets --all-features --locked"
cargo build $BUILD_FLAGS || exit 1
BUILD_WARNINGS=$(cargo build $BUILD_FLAGS 2>&1 | grep "^warning: ")
[[ -z "$BUILD_WARNINGS" ]] || (echo -e "\033[0;31;1merror\033[0m: Warnings were emitted from build!"; exit 1)

Calling build twice because keeping TTY colors for the output and having no colors for grep requires some sed incantation. I'm sure there's a better way to do it but this kinda achieves my objective.

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.