Skip to content

fix(context): merge files and --config as layers - #17775

Merged
epage merged 6 commits into
rust-lang:masterfrom
weihanglo:config-layers
Nov 20, 2026
Merged

epage merged 6 commits into
rust-lang:masterfrom
weihanglo:config-layers

Conversation

@weihanglo

@weihanglo weihanglo commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

What does this PR try to resolve?

After this,
GlobalContext now builds config from the same layers
cargo config get --merged=no does.
That is, from the new ConfigStore::from_layers (name open to change)

The user visible changes are only in merge-failure errors:

  • higher precedence side now show first for also includes and --config,
    like we already do for different config files
  • include conflicts no longer say "failed to load config include",
    a small regression but it doesn't really add more useful info
    (and anyway too verbose ATM)

Final merged values are unchanged.

Notes to reviewers

This is a step toward merging every ConfigStore layer in precedence order.


🤖 LLM involvement: Prototyping and blessing snapshots.

@rustbot rustbot added A-configuration Area: cargo config files and env vars S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 20, 2026
@rustbot

rustbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @Muscraft, @epage, @weihanglo
  • @Muscraft, @epage, @weihanglo expanded to Muscraft, epage, weihanglo
  • Random selection from Muscraft, epage

@weihanglo weihanglo changed the title fix(context): ensure config merge in precedence order fix(context): merge files and --config as layers Nov 20, 2026
@weihanglo weihanglo changed the title fix(context): merge files and --config as layers fix(context): merge files and --config as layers Nov 20, 2026
Comment thread src/context/loader.rs Outdated
.context("could not load Cargo configuration")?;
// Merge from highest precedence, so ties keep the value already merged.
for mut layer in self.load_unmerged_from(path)?.into_iter().rev() {
layer.values_mut().remove("include");

@epage epage Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This got introduced but I'm not seeing the old remove being removed?

View changes since the review

@weihanglo weihanglo Nov 20, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed because the merged view now shares the same code path with cargo config get --merge=no, and the latter passes remove=false to include_paths so no remove at all. We need to remove it at merge time then, otherwise it would show up in cargo config get merged version, as well as in merged ConfigStore (unsure if it should for the latter though, but we should keep the behavior first).

Unsure if we want a regression test, maybe?

BTW once cli merge starts using layer merge the remove argument should be gone.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test.

BTW once cli merge starts using layer merge the remove argument should be gone.

Also the removal will be only in ConfigStore::from_layers at merge time

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My in-work PR moves the remove call into the caller, so been dealing a bit with this.

Still at bit confused. Didn't you effectively move it into the caller here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--config through load_cli_args -> load_includes -> include_paths still pass remove=true so this PR does not yet completely remove that.

Happy to push the whole temporary stack if you want to take a look (still tweaking, rebasing, refacotring though)

Or I can wait and build upon yours, or vice versa, whichever works.

@epage

epage commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

aside: Starting to wonder if we should load highest precedence first so there is less reversing everywhere...

this is a refactor,
but doing so swaps the merge base and incoming in error message.

Also, a merge failure involving an `include`d file
no longer reported with its own line.
The later context still gives the full path.
I think this is a fine tradeoff.
And it is anyway too verbose right now
Before,
`--config` values were forced to be on top of the merged config.
After,
they are now merged by the new `ConfigStore::from_layers`,
no longer too special.

Some user-observable changes:

* A mergeable list set by both a file and `--config`
  now reports the list container's definition as `--config`
  This probably doesn't matter that much,
  but it is consistent with other precedence kinds.
* A merge failure between a file and `--config`
  now calls out `--config` (higher precedence one) first
  in the error message.
@weihanglo

Copy link
Copy Markdown
Member Author

aside: Starting to wonder if we should load highest precedence first so there is less reversing everywhere...

I guess the switch may be easier when the whole stack is done. I would have one or two more pull requests to get there.

@epage
epage added this pull request to the merge queue Nov 20, 2026
Merged via the queue into rust-lang:master with commit 9ae79c9 Nov 20, 2026
30 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 20, 2026
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-rust-lang__cargo that referenced this pull request Nov 20, 2026
…ang#17780)

### What does this PR try to resolve?

After this, `--config` load and merge now uses the same underlying
mechanism as config files. It is just another store layer and no longer
special.

This is mostly a refactor PR, but it does comes with some user-visible
error messages changes:

* It has the same swap like
<rust-lang#17775> in merge failure, where
high precedence item shows up first.
* The other is "failed to load config from `[ROOT]/cli.toml`" becomes
"failed to merge --config argument `cli.toml`", which I think is clearer
(also due to the change how we load `include`s from `--config`)

Also `MergePriority` got removed in this PR.

### Notes to reviewers

This is the main PR before moving `ConfigLoader` to cargo-util-config.
The rest is moving code and rename the API I believe.

I hope this doesn't step on on Ed's toes too hard :)

---

🤖 LLM involvement: LLM did a great job at splitting commits for me. What
I need to do is shareing the epage PR style page, and show some
well-split PR from Ed and :shipit:. Also used for directed prototyping.
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Nov 22, 2026
Update cargo submodule

28 commits in f93c676e3e7ab1aee08dc95d869700f6b99a1a20..ae8f06eb41b8c4a534d0f280f1b9d287f45ead73
2026-11-15 17:27:10 +0000 to 2026-11-21 04:06:35 +0000
- feat(cargo-util-config): expose config loader (rust-lang/cargo#17781)
- feat(config): Add IncludeConfig (rust-lang/cargo#17782)
- fix(config): `--config` use same layers merge as config files (rust-lang/cargo#17780)
- refactor(config): Move include removal up a layer, making include_paths (rust-lang/cargo#17776)
- Support wasm64-unknown-emscripten (rust-lang/cargo#17620)
- fix(context): merge files and `--config` as layers (rust-lang/cargo#17775)
- fix(config)!: Really ensure no comments are present  (rust-lang/cargo#17774)
- fix(config): list unmerged config in precedence order  (rust-lang/cargo#17773)
- fix(config): Handle parsing dotted keys using TOML keys  (rust-lang/cargo#17770)
- fix(config): Improve config schema by implementing Serialize (rust-lang/cargo#17762)
- fix(config): drop redundant config merge error context (rust-lang/cargo#17769)
- fix(config): keep winning definition for non-mergeable list  (rust-lang/cargo#17766)
- test(build-analysis): update for new section timing order (rust-lang/cargo#17767)
- docs(ref): Deprecate 'workspace.package.authors' (rust-lang/cargo#16951)
- Fix git checkout long paths (rust-lang/cargo#17752)
- fix(config): layer loads to work the same as merged load  (rust-lang/cargo#17761)
- refactor(config): Switch another location to `resolve` (rust-lang/cargo#17760)
- refactor(config): Cleanup IncludeConfig by adding ConfigParentRelativePath (rust-lang/cargo#17759)
- docs(lints): Remove nonexistent lints from cargo::default desc (rust-lang/cargo#17757)
- Add period to cargo-toml-vs-cargo-lock.md for clarity. (rust-lang/cargo#17754)
- fix(config)!: Be more explicit about what ConfigRelativePaths are relative to (rust-lang/cargo#17751)
- Fix ‘dependencoes’ typos (rust-lang/cargo#17753)
- refactor(credential)!: replace time with `[email protected]` (rust-lang/cargo#17750)
- refactor(context): miscellaneous loader cleanup (rust-lang/cargo#17747)
- docs(profiles): fix outdated default (rust-lang/cargo#17748)
- chore(config): migrate Renovate config (rust-lang/cargo#17746)
- refactor(context): a `ConfigLoader` type (rust-lang/cargo#17745)
- refactor: modernize with 1.100 API (rust-lang/cargo#17728)
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Nov 22, 2026
Update cargo submodule

28 commits in f93c676e3e7ab1aee08dc95d869700f6b99a1a20..ae8f06eb41b8c4a534d0f280f1b9d287f45ead73
2026-11-15 17:27:10 +0000 to 2026-11-21 04:06:35 +0000
- feat(cargo-util-config): expose config loader (rust-lang/cargo#17781)
- feat(config): Add IncludeConfig (rust-lang/cargo#17782)
- fix(config): `--config` use same layers merge as config files (rust-lang/cargo#17780)
- refactor(config): Move include removal up a layer, making include_paths (rust-lang/cargo#17776)
- Support wasm64-unknown-emscripten (rust-lang/cargo#17620)
- fix(context): merge files and `--config` as layers (rust-lang/cargo#17775)
- fix(config)!: Really ensure no comments are present  (rust-lang/cargo#17774)
- fix(config): list unmerged config in precedence order  (rust-lang/cargo#17773)
- fix(config): Handle parsing dotted keys using TOML keys  (rust-lang/cargo#17770)
- fix(config): Improve config schema by implementing Serialize (rust-lang/cargo#17762)
- fix(config): drop redundant config merge error context (rust-lang/cargo#17769)
- fix(config): keep winning definition for non-mergeable list  (rust-lang/cargo#17766)
- test(build-analysis): update for new section timing order (rust-lang/cargo#17767)
- docs(ref): Deprecate 'workspace.package.authors' (rust-lang/cargo#16951)
- Fix git checkout long paths (rust-lang/cargo#17752)
- fix(config): layer loads to work the same as merged load  (rust-lang/cargo#17761)
- refactor(config): Switch another location to `resolve` (rust-lang/cargo#17760)
- refactor(config): Cleanup IncludeConfig by adding ConfigParentRelativePath (rust-lang/cargo#17759)
- docs(lints): Remove nonexistent lints from cargo::default desc (rust-lang/cargo#17757)
- Add period to cargo-toml-vs-cargo-lock.md for clarity. (rust-lang/cargo#17754)
- fix(config)!: Be more explicit about what ConfigRelativePaths are relative to (rust-lang/cargo#17751)
- Fix ‘dependencoes’ typos (rust-lang/cargo#17753)
- refactor(credential)!: replace time with `[email protected]` (rust-lang/cargo#17750)
- refactor(context): miscellaneous loader cleanup (rust-lang/cargo#17747)
- docs(profiles): fix outdated default (rust-lang/cargo#17748)
- chore(config): migrate Renovate config (rust-lang/cargo#17746)
- refactor(context): a `ConfigLoader` type (rust-lang/cargo#17745)
- refactor: modernize with 1.100 API (rust-lang/cargo#17728)
@rustbot rustbot added this to the 1.102.0 milestone Nov 22, 2026
@weihanglo
weihanglo deleted the config-layers branch November 23, 2026 20:47
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants