AIlow to unset .cargo/config
settings through a new reset
config key #8687
Description
Describe the problem you are trying to solve
Cargo configuration files (.cargo/config
) are very useful for setting reasonable default values for a project. Since there are sometimes special cases where these default value do not work, cargo allows overriding config settings through a lower-level .cargo/config
file, the config cli feature, or environment variables.
While it is possible to override a configuration key with a different value, there is currently no way to unset a configuration key. This is problematic because for some keys the behavior of cargo already changes if the key is present, independent of the assigned value. One example is the build.target
flag for specifying a default target, which configures cargo to pass a --target
argument to rustc. This leads to different behavior than a build with no --target
argument, e.g. the output is placed in target/target-name/debug
instead of target/debug
.
Another example is the unstable
table, for example the unstable.build-std
key. As soon as it's present, cargo tries to recompile (parts of) the standard library, which fails when no --target
argument is passed. As for the build.target
key, there is no way to unset the config key again as soon as it's enabled.
Another class of config keys that cannot be unset are array keys like build.rustflags
. Since they are merged with previous arrays, there is no way to remove a rustflags
argument set in a parent directory.
Describe the solution you'd like
Add a new reset
array to cargo configuration files, which specfies a list of configuration keys that should be unset again. For example, reset = ["build"]
would unset the complete [build]
table of previously parsed configuration and reset = ["build.target", "unstable.build-std"]
would only reset the two specific keys.
The reset key would be processed by Cargo in the configuration merge step. It would result in the removal of the specified config keys from the parsed data. As a result, lower-level config files or the config-cli
feature would be able to reset specfic keys of already parsed config files back to their "not set" state. After the reset, it is still possible to set new values. This way, it would be possible to replace a build.rustflags
array with a different array without any merging.
Since the reset
key is a normal config value, it can not only be set from config files, but also through a --config
command line argument or even through a config environment variable.
Notes
This would resolve #8112, #8638, and #6775.
I originally had this idea while writing the internals post about .cargo/config problems
.