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

Add cargo config subcommand. #9302

Merged
merged 4 commits into from
Mar 30, 2021
Merged

Add cargo config subcommand. #9302

merged 4 commits into from
Mar 30, 2021

Conversation

ehuss
Copy link
Contributor

@ehuss ehuss commented Mar 25, 2021

This adds an initial version of the cargo config command as discussed in #2362.

Closes #2362

@rust-highfive
Copy link

r? @Eh2406

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 25, 2021
@ehuss
Copy link
Contributor Author

ehuss commented Mar 25, 2021

Open questions

  • Is there any preference in style for the --merged flag. Should it be --merged=yes/no or --no-merged?

Future work

  • Warnings for unknown keys aren't really handled. Not sure if this is possible.
  • Quoted keys aren't handled as CLI input. For example, config get 'target."cfg(target_os=\"linux\")".runner' Unfortunately toml doesn't expose the parsing functions to make this easy. Could do from_str("{} = 1", key), and then collect all the key components. I wouldn't want that in the normal Config.get() path, though, for performance.
  • JSON doesn't show environment variables. Not sure how to handle that.
  • The original design doc mentioned a few other output formats, but I only implemented 3.
  • The design doc mentioned future subcommands like set, delete, and edit.

Environment variable handling

I'm a little unhappy with the way environment variables are handled. If a command like cargo config get profile.dev is run, and the environment variable CARGO_PROFILE_DEV_OPT_LEVEL=3 is set, then it will only display the environment variable as a note in a comment instead of actually part of the value output. For json output, it is not displayed at all.

If getting a non-table value (like cargo config get profile.dev.opt-level), then it should work correctly and use the environment variable.

I'm not sure how to go about solving that. The only solution I can think of is defining some kind of schema that describes the structure of the config types. I can't think of a way to do this with serde.

Even with that, there are some "lazy" environment variables that I think would be impossible to handle correctly. They are:

  • CARGO_PROFILE_*_…
  • CARGO_ALIAS_*
  • CARGO_REGISTRIES_*_…
  • CARGO_TARGET_*_…

@ehuss ehuss mentioned this pull request Mar 25, 2021
13 tasks
@Revantus
Copy link
Contributor

Personally I prefer the --no-merged approach. Testing locally it seems that --merged is the default when using cargo config get which makes choosing --merged and --merged yes feel kind of redundant.

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Is there any preference in style for the --merged flag. Should it be --merged=yes/no or --no-merged?

Personally over the years I've come to appreciate --flag=val more than --no-flag, so I'd go for --merged. I don't feel strongly on this though.

JSON doesn't show environment variables. Not sure how to handle that.

Since JSON goes to stdout, could warnings about this go to stderr perhaps?

I'm not sure how to go about solving that. The only solution I can think of is defining some kind of schema that describes the structure of the config types. I can't think of a way to do this with serde.

Right now all of Cargo's configuration is entirely untyped and accessed on-the-fly so one of the issues with your profile example is that within Cargo we're always accessing profile configuration in a typed manner, but with this new subcommand it's instead all untyped so we don't know what to do with the map.

One possible alternative is we instead "register" configuration values with Config early on, so Config learns about the structure of all expected configuration. Basically your schema idea but with registration up-front of what the schema is. I have no idea how to do this in an ergonomic fashion though and it's definitely out of the scope of this PR.


fn print_json(config: &Config, key: &ConfigKey, cv: &CV, include_key: bool) {
let json_value = if key.is_root() || !include_key {
match cv {
Copy link
Member

Choose a reason for hiding this comment

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

Could this be deduplicated with json_add below where there's a function from CV to a json value?

@ehuss
Copy link
Contributor Author

ehuss commented Mar 29, 2021

Since JSON goes to stdout, could warnings about this go to stderr perhaps?

Yea, I felt a little uncomfortable with that, but I can't say why exactly. I went ahead and pushed a commit that prints a note to stderr.

I have no idea how to do this in an ergonomic fashion though and it's definitely out of the scope of this PR.

Yea, I will try to think it over for a while. I fear it will be a lot of code and changes, just to accommodate this one use case. I don't think it isn't super important right now. My primary use case is just to help people debug configuration settings, and printing the environment variables is sufficient to me for that purpose.

@alexcrichton
Copy link
Member

Ok that all sounds good to me!

@bors: r+

@bors
Copy link
Collaborator

bors commented Mar 30, 2021

📌 Commit 23d4a68 has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 30, 2021
@bors
Copy link
Collaborator

bors commented Mar 30, 2021

⌛ Testing commit 23d4a68 with merge 7204d39...

@bors
Copy link
Collaborator

bors commented Mar 30, 2021

☀️ Test successful - checks-actions
Approved by: alexcrichton
Pushing 7204d39 to master...

@bors bors merged commit 7204d39 into rust-lang:master Mar 30, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 1, 2021
Update cargo

5 commits in 1e8703890f285befb5e32627ad4e0a0454dde1fb..3c44c3c4b7900b8b13c85ead25ccaa8abb7d8989
2021-03-26 16:59:39 +0000 to 2021-03-31 21:21:15 +0000
- Fix semver docs for 1.51. (rust-lang/cargo#9316)
- Add `cargo config` subcommand. (rust-lang/cargo#9302)
- Give one more example for the --featuers CLI (rust-lang/cargo#9313)
- Bump to 0.54.0, update changelog (rust-lang/cargo#9308)
- Make the URL to the tracking issue for `--out-dir` into a link (rust-lang/cargo#9309)
@ehuss ehuss added this to the 1.53.0 milestone Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Finish implementing cargo config and related support
6 participants