-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
target.'cfg(debug_assertions)'.dependencies doesn't work as expected #7634
Comments
target.'cfg(debug_assertions)'.dependencies
doesn't work as expected
I think this is essentially a duplicate of #5777. I'd be a little uneasy about trying to set debug-assertions in TargetInfo because there isn't one profile, but instead a profile per unit, based on various factors (cargo target, package, cli flags, etc.). |
Yeah, you are right, it's basically the same issue. But that issue also doesn't have a solution so the question is still what the best solution for this would be. As for setting |
I think it would be best for now if Cargo generated an error if it sees |
Ok, I guess that's the simplest solution for now. I'm going to have a shot at implementing that error and probably also create a PR to update the docs. What do you think about something like |
I don't know how profile-specific dependencies would work. A single artifact can consist of multiple profiles, and a single invocation can do multiple things with different profiles. It seems like it would be difficult. |
Interesting, I thought there was always only one profile active for a specific build/run. To me, it looks like the CompileOptions also only have a single profile. But whatever, I guess this isn't really that important. |
* Make dependency on `image` optional After PR #481 was merged, the `image` dependency became unused when building with debug assertions disabled, as it is only used to implement output sanity checks when such assertions are enabled. The `image` crate transitively pulls a significant amount of dependencies, so it's useful for OxiPNG users to get rid of them when not needed. [Cargo does not allow specifying dependencies that are only pulled when debug assertions are enabled](rust-lang/cargo#7634), so the next best way to give users some flexibility is to gate those debug assertions behind a feature flag. These changes add a `sanity-checks` feature flag that controls whether the `image` crate and the related sanity checks are compiled in. This feature is enabled by default to keep debug builds useful to catch problems during development. * Fix Clippy lints * Run tests with new sanity-checks feature enabled
Problem
Specifying conditional dependencies using
target.'cfg(debug_assertions)'.dependencies
does not work as expected (i.e. only building the dependencies when debug assertions are enabled) and instead always includes the dependencies. The same applies forcfg(test)
andcfg(proc_macro)
althoughdev-dependencies
can be used instead ofcfg(test)
.I guess it kind of makes sense that only
cfg
-attributes that have something to do with a target work in this context however it is still very confusing and unexpected. Also, the documentation explicitly mentions thatcfg(feature = ...)
doesn't work so I would expect everything else to work.Steps
Using this Cargo.toml
and compiling with
cargo build --release
still compilescolor-backtrace
.Possible Solutions
cfg
-attributes related to targets work in this context. In that case, it would be good to add another way to include dependencies only for debug or release builds e.g. usingprofile.dev.dependencies
. (This might be a good idea in any case).cfg
-attributes in the target context. The main problem why this currently doesn't work is that thecfg
values are taken fromrustc --print cfg
which always includesdebug_assertions
. One option would be to callrustc
using the correct attributes for optimization, crate type, etc. but this would require some changes as this is currently computed as part ofTargetInfo
which is independent of any flags related to profiles. Another option would be to fix thecfg
list at the uses where profile flags would be available. Although this is kinda hacky it would be fairly easy to do as there are only 5 uses of thecfg
list and they all have profile information available.Personally I think solution 2 will always be a bit messy so it's probably best to add something like
profile.dev.dependencies
instead. I would be happy to implement this if it sounds like a good idea.The text was updated successfully, but these errors were encountered: