Skip to content

Handle exiting in error state on help & give suggestions.#4266

Merged
danielbachhuber merged 11 commits intomasterfrom
suggest_help
Aug 10, 2017
Merged

Handle exiting in error state on help & give suggestions.#4266
danielbachhuber merged 11 commits intomasterfrom
suggest_help

Conversation

@gitlost
Copy link
Contributor

@gitlost gitlost commented Aug 3, 2017

Issue #4265

Adds handling of help command errors on exiting, giving suggestions.

Removes the handling in the help command itself, as it's not best placed to deal with it. Also renames its find_subcommand() method to find_command_to_help() as it's very confusing when it's not a CompositeCommand.

Disables for now the auto_check_update(), as it presents the user with an odd experience when triggered. It's hard to know where to put it.

) ) {
$this->auto_check_update();
$this->_run_command();
// $this->auto_check_update(); // Disable for now? If it hits the cache threshold and determines to update it exits with no recourse for the user.
Copy link
Member

Choose a reason for hiding this comment

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

Let's keep $this->auto_check_update() in please, until we discuss further. This is WP-CLI's auto-update mechanism. It's non-trivial to disable it, particularly given we're so close to a release date.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok.

}

private function _run_command() {
private function _run_command( $exiting = true ) {
Copy link
Member

Choose a reason for hiding this comment

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

This seems like ambiguous behavior. Is there a better way to implement it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll remove the arg and rename the method _run_command_and_exit() and have the case after the 1st help attempt just use run_command( args...).

@danielbachhuber
Copy link
Member

Generally, this pull request probably needs some discussion before further development. I'd be hesitant to land something like this without detailed conversation because it's a critical execution path and only has good, but not great, test coverage.

if ( is_string( $possible_suggestion ) ) {
WP_CLI::error( $possible_suggestion );
}
// Should never happen.
Copy link
Member

Choose a reason for hiding this comment

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

How can we get to 100% confidence?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well if it does find the command then it doesn't return...if it doesn't then it does...then find_command_to_run() runs the same loop as find_command_to_help() so it will fail too and return a suggestion...100%!

Copy link
Member

Choose a reason for hiding this comment

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

Ok, then we can remove the nonsensical error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed amazingly amusing error message and now Help_Command uses Runner::find_command_to_run() so should be 100% confident.

if ( function_exists( 'add_filter' ) ) {
$command_string = implode( ' ', $args );
\WP_CLI::error( sprintf( "'%s' is not a registered wp command.", $command_string ) );
}
Copy link
Member

Choose a reason for hiding this comment

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

Why is this removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it's a poor relation of what find_command_to_run() is doing...

Copy link
Member

Choose a reason for hiding this comment

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

Can you clarify in greater detail? I'm hesitant to remove this without a clear articulation of what this was doing before and why removing it won't cause unintended consequences.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed it now to use Runner::find_command_to_help() to address #4266 (comment) fully, and removed find_command_to_help() (previously Help_Command::find_subcommand()) altogether - I'll expand below as the review commenting is getting a bit messy.

@danielbachhuber
Copy link
Member

@gitlost To help me better understand the nature of these changes, can you use Licecap to provide some GIFs of the behavior before and after?

@gitlost
Copy link
Contributor Author

gitlost commented Aug 3, 2017

Doesn't seem to be a Linux version of that - will come up with something...

@danielbachhuber danielbachhuber self-requested a review August 9, 2017 02:21
And STDERR should be:
"""
Error: 'non-existent-command' is not a registered wp command.
Error: 'non-existent-command' is not a registered wp command. See 'wp help'.
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about changing:

See 'wp help'.

To:

See 'wp help' for available commands.

I think it adds more clarity to the message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, changed.

"""
Warning: This does not seem to be a WordPress install.
If the command 'non-existent-command' is in a plugin or theme, pass --path=`path/to/wordpress`.
Error: 'non-existent-command' is not a registered wp command. See 'wp help'.
Copy link
Member

Choose a reason for hiding this comment

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

If we definitely know this isn't a WordPress install, we don't need to include If the command 'non-existent-command' is in a plugin or theme, pass --path=path/to/wordpress

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed it to one line "No WordPress install found. If the command...".

The idea behind mentioning about plugin/theme is to address your comment #4265 (comment) where the command could be added in a plugin/theme.

"""
And STDOUT should be empty

Scenario: Suggestions for command typos in help
Copy link
Member

Choose a reason for hiding this comment

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

Does this scenario need Given an empty directory ?

"""
And STDOUT should be empty

Scenario: Suggestions for subcommand typos in help of specially treated commands
Copy link
Member

Choose a reason for hiding this comment

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

This seems to be missing a Given too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

if ( is_string( $possible_suggestion ) ) {
WP_CLI::error( $possible_suggestion );
}
// Should never happen.
Copy link
Member

Choose a reason for hiding this comment

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

Ok, then we can remove the nonsensical error message.

// In case the operation fails, ensure the timestamp has been updated.
$cache->write( $cache_key, time() );

// Ask before doing remote request.
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this, please, and open an issue to discuss changes to the update process before making them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done and will do.

* @param string $question Question to display before the prompt.
* @param array $assoc_args Skips prompt if 'yes' is provided.
*/
public static function ask( $question, $assoc_args = array() ) {
Copy link
Member

Choose a reason for hiding this comment

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

This can be removed for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

if ( function_exists( 'add_filter' ) ) {
$command_string = implode( ' ', $args );
\WP_CLI::error( sprintf( "'%s' is not a registered wp command.", $command_string ) );
}
Copy link
Member

Choose a reason for hiding this comment

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

Can you clarify in greater detail? I'm hesitant to remove this without a clear articulation of what this was doing before and why removing it won't cause unintended consequences.

gitlost and others added 4 commits August 9, 2017 16:11
```
Loading composer repositories with package information
Updating dependencies
Package operations: 0 installs, 1 update, 0 removals
  - Updating composer/composer (1.4.3 => 1.5.0):  Checking out 6f3310c762
Writing lock file
Generating autoload files
```
@gitlost
Copy link
Contributor Author

gitlost commented Aug 9, 2017

Here's a before/after screenshot of running this bash script https://gist.github.com/gitlost/e895eb50068aff4ca36a61de3f74a2e7 in the suggest_help branch:

screenshot from 2017-08-09 15-52-38

Re removing the code from Help_Command, the idea is to consolidate the exiting and error messages in Runner. At the moment help will exit if it determines that WP is loaded but will just return otherwise. The error it gives on exiting is different than that that occurs when other commands don't execute (and which don't do this WP loaded check). It's confusing and inconsistent, and leads to odd behaviour at the moment, so I think it's better if help just tries to help and exit, and otherwise let Runner look after things.

@danielbachhuber
Copy link
Member

Thanks @gitlost. We can land this and see if anything shakes out of the behavior changes.

@danielbachhuber danielbachhuber merged commit c13b881 into master Aug 10, 2017
@danielbachhuber danielbachhuber deleted the suggest_help branch August 10, 2017 16:52
@gitlost
Copy link
Contributor Author

gitlost commented Aug 10, 2017

Thanks @danielbachhuber, actually was trying to come up with a proper less hand-waving explanation when I noticed that there's an issue with help if the WP install is partial, as tested for in "Help when WordPress is downloaded but not installed" in features/help.feature:49, and reproducible by:

wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli-nightly.phar -nc -q && mv wp-cli-nightly.phar wp-nightly.phar && chmod +x wp-nightly.phar
rm -rf /tmp/pull_4266_wp_install
mkdir /tmp/pull_4266_wp_install
mysql --user=wp_cli_test --password=password1 --execute='DROP DATABASE IF EXISTS wp_cli_test;'
./wp-nightly.phar core download --path=/tmp/pull_4266_wp_install
./wp-nightly.phar config create --dbname=wp_cli_test --dbuser=wp_cli_test --dbpass=password1 --path=/tmp/pull_4266_wp_install
./wp-nightly.phar help core is-installed --path=/tmp/pull_4266_wp_install
./wp-nightly.phar help --path=/tmp/pull_4266_wp_install

where the help commands give Error: Can’t select database. errors (but won't on help core install for instance).

Which probably explains why help config and help core install/multisite-install/verify-checksum/version and help db are treated specially.

I'll push a fix to a new PR which involves adding a special help_wp_die_handler to cater for this, though it's a bit hacky.

danielbachhuber added a commit that referenced this pull request Aug 10, 2017
Add help_wp_die_handler. Remove special help treatment for config and db (#4266).
danielbachhuber added a commit that referenced this pull request Aug 15, 2017
This reverts commit c13b881, reversing
changes made to 3608f68.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants