Skip to content
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

Cannot merge env vars in [env] due to type mismatches #14932

Open
weihanglo opened this issue Dec 14, 2024 · 1 comment
Open

Cannot merge env vars in [env] due to type mismatches #14932

weihanglo opened this issue Dec 14, 2024 · 1 comment
Labels
A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables C-bug Category: bug S-triage Status: This issue is waiting on initial triage.

Comments

@weihanglo
Copy link
Member

Problem

Cargo doesn't know how to merge toml strings and tables for the [env] table.

Steps

mkdir outer
cd outer
mkdir .cargo
echo -e "[env]\nFOO = { value = '0' }" > .cargo/config.toml

cargo new inner
cd inner
mkdir .cargo
echo -e "[env]\nFOO = '1'" > .cargo/config.toml
cargo b

And you will see the error.

error: could not load Cargo configuration

Caused by:
  failed to merge configuration at `/home/user/inner/.cargo/config.toml`

Caused by:
  failed to merge key `env` between /home/user/inner/outer/.cargo/config.toml and /home/user/inner/.cargo/config.toml

Caused by:
  failed to merge key `FOO` between /home/user/inner/outer/.cargo/config.toml and /home/user/inner/.cargo/config.toml

Caused by:
  failed to merge config value from `/home/user/inner/.cargo/config.toml` into `/home/user/dirty/inner/outer/.cargo/config.toml`: expected string, but found table

Possible Solution(s)

According to The Cargo Book, there is no merge strategy for TOML table.

If a key is specified in multiple config files, the values will get merged together. Numbers, strings, and booleans will use the value in the deeper config directory taking precedence over ancestor directories, where the home directory is the lowest priority. Arrays will be joined together with higher precedence items being placed later in the merged array.

For what I can recall, it is because TOML table merge is handled case by case. I think it makes sense for the [env] table to just replace one with the other, regardless of string or table.

Notes

No response

Version

* cargo 1.83.0 (5ffbef321 2024-10-29)
* cargo 1.85.0-nightly (20a443231 2024-12-06)
@weihanglo weihanglo added A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Dec 14, 2024
@arlosi
Copy link
Contributor

arlosi commented Dec 18, 2024

I think it makes sense for the [env] table to just replace one with the other, regardless of string or table.

When merging, cargo doesn't know that it's the [env] table. The merging is done early, so whatever merging strategy needs to be done consistently for all config. I'm not sure what other examples we have where a config value could be legally changed from a table / list to something else. It might be good to enumerate them all.

// Allow switching types except for tables or arrays.
(expected @ &mut CV::List(_, _), found)
| (expected @ &mut CV::Table(_, _), found)
| (expected, found @ CV::List(_, _))
| (expected, found @ CV::Table(_, _)) => {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables C-bug Category: bug S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

No branches or pull requests

2 participants