Skip to content

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

Open
@weihanglo

Description

@weihanglo

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-configurationArea: cargo config files and env varsA-environment-variablesArea: environment variablesC-bugCategory: bugS-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions