Skip to content

Conversation

weihanglo
Copy link
Member

@weihanglo weihanglo commented Oct 13, 2025

What does this PR try to resolve?

This adds the support of array of any types in Cargo config.
The motivation of this is mostly from the proposed -Zconfig-include syntax (#7723 (comment))

[[include]]
path = "a.toml"
optional = true
[[include]]
path = "b.toml"
optional = false

which before this Cargo only supported array of string.

Some design decisions:

  • Insta-stabilization: This is insta- stabilizing a extension of Cargo config. See feat: support array of any types in Cargo config #16103 (comment)
  • No CARGO_* env support added — Only the --config CLI and file-based configuration start supporting array of any types. CARGO_* config environment still doesn't. The only type you can set through environment variable are still primitive types. For advanced env usage, we left it as follow-up to deal with in -Zadvanced-env
  • Nothing changed in config merging rule — Nested complex types in array won't merge with any other items, as it still respects the current array merging rule, which appends items.
  • cargo config get now prints inline array/tables for array of nested array/tables.

fixes #16111

How to test and review this PR?

Starting from the fifth commit, those changes are for handling diagnostic regression, as now Config::from_toml accepts any types and the invalid type error is delayed from the "TOML->ConfigValue" deserialization to "ConfigValue->Target" Type deserialization. Not sure if we should split it to a separate PR, but the solution here I admit is a bit nasty.

@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 Oct 13, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 13, 2025

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

@weihanglo weihanglo changed the title feat: support array of any types in Cargo Config feat: support array of any types in Cargo config Oct 13, 2025
Caused by:
expected string but found integer at index 0
[ERROR] invalid type: integer `1`, expected a string
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the error message regression that the next commit want to deal with

Comment on lines 472 to 480
seed.deserialize(de)
.map_err(|e| {
// This along with ArrayItemKeyPath provide a better error context of the
// ConfigValue definition + the key path within an array item that native
// TOML key path can't express. For example, `foo.bar[3].baz`.
key_path.push_index(i);
e.with_array_item_key_context(&key_path, definition)
})
.map(Some)
Copy link
Member Author

@weihanglo weihanglo Oct 14, 2025

Choose a reason for hiding this comment

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

While it indeed fixes the this might the less good approach dealing with the diagnostic issue in general. The current approach only track the key path for array items when deserialzation fails. If caller requests for Vec<Value<String>> and performs post-deserialization validation, the key path info is missing for a better error context.

We might want to add an extra optional field array_item_path to Value<T> to preserve the information though. That is why I feel like may we should put off the second half of this PR.

Copy link
Member Author

@weihanglo weihanglo Oct 14, 2025

Choose a reason for hiding this comment

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

Think more about it. The other option is doing nothing and staying with the current collect-on-error implementation. The caller should be able to compute the index of an item given it has the full picture of the array.

Copy link
Contributor

Choose a reason for hiding this comment

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

Some design decisions:

This is insta- stabilizing config features that have no use and we could overlook the stabilization conversation on the design for this when making use of it because it exists and is "stable".

Copy link
Member Author

Choose a reason for hiding this comment

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

Do we want to tie it with a feature like -Zconfig-include? I just feel the PR will be huge. Maybe we can put the support only on nightly until we are going to stabilize a use case of it, though I am not sure how the code would look like with that.

Copy link
Member Author

Choose a reason for hiding this comment

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

This demonstrates what we have supported today: no nested arrays
Actually it is not supported yet.
Will do in the next few commits.
Only add the support to TOML-based Cargo config.
Environment variables still only supports array of strings.
`-Zadvanced-env` is not yet supported.

There is a regression in
`cargo_alias_config::alias_malformed_config_list`
that the key path error report is missing.

This may require a revamp over ConfigKey.
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 S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support array of any types in Cargo config

3 participants