Problem/Motivation
I'm receiving Unable to decode output into JSON: Syntax error errors from drush commands that are using the batch api internally. This results in the command to return a non-0 exit code breaking automation.
Steps to reproduce
On a multilingual site,
- install twig_tweak (or any module with missing translations)
- install monolog
- set up a custom handler that logs to
php://stderrusing@monolog.formatter.json - use
drush locale:check.
After this, the above error is shown.
Note, although this is one way to reproduce, the main reason for this error is the batch process printing out multiple lines of JSON, then Drupal trying to json_decode the output of the process. Any setup that makes any batch process log at least 1 line of JSON output will encounter the same error.
Proposed resolution
Fix the issue.
Remaining tasks
Fix the issue.
User interface changes
-
API changes
-
Data model changes
-
Additional information
Stack trace of the error:
Exception trace:
at /var/www/html/vendor/consolidation/site-process/src/ProcessBase.php:171
Consolidation\SiteProcess\ProcessBase->getOutputAsJson() at /var/www/html/vendor/drush/drush/includes/batch.inc:157
_drush_backend_batch_process() at /var/www/html/vendor/drush/drush/includes/batch.inc:80
drush_backend_batch_process() at /var/www/html/vendor/drush/drush/src/Drupal/Commands/core/LocaleCommands.php:89
Drush\Drupal\Commands\core\LocaleCommands->check() at n/a:n/a
call_user_func_array() at /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php:257
Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback() at /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php:212
Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter() at /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php:176
Consolidation\AnnotatedCommand\CommandProcessor->process() at /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php:311
Consolidation\AnnotatedCommand\AnnotatedCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:255
Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:1027
Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:273
Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/symfony/console/Application.php:149
Symfony\Component\Console\Application->run() at /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php:118
Drush\Runtime\Runtime->doRun() at /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php:48
Drush\Runtime\Runtime->run() at /var/www/html/vendor/drush/drush/drush.php:72
require() at /var/www/html/vendor/drush/drush/drush:4
Issue fork monolog-3223751
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
mhavelant commentedComment #3
lussolucaHi, I'm not able to reproduce this issue.
Can you provide some more info?
Comment #4
goz commentedI have the same message, and it seems to come from PHP8+
All i do is enabling monolog, with no specific json settings.
Here is an example of setting :
On instance with PHP 7.4-fpm :
But in PHP 8.0 or 8.1 :
Comment #5
goz commentedProcessPage.php comes from consolidation/site-process library. There is similar issue on github for this project : https://github.com/consolidation/site-process/issues/56
And here is other issue about this (linked in issue 56) :
https://github.com/drush-ops/drush/issues/4281
In my case, in \Consolidation\SiteProcess\ProcessBase:getOutputAsJson(), $output is :
and $sanitizedOutput is :
We can see removeNonJsonJunk() remove the first string which has not been declared in json format, but miss the second one.
I'm not sure the issue is really from Consolidation/site-process. That's depend from point of view :
1/ Should site-process really sanitize string and return a valid json in anycase ? : site-process has to be fixed
2/ Do site-process call should be done with cleaner string ? So try to not have multiple string/json concatened ? : monolog (or other drupal module) has to be fixed
3/ 1/ and 2/
Comment #6
goz commentedMy bad, i do not reproduce using php://stderr as suggested in https://github.com/drush-ops/drush/issues/4790#issuecomment-932729614. Even specifying json formatter
Comment #7
lussolucaOK that using stderr fix the issue, but I prefer to implement a custom line formatter specifically for Drush commands.
I'll work on this.
Comment #10
goz commentedGreat, thanks lussoluca !
Comment #11
renrhafAlso having the issue; fixed using stderr instead of stdout.
Comment #12
lussoluca@renrhaf, with which version of the module?
Comment #13
renrhafHi @lussoluca, I'm using the 2.0.2 version of the module
After using the patch the problem was still here so switched to stderr and it's good
Comment #17
lussolucaPatch committed and pushed to 2.x and 3.0.x