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

cli: exit gracefully when no subcommand #2427

Merged
merged 1 commit into from
Aug 13, 2020
Merged

cli: exit gracefully when no subcommand #2427

merged 1 commit into from
Aug 13, 2020

Conversation

leighmcculloch
Copy link
Contributor

@leighmcculloch leighmcculloch commented Jul 17, 2020

What

Exit from rustup CLI gracefully when no subcommand is provided.

Why

That appears to be the intention because the SubcommandRequiredElseHelp app setting is configured on the CLI, however that is not the behavior because the main function treats the error indicating no subcommand was provided as an error.

pub fn cli() -> App<'static, 'static> {
let mut app = App::new("rustup")
.version(common::version())
.about("The Rust toolchain installer")
.after_help(RUSTUP_HELP)
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::SubcommandRequiredElseHelp)

Instead of exiting gracefully the word error: is printed with no error message:
Screen Shot 2020-07-17 at 12 22 44 AM

Fixes #2425

Known Limitations

I have not added tests for this change. I'm rather new to rust and this codebase and I could not find tests for this code to modify or change.

@kinnison
Copy link
Contributor

As noted here: #2425 (comment) I think that we might want to investigate why clap isn't returning clap::ErrorKind::HelpDisplayed

@leighmcculloch
Copy link
Contributor Author

I think clap is returning the correct error, it explicitly returns this error when this feature is enabled:

https://github.com/clap-rs/clap/blob/e8d46f403671ebdd14f4f530f82f6ba6fe9c78cc/src/parse/parser.rs#L750-L756

@leighmcculloch
Copy link
Contributor Author

clap does the same thing for the similar feature, ArgRequiredElseHelp:

https://github.com/clap-rs/clap/blob/e8d46f403671ebdd14f4f530f82f6ba6fe9c78cc/src/parse/validator.rs#L60-L68

So there's consistency in how it responds with these features.

@kinnison
Copy link
Contributor

Yes, but it would, by default, report these on stderr and exit with failure: https://github.com/clap-rs/clap/blob/v2-master/src/errors.rs#L385

@leighmcculloch
Copy link
Contributor Author

leighmcculloch commented Jul 17, 2020

Interesting, that might be a separate issue though. I don't know.

It's worth noting that ErrKind::MissingArgumentOrSubcommand is not the what is returned if the subcommand is missing and AppSettings::SubcommandRequiredElseHelp is not used. In that case ErrorKind::MissingSubcommand is returned.

It does seem quite confusing.

@kinnison
Copy link
Contributor

You've found a difference in behaviour between 1.21 and 1.22 (the presence of error:) and possibly a bug in how clap-v2 handles this situation. I'm still not convinced this is the right "fix" but I'm not going to reject it yet. Would you care to open a bug on clap discussing this?

@leighmcculloch
Copy link
Contributor Author

@kinnison See clap-rs/clap#2021.

@kinnison
Copy link
Contributor

Fantastic, let's wait and see what the clap people say about the behaviour.

@leighmcculloch
Copy link
Contributor Author

@kinnison According to clap-rs/clap#2021 (comment) this would be a breaking change if changed and so if it is changed that will be limited to v3. rustup is using v2.

@leighmcculloch
Copy link
Contributor Author

Looks like I have some tests to fix, I'll get on that.

@kinnison
Copy link
Contributor

Cool, making sure that over all our behaviour is clean is the goal. You may also want to rebase as I merged another test improvement from Robert earlier today.

@kinnison
Copy link
Contributor

I prefer rebases over merges, it makes for a clearer commit sequence to review. However I can wait for now and we can clean this up later. Are you sure that the changes in behaviour that your test changes imply are desired? Are older rustup behaviours now being changed? I ask because if we've broken how rustup used to behave then we need to be careful if anything might be used in scripting or similar.

@leighmcculloch
Copy link
Contributor Author

I prefer rebases over merges, it makes for a clearer commit sequence to review.

I force pushed a squash of the commits rebased on master to make the history clean.

Are you sure that the changes in behaviour that your test changes imply are desired? Are older rustup behaviours now being changed? I ask because if we've broken how rustup used to behave then we need to be careful if anything might be used in scripting or similar.

We could keep the error code return without the error: text, but that seems inconsistent.

What do you recommend?

@kinnison
Copy link
Contributor

Sorry it took me so long to get back here. Thank you for the squash, that makes it easier to read the commit. As for behaviour, I personally would prefer that whatever the behaviour was in versions before we started this refactor (so 1.21 or earlier) were preserved for anything where scripting might be used to detect errors. If it's purely in interactive behaviours then it's probably fine to change behaviour, though if the help text goes to stderr then it is opportunistic help and should have an exit code which reflects that (i.e. non-zero in some form).

I don't mind losing the error: text though.

@kinnison kinnison added this to the 1.23.0 milestone Jul 31, 2020
tests/cli-misc.rs Outdated Show resolved Hide resolved
tests/cli-misc.rs Outdated Show resolved Hide resolved
tests/cli-misc.rs Outdated Show resolved Hide resolved
tests/cli-misc.rs Outdated Show resolved Hide resolved
@leighmcculloch
Copy link
Contributor Author

@kinnison @rbtcollins The PR is updated to maintain the existing exit code when executed without subcommands, while losing the error: prefix on output.

@rbtcollins
Copy link
Contributor

Could you please rebase this to reflect the final changes being done, rather than the review-edit-cycle? Thanks!

@leighmcculloch
Copy link
Contributor Author

@rbtcollins The PR is updated with a force-push that squashes the commits into a single commit. The diff and the change are identical.

@rbtcollins rbtcollins merged commit 2d01d3f into rust-lang:master Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rustup CLI displays "error" message with no text on run
3 participants