Skip to content

Environment variables override cargo --config <filename> in precedence #10992

@sunshowers

Description

@sunshowers

Problem

If both an environment variable and cargo --config <filename> are specified at the same time, the environment variable overrides the file passed in over the CLI. This is counter-intuitive and also the documentation at https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides is unclear about this:

Configuration values specified this way take precedence over environment variables, which take precedence over configuration files.

Steps

Create .cargo/foo.toml:

[build]
target-dir = "from-foo-toml"

Then, run:

CARGO_BUILD_TARGET_DIR=from-env cargo build --config .cargo/foo.toml

Notice that the build happens in "from-env" rather than the expected "from-too-toml".

Possible Solution(s)

I think the behavior should be changed so that config files passed in over --config come first and environment variables come later.

(Unlike #10991, this I think is straight-up incorrect and should be fixed.)

Notes

I'm guessing this is a regression from #10755 and specifically this change, though I could be wrong. cc @jonhoo, @ehuss

Version

cargo 1.63.0 (fd9c4297c 2022-07-01)
release: 1.63.0
commit-hash: fd9c4297ccbee36d39e9a79067edab0b614edb5a
commit-date: 2022-07-01
host: x86_64-unknown-linux-gnu
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.83.1-DEV (sys:0.4.55+curl-7.83.1 vendored ssl:OpenSSL/1.1.1n)
os: Pop!_OS 22.04 (jammy) [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-config-cliArea: --config CLI optionC-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions