-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Cargo should accumulate rustflags from all sources, rather than from only one source #6338
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
Comments
While I'm not sure of how to solve the issue, the setup of having environment variables trump configuration files is common to the point I think it's the expected behaviour, making accumulating break many people's (IMHO) expectations. |
[citation needed] I haven't seen many build tools read both an envvar and a user configuration file for the same setting. Usually a high-level tool controls the envvar and that is passed to the low-level tool as a command-line arg, there is a clear separation of concerns. A common pattern is to offer multiple envvars and use all of them, with the intention / naming reflecting who is supposed to set them (system-wide vs per-package vs user default vs user temporary override) and to have later ones take precedence and be able to override earlier ones. For example, see autoconf. This sort of behaviour is more predictable as people don't really expect the presence/absence of an envvar to completely change something else, but only to tweak it. To disable a config file completely, it is customary to explicitly require a command-line flag like "--no-rc" or something. The main thing I am concerned about here is that if a crate author sets |
Apologies, I meant an environment variable overrides a specific setting in a configuration file. I have no citation, but my intuition is: command line option > environment flag > configuration file. However, when the setting is not for a single value but for a list of values I don't think there's an established practice, especially one that differentiates between overriding, prepending and appending, which is exactly the case you're facing. If you need to prepend to WDYT? |
Look at the autoconf practise that goes back several decades, it (to repeat myself):
This relies on the fact that
It would be better to do this in However I still consider this suboptimal to cargo just simply reading from all sources and composing everything together. I'd say this is most intuitive and predictable when looking at large-scale behaviour of how you want complex buildsystems to interact. For example your "intuition":
doesn't even match the current situation, it is not "command line option > environment flag", what happens is that cargo reads "environment flag > user config > crate config" and then passes one of the previous as the command-line flags to rustc. Anyway what is actually the issue here is not the mechanism (cli vs envvar vs config) but reading flags from different conceptual sources i.e.
Here I agree it is "obvious" the precedence should go 1 > 2 > 3, however the behaviour shouldn't be to override lower-precedence ones, it should be to append to the lower-precedence ones. Then rustc will treat later (appended) flags as higher-precedence if they "conflict" with an earlier (prepended) flag - and it already does this, this is conventional behaviour for pretty much all compilers and other programs. |
Ah, I understand what you're saying. Makes sense to me now (sorry). |
In the wild I've seen both use cases for overriding and for appending, so we may not want to use RUSTFLAGS literally but it seems reasonable to have some configuration for pulling in custom flags from multiple sources. |
For the case of prepending and overwriting, those might be useful when cross compiling. In my case though, cross-compile led me to "file not recognized: file format not recognized" error. For example,
Maybe I haven't found a better way, but from my perspective as user, prepending the flag will solve my common cross-compiling problem here. Which I think due to the precedence of |
There's a similar issue here:
This is hazardous because we use the second workaround suggested in rust-lang/rust#47551 (comment) to avoid a musl backtrace issue. That means that crates which define custom |
This issue is almost identical to #5376, so I am going to close in favor of that so that this is tracked/discussed in one place. |
In Debian Rust package builds we would like to apply e.g.
-C linker
and--remap-path-prefix
in all situations. We currently do this by settingCARGO_HOME
to point to a directory containing aconfig
file with contents similar to the following:However if
RUSTFLAGS
is set (e.g. to empty) in the environment, the above build.rustflags will be totally ignored.The only method we have currently, to absolutely ensure that certain flags are set, is to append to
RUSTFLAGS
itself, but this is ugly because it does not support escaping of spaces.The proposal here is to change
env_args
to instead use all the sources listed there, in order of priority - that is, set the effective rust flags to beconfig/build.rustflags + Cargo.toml/target.rustflags + RUSTFLAGS
, with later flags taking precedence.The text was updated successfully, but these errors were encountered: