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

A way to verify that --config KEY=VALUE with a complex TOML key is modifying the config in the desired way #11667

Closed
Igonato opened this issue Feb 2, 2023 · 5 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@Igonato
Copy link

Igonato commented Feb 2, 2023

Problem

I'm trying to use the --config argument with accordance to the docs ...reference/config.html#command-line-overrides. The "Example of a complex TOML key"

cargo --config "target.'cfg(all(target_arch = \"arm\", target_os = \"none\"))'.runner = 'my-runner'"

looks like the functionality that I'm after, but when trying to adapt the example to my task, I'm not seeing the desired result.

It would be nice to have a way to sanity check if the right config value was indeed overridden. For example, by outputting the resulting config (with all the defaults and overrides) with a separate command or when -vv flag is set.

I'm very new to Rust, so having difficulties navigating the codebase. I see that the clap crate is used to parse the command line arguments, but I don't see anything TOML related in its docs, so I assume it just collects String values and parsing and merging done somewhere else, can't find where. In the Cargo Contributor Guide/Architecture/Files I see:

TomlManifest contains the contents of the Cargo.toml file. It is translated to a Manifest object for some simplification, and the Manifest is stored in a Package.

Can't find a place where the merging takes place in those files either (but I might be entirely missing it).

Summary

Basically, I'm looking for a place to shove in a dbg!(merged_config) and see the results of my --config KEY=VALUE manipulations. And if this was a standard feature with -vv or a separate flag - it would be great.

Proposed Solution

  • Print merged config when -vv flag is set or with a separate flag/command.
  • Improve the documentation with more examples of the "complex TOML syntax".
@Igonato Igonato added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Feb 2, 2023
@weihanglo
Copy link
Member

weihanglo commented Feb 2, 2023

Thanks for the well-written report!

Can't find a place where the merging takes place in those files either (but I might be entirely missing it).

Cargo configurations (.cargo/config.toml) are different from the manifest files (Cargo.toml). Config values merging lives here. The whole module contains types for configurations and their discovery as well.

Print merged config when -vv flag is set or with a separate flag/command.

IIRC there is an unstable command cargo config get exactly for this purpose. To verify the merge, you could run

cargo +nigntly -Z unstable-options \
  --config "target.'cfg(all(target_arch = \"arm\", target_os = \"none\"))'.runner = 'my-runner'" \
  config get --show-origin

Improve the documentation with more examples of the "complex TOML syntax".

It is not immediately clear what you mean. The KEY=VALUE is effectively TOML syntax (with some escapes). Could you elaborate?

@Igonato
Copy link
Author

Igonato commented Feb 2, 2023

@weihanglo thanks for the reply!

Cargo configurations (.cargo/config.toml) are different from the manifest files (Cargo.toml)

I think this might be the source of my problems. I want to specifically override a value from Cargo.toml in cargo build command, and I thought that's what the --config argument is about. So if I need something that doesn't specifically overlap between the two, I'm out of luck?

Otherwise, the unstable cargo config looks like the tool for the job as it is described in the OP. I'll try it later today and get back to confirm/close the issue.

Could you elaborate?

With ...'cfg(all(...))'.key = 'foo' I can't find cfg() or all() documented at toml.io, so I assume it's something homebrewed by the library(?) that performs .toml processing in cargo.

@Igonato
Copy link
Author

Igonato commented Feb 2, 2023

#6583 seems very much related to what I originally wanted.

@Igonato
Copy link
Author

Igonato commented Feb 2, 2023

#9301 > tracking issue for cargo config get

Tested cargo config get, it doesn't show the default, but does show the --config override:

$ cargo +nightly -Z unstable-options config get build.rustc
error: config value `build.rustc` is not set

$ cargo +nightly -Z unstable-options config get build.rustc --config 'build.rustc="myrustc"'
build.rustc = "myrustc"

I guess this solves the problem as described in the OP.

@Igonato Igonato closed this as completed Feb 2, 2023
@weihanglo
Copy link
Member

With ...'cfg(all(...))'.key = 'foo' I can't find cfg() or all() documented at toml.io, so I assume it's something homebrewed by the library(?) that performs .toml processing in cargo.

It's not anything special; the whole key is just a string in TOML. Cargo parses it to give it the ability to specify stuff conditionally, similar to what #[cfg] does in Rust source code. See this link for more: https://doc.rust-lang.org/nightly/cargo/reference/config.html#target

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

2 participants