Repository navigation
Done
Labels
Description
Description
When wp package install cannot decode the GitHub API response for a package's default branch, the warning prints a literal %s instead of the package name.
Where
src/Package_Command.php, in get_github_default_branch():
$package_data = json_decode( $response->body );
if ( JSON_ERROR_NONE !== json_last_error() ) {
WP_CLI::warning( "Couldn't fetch default branch for package '%s' (failed to decode JSON response). Presuming default branch is 'master'." );
return 'master';
}The string has no sprintf() call and no argument. WP_CLI::warning() does not format the message, so the %s is printed as is.
The two sibling warnings in the same method both use sprintf( ..., $package_name ). One is the non-2xx status case above this code. The other is the invalid default_branch case below it. $package_name is in scope here.
Steps to reproduce
- Make the GitHub API request for the repository return HTTP 200 with a body that is not JSON. A proxy or captive portal page can do this.
- Run
wp package install https://github.com/<owner>/<repo>.git.
Expected
Warning: Couldn't fetch default branch for package '<owner>/<repo>' (failed to decode JSON response). Presuming default branch is 'master'.
Actual
Warning: Couldn't fetch default branch for package '%s' (failed to decode JSON response). Presuming default branch is 'master'.
Suggested fix
Wrap the message in sprintf(), the same way as the sibling warnings:
WP_CLI::warning(
sprintf(
"Couldn't fetch default branch for package '%s' (failed to decode JSON response). Presuming default branch is 'master'.",
$package_name
)
);Environment
- WP-CLI 3.0.1. The bundled code has the same line as
mainat6487f3a. - PHP 8.6.0
Notes
- I reproduced this with the real
wpbinary against a local mock ofapi.github.comthat returned HTTP 200 with the bodynot json. With the suggested fix applied, the same run prints'example/some-pkg'in place of'%s'. - There is no existing test for the "Presuming default branch is 'master'" warnings. The method is private and calls
Utils\http_requestagainst a hard-codedapi.github.comURL, so a test needs an HTTP mock or proxy. I plan no new test for this one-string fix, unless you want one. - I searched open and closed issues and PRs for "%s", "sprintf" and "default branch". I found no existing report.
Request
Could you please assign this issue to me? I would like to raise the PR for the fix.
Activity