-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 '-C' flag for changing current dir before build #10952
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @ehuss (or someone else) soon. Please see the contribution instructions for more information. |
The note about the trailing slash under "additional information" is in question. Subsequent retesting seems to suggest something else is going on. I am continuing to look into it. |
FYI I modified the following line in the PR description because it would cause github to close the issue when this PR is merged which we probably don't want to have happen
|
85ebc79
to
5daf7a8
Compare
This will be a help for cases like rust-lang#10952 which I would expect would assert that the config is not loaded before changing the current_dir.
This will be a help for cases like rust-lang#10952 which I would expect would assert that the config is not loaded before changing the current_dir.
refactor(cli): Lazy load config This is trying to clarify `-C` support when it is implemented in #10952 Cargo currently has two initialization states for Config, `Config::default` (process start) and `config.configure` (after parsing args). The most help we provide for a developer touching this code is a giant `CAUTION` comment in one of the relevant functions. Currently, #10952 adds another configuration state in the middle where the `current_dir` has been set. The goal of this PR is to remove that third configuration state by - Lazy loading `Config::default` so it can be called after parsing `-C` - Allowing `-C` support to assert that the config isn't loaded yet to catch bugs with it The hope is this will make the intent of the code clearer and reduce the chance for bugs. In doing this, there are two intermediate refactorings - Make help behave like other subcommands - Before, we had hacks to intercept raw arguments and to intercept clap errors and assume what their intention was to be able to implement our help system. - This flips it around and makes help like any other subcommand, simplifying cargo initialization. - We had to upgrade clap because this exposed a bug where `Command::print_help` wasn't respecting `disable_colored_help(true)` - Delay fix's access to config Personally, I also find both changes make the intent of the code clearer. To review this, I recommend looking at the individual commits. As this is just refactors, this has no impact on testing.
☔ The latest upstream changes (presumably #11029) made this pull request unmergeable. Please resolve the merge conflicts. |
This will be a help for cases like rust-lang#10952 which I would expect would assert that the config is not loaded before changing the current_dir.
This will be a help for cases like rust-lang#10952 which I would expect would assert that the config is not loaded before changing the current_dir.
Ping @compenguy. Just checking in to see if you are still interested in working on this, or if you had any questions we could help with. |
I'm still on this. It's mostly held up on getting the cli tests implemented, which I'm not entirely clear how to get started on, but if my day job can cut me a break at some point I should be able to put in the time to figure it out. |
Thanks for the response and take your time! Ed already set up a good lazy load mechanism in #11029 for you to move on. So basically we could have a directory changed before For writing tests, you could follow here or existing tests. You could create a new place like I'd suggest adding tests for at least the following scenarios:
You also need to add a doc for the new flag. You can find the doc of global options here. Note that every time you modify docs for cargo commands, you'll need to do this to rebuild manpages. I usually just run I am only suggesting, so please say it out loud if you have a better idea! And feel free to ask anything :) |
And in case this is a concern, most of our tests are calling into the |
Hi @compenguy, just checking in to see if you are still working on this issue. If not, may I handle the remaining parts? Thank you. |
d1956e4
to
7dea900
Compare
b82d946
to
6510cc5
Compare
Thank you! @rfcbot resolved bikeshed-flag-name |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Could you update the PR title and description to account for the change to only use -C
?
As the rfcbot was more used as a straw poll, this was pre-approved before the PR was made, and the flag situation has significant buy-in on zulip, I'm going to merge now rather than wait the 10 days (I updated the title and description) @bors r+ |
☀️ Test successful - checks-actions |
10 commits in 82c3bb79e3a19a5164e33819ef81bfc2c984bc56..39c13e67a5962466cc7253d41bc1099bbcb224c3 2023-02-04 22:52:16 +0000 to 2023-02-12 02:01:08 +0000 - chore: Update to toml v0.6, toml_edit v0.18 (rust-lang/cargo#11618) - doc: more doc comments and intra-doc links (rust-lang/cargo#11703) - Deny warnings in CI, not locally (rust-lang/cargo#11699) - add comment to lto.rs (rust-lang/cargo#11701) - Re-export cargo_new::NewProjectKind as public (rust-lang/cargo#11700) - Add '-C' flag for changing current dir before build (rust-lang/cargo#10952) - `-Zrustdoc-scrape-example` must fail with bad build script (rust-lang/cargo#11694) - Update CHANGELOG for 1.68 backports (rust-lang/cargo#11690) - Update 1password to the version 2 CLI (rust-lang/cargo#11692) - chore: autolabel more for `A-*` (rust-lang/cargo#11679)
Update cargo 10 commits in 82c3bb79e3a19a5164e33819ef81bfc2c984bc56..39c13e67a5962466cc7253d41bc1099bbcb224c3 2023-02-04 22:52:16 +0000 to 2023-02-12 02:01:08 +0000 - chore: Update to toml v0.6, toml_edit v0.18 (rust-lang/cargo#11618) - doc: more doc comments and intra-doc links (rust-lang/cargo#11703) - Deny warnings in CI, not locally (rust-lang/cargo#11699) - add comment to lto.rs (rust-lang/cargo#11701) - Re-export cargo_new::NewProjectKind as public (rust-lang/cargo#11700) - Add '-C' flag for changing current dir before build (rust-lang/cargo#10952) - `-Zrustdoc-scrape-example` must fail with bad build script (rust-lang/cargo#11694) - Update CHANGELOG for 1.68 backports (rust-lang/cargo#11690) - Update 1password to the version 2 CLI (rust-lang/cargo#11692) - chore: autolabel more for `A-*` (rust-lang/cargo#11679) r? `@ghost`
Update cargo 10 commits in 82c3bb79e3a19a5164e33819ef81bfc2c984bc56..39c13e67a5962466cc7253d41bc1099bbcb224c3 2023-02-04 22:52:16 +0000 to 2023-02-12 02:01:08 +0000 - chore: Update to toml v0.6, toml_edit v0.18 (rust-lang/cargo#11618) - doc: more doc comments and intra-doc links (rust-lang/cargo#11703) - Deny warnings in CI, not locally (rust-lang/cargo#11699) - add comment to lto.rs (rust-lang/cargo#11701) - Re-export cargo_new::NewProjectKind as public (rust-lang/cargo#11700) - Add '-C' flag for changing current dir before build (rust-lang/cargo#10952) - `-Zrustdoc-scrape-example` must fail with bad build script (rust-lang/cargo#11694) - Update CHANGELOG for 1.68 backports (rust-lang/cargo#11690) - Update 1password to the version 2 CLI (rust-lang/cargo#11692) - chore: autolabel more for `A-*` (rust-lang/cargo#11679) r? `@ghost`
Update cargo 10 commits in 82c3bb79e3a19a5164e33819ef81bfc2c984bc56..39c13e67a5962466cc7253d41bc1099bbcb224c3 2023-02-04 22:52:16 +0000 to 2023-02-12 02:01:08 +0000 - chore: Update to toml v0.6, toml_edit v0.18 (rust-lang/cargo#11618) - doc: more doc comments and intra-doc links (rust-lang/cargo#11703) - Deny warnings in CI, not locally (rust-lang/cargo#11699) - add comment to lto.rs (rust-lang/cargo#11701) - Re-export cargo_new::NewProjectKind as public (rust-lang/cargo#11700) - Add '-C' flag for changing current dir before build (rust-lang/cargo#10952) - `-Zrustdoc-scrape-example` must fail with bad build script (rust-lang/cargo#11694) - Update CHANGELOG for 1.68 backports (rust-lang/cargo#11690) - Update 1password to the version 2 CLI (rust-lang/cargo#11692) - chore: autolabel more for `A-*` (rust-lang/cargo#11679) r? `@ghost`
This implements the suggestion in #10098 to make cargo change cwd before
completing config processing and starting the build. It is also an
alternative to
--manifest-path
that resolves the issue describedin #2930.
The behavior of this new flag makes cargo build function exactly the same when run at the root of the project as if run elsewhere outside of the project. This is in contrast to
--manifest-path
, which, for example, results in a different series of directories being searched for.cargo/config.toml
between the two cases.Fixes #10098
Reduces impact of #2930 for many, possibly all impacted, by switching to this new cli argument.
How should we test and review this PR?
The easiest way to reproduce the issue described in #2930 is to create an invalid
.cargo/config.toml
file in the root of a cargo project, for example!
as the contents of the file. Running cargo with the current working directory underneath the root of that project will quickly fail with an error, showing that the config file was processed. This is correct and expected behavior.Running the the same build with the current working directory set outside of the project, e.g. /tmp, and passing the
--manifest-path /path/to/project/Cargo.toml
. The build will proceed without erroring as a result of reading the project's.cargo/config.toml
, because config file searching is done from cwd (e.g. in/tmp
and/
) without including the project root, which is a surprising result in the context of the cargo config documentation, which suggests that a.cargo/config.toml
file checked into the root of a project's revision control will be processed during the build of that project.Finally to demonstrate that this PR results in the expected behavior, run cargo similar to the previous run, from /tmp or similar, but instead of
--manifest-path /path/to/project/Cargo.toml
, pass-C/path/to/project
to cargo (note the missingCargo.toml
at the end). The build will provide the exact same (expected error) behavior as when running it within the project root directory.Additional information
Passing a path with a trailing '/' will result in failure. It is unclear whether this is a result of improper input sanitization, or whether the config.cwd value is being improperly handled on output. In either case this needs to be resolved before this PR is merge-ready.(the above issue appears to have been a side effect of local corruption of my rustup toolchain, and unrelated to this change)
Because a
struct Config
gets created before command line arguments are processed, a config will exist with the actual cwd recorded, and it must then be replaced with the new value after command line arguments are processed but before anything tries to use the stored cwd value or any other value derived from it for anything. This change effectively creates a difficult-to-document requirement during cargo initialization regarding the order of events. For example, should a setting stored in a config file discovered via cwd+ancestors search be wanted before argument processing happens, this could result in unpleasant surprises in the exact use case this feature is being added to fix.A long flag was deferred out to not block this on deciding what to name it. A #11696 was created to track this.