-
Notifications
You must be signed in to change notification settings - Fork 151
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
Accept rustflag equivalent to no_version #243
Comments
@CreepySkeleton I read that thread and would still like to see the improvement I suggested. The discussion in #217 did not cover what should happen if CARGO_PKG_VERSION is not set. My codebase has a lot of structopt-based clis in a monorepo that has no concept of versions, and it would be great for the build system to be able to set a rustflag so that we avoid writing dozens of |
It should be possible to not generate a version call if the env variable is not setted. |
@dtolnay thanks for clarifying, now I see your point. But this custom-cfg solution looks kind of hacky. @TeXitoi I'd rather keep things explicit. I suggest feature gated behavior: [features]
no_default_version = [] It's not that flexible - you can't just tweak behavior on the fly, just by an env var - but it still covers use cases like @dtolnay described quite well. @dtolnay what do you think? |
@CreepySkeleton I think |
@dtolnay Out of curiosity: if your cli is able to set env variables why can't you just CARGO_PKG_VERSION=0.0.0 cargo build... In any case, I think RUSTFLAGS should be left alone save for experimental hacking or inter-compiler stuff. I've had enough interesting experience with I propose STRUCTOPT_NO_DEFAULT_VERSION environment variable - if it's set to |
@CreepySkeleton, not having versions is different than having a version number of 0.0.0. They do different things when running
No internal-ish variables are involved. The rustc $ rustc --help
Usage: rustc [OPTIONS] INPUT
Options:
-h, --help Display this message
--cfg SPEC Configure the compilation environment
... Rustc exposes these flags to source code as |
Right, didn't think about that.
That awkward moment when you realize that you are the one who haven't read docs... Then I have no more objection and absolutely agree. |
Doesn't the cfg flag also work as feature flag? |
Just the opposite - feature may act as Actually, my objection against @dtolnay Could you please explain why you prefer a cfg flag over an env variable? |
@CreepySkeleton, reliance on environment variables is very bad for build cache systems. The core problem is deciding when to rebuild a crate vs use a cached build. Do you rebuild if anything in the env is changed? In practice that makes a cache mostly useless because environments vary a lot across machines for insignificant reasons. Do you use cache despite env changes? That means you'd be getting builds that silently nondeterministically disrespect important environment variables. Since there is no way to introspect what environment variables a macro looks at, it's impossible to have a useful deterministic cache. Cargo has big problems with this too; it prioritizes use of the local cache over correctness, so the environment is not taken into account when deciding whether to rebuild. In contrast, I suspect that the uses of environment variables you've seen are all examples of people "doing it wrong". |
@dtolnay Sorry for the late reply, I needed time to think it over. Well, it seems I have no more objection. We definitely can implement this. I also very much appreciate your in-detail explanation and reasoning, special thanks for cargo inside. @TeXitoi What is your opinion on this matter? |
If it can be used with cfg flags and feature, I'm OK with such an enhancement. |
But I still don't understand why you don't want "if the CARGO_PKG_VERSION env var is not setted, don't generate a |
@TeXitoi, I would be happy with that as well. |
@dtolnay I don't understand. You just said
Let's say you first build in "do have version" environment, then environment changes to "have no version" one. @TeXitoi I don't see how |
Structopt allready dépends on this env var son that's not "more" bad. Then I prefer a feature that's the cargo users can use than a cfg. |
I don't like it. I think if people expect to have a version for their app (I feel like most people do and @dtolnay's case is special here) and they would be upset if On the other hand, If your Don't get it wrong - I will help to implement this if you both come to agreement this is the best way, I just personally don't think it's the right way to go. |
In my case these systems would use totally separate caches so it's not a problem. Cargo does correctly rebuild when changing environment variables that it knows about, like CARGO_PKG_VERSION. Only non-Cargo variables like STRUCTOPT_SOMETHING would cause broken caching. I believe @TeXitoi and I are in agreement that #243 (comment) is an acceptable fix. |
I suggest |
@CreepySkeleton -- downsides of that approach are:
For an "explicitly set to off" build-system-wide setting, |
Makes sense, CARGO_PKG_VERSION is out of question. Maybe "CARGO_PKG_VERSION is not set" isn't that bad after all... |
Err, "CARGO_PKG_VERSION is not set" is worse than I thought because of
I suggest feature then |
@CreepySkeleton a feature is not appropriate because whether or not versions exist is a global property of the build system, not a decision that an individual package depending on structopt would make. See #243 (comment). "CARGO_PKG_VERSION is not set" is fine because the motivation for a global no_version setting is for monorepos -- where dozens to hundreds of projects live in the same repo, versions don't exist, and the build system is not Cargo. I believe @TeXitoi is on board with this based on #243 (comment). |
I can implement 1., that's 2 lines of code here structopt/structopt-derive/src/attrs.rs Line 579 in 14937e8
I'm OK for 2, but I'm really not found of features and cfg as it is a mess to properly test and maintain when the number of conditions grows. |
So what do we do? |
1 xor 2 xor (1 and 2) I don't care ;-) |
Aggh! |
Structopt currently looks at the environment variable $CARGO_PKG_VERSION to determine a version for the command line app. In non-Cargo environments this environment variable doesn't make sense and we would prefer to use no_version by default everywhere.
I would recommend for structopt-derive to accept a rustflag cfg that disables the check for CARGO_PKG_VERSION and assumes no_version by default.
RUSTFLAGS='--cfg structopt_no_version' cargo build ...
If a
version = "..."
attribute is present, that should take precedence over structopt_no_version.Implementation-wise this would look like
#[cfg(structopt_no_version)]
and#[cfg(not(structopt_no_version))]
codepaths in structopt-derive.cc @bolinfest
The text was updated successfully, but these errors were encountered: