From 562b0899eb162c9e8e36cb7619bf0dd71753e8c5 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Mon, 2 Nov 2026 15:25:19 +0100 Subject: [PATCH] #390 - cleverage:process:execute: replace invalid UTF-8 in the JSON output, report an output that cannot be encoded instead of failing Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 3 +++ UPGRADE.md | 2 ++ docs/01-quick_start.md | 4 ++-- src/Command/ExecuteProcessCommand.php | 17 ++++++++++++++--- tests/Command/ExecuteProcessCommandTest.php | 20 ++++++++++++++++++++ 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fd83fe9..146d24ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Latest ------ +## Fixes +* [#390](https://github.com/cleverage/process-bundle/issues/390) `cleverage:process:execute` with `--output-format=json-stream` and STDOUT (#353): the invalid UTF-8 sequences of the output are replaced, and an output that still cannot be encoded as JSON (e.g. `NAN`) is reported on the error output instead of throwing a `JsonException` that made the command fail after the execution and skipped the next processes. `UPGRADE.md` (v5.5): the `dump` format without `symfony/var-dumper` and the unknown formats make the command fail before any execution. Update documentation, add tests. + v5.6 ----- diff --git a/UPGRADE.md b/UPGRADE.md index 0ddbb8bd..2591dd92 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -71,6 +71,8 @@ process because of an error, let the error fail it (error strategy `stop`). - With `--output-format`, the output is printed to STDOUT whatever the verbosity (it required `-vv`): a script reading STDOUT now receives it. - A non-array output that cannot be stored as JSON stream is reported on the error output. +- `--output-format=dump` without `symfony/var-dumper`, or an unknown format, makes the command fail before any process + is executed: it was ignored for STDOUT without `-vv`. Install `symfony/var-dumper` or fix the option. ### Tasks and transformers diff --git a/docs/01-quick_start.md b/docs/01-quick_start.md index f3775a7b..bb5ab30c 100644 --- a/docs/01-quick_start.md +++ b/docs/01-quick_start.md @@ -78,7 +78,7 @@ Some features rely on optional packages, suggested by the bundle: | `ext-intl` (PHP extension) | the [SlugifyTransformer](reference/transformers/slugify_transformer.md) | | `symfony/process` | the [CommandRunnerTask](reference/tasks/command_runner_task.md) and the [ProcessLauncherTask](reference/tasks/process_launcher_task.md) | | `symfony/stopwatch` | the [StopwatchTask](reference/tasks/stopwatch_task.md) | -| `symfony/var-dumper` | the dumps of the [DebugTask](reference/tasks/debug_task.md), of the `debug` transformer and of `cleverage:process:execute --output-format=dump` (nothing is dumped without it) | +| `symfony/var-dumper` | the dumps of the [DebugTask](reference/tasks/debug_task.md), of the `debug` transformer and of `cleverage:process:execute --output-format=dump` (required by this format: the command fails without it) | | `symfony/monolog-bundle` | the `cleverage_process`, `cleverage_process_task` and `cleverage_process_transformer` log channels, with the process and task data in the `extra` of the records (see [logging](03-custom_tasks.md#logging)); without it, the default Symfony logger is used | Without `symfony/process`, `symfony/stopwatch` or `ext-intl`, the container still compiles, and a process using one of @@ -267,7 +267,7 @@ Process 'project_prefix.process_name' executed successfully | `--input-from-stdin` | | Read the input value from STDIN instead (e.g. `cat file.json \| bin/console cleverage:process:execute ...`) | | `--context=:` | `-c` | Contextual value, can be repeated. The key must only contain word characters (`\w+`: letters, digits, `_`). The value is parsed as YAML, so `-c limit:10` gives an integer and `-c name:"'foo'"` forces a string (also needed for dates: `-c date:2024-01-01` gives an integer timestamp) | | `--output=` | `-o` | Where to dump the value returned by the task defined by the process `end_point`: `-` (default) for STDOUT, or a file path | -| `--output-format=` | `-t` | Format of the dumped output: `dump` (Symfony VarDumper, STDOUT only) or `json-stream` (written to STDOUT, or into the `--output` file if the value is an array: otherwise a warning is written on the error output). Nothing is dumped when this option is omitted. Without `end_point`, the output is `null`. The format and the process codes are checked before any process is executed | +| `--output-format=` | `-t` | Format of the dumped output: `dump` (Symfony VarDumper, STDOUT only) or `json-stream` (written to STDOUT, invalid UTF-8 sequences replaced, or into the `--output` file if the value is an array: otherwise, or when the value cannot be encoded as JSON for STDOUT, a warning is written on the error output). Nothing is dumped when this option is omitted. Without `end_point`, the output is `null`. The format and the process codes are checked before any process is executed | Example: diff --git a/src/Command/ExecuteProcessCommand.php b/src/Command/ExecuteProcessCommand.php index 8b56aba1..911ccac5 100644 --- a/src/Command/ExecuteProcessCommand.php +++ b/src/Command/ExecuteProcessCommand.php @@ -171,7 +171,14 @@ protected function handleOutputData(mixed $data, InputInterface $input, OutputIn if (self::OUTPUT_FORMAT_DUMP === $input->getOption('output-format')) { VarDumper::dump($data); } else { - $output->writeln(json_encode($data, \JSON_THROW_ON_ERROR)); + // Invalid UTF-8 replaced: the output of a process reading a non UTF-8 file can still be printed + $json = json_encode($data, \JSON_INVALID_UTF8_SUBSTITUTE); + if (false === $json) { + // Reported, as before 5.5 without -vv: the next processes are executed + $this->getErrorOutput($output)->writeln(\sprintf('Output not printed: it cannot be encoded as JSON (%s)', json_last_error_msg())); + } else { + $output->writeln($json); + } } return; @@ -179,8 +186,7 @@ protected function handleOutputData(mixed $data, InputInterface $input, OutputIn // JsonStreamFile::writeLine() only takes an array if (!\is_array($data)) { - $errorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; - $errorOutput->writeln(\sprintf("Output not stored in '%s': the JSON stream format requires an array, %s given", $input->getOption('output'), get_debug_type($data))); + $this->getErrorOutput($output)->writeln(\sprintf("Output not stored in '%s': the JSON stream format requires an array, %s given", $input->getOption('output'), get_debug_type($data))); return; } @@ -190,4 +196,9 @@ protected function handleOutputData(mixed $data, InputInterface $input, OutputIn $output->writeln(\sprintf("Output stored in '%s'", $input->getOption('output'))); } } + + private function getErrorOutput(OutputInterface $output): OutputInterface + { + return $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; + } } diff --git a/tests/Command/ExecuteProcessCommandTest.php b/tests/Command/ExecuteProcessCommandTest.php index 13d1ace5..0b44c0fc 100644 --- a/tests/Command/ExecuteProcessCommandTest.php +++ b/tests/Command/ExecuteProcessCommandTest.php @@ -87,6 +87,26 @@ public function testTheJsonOutputIsPrintedWithoutVerbosity(): void self::assertStringContainsString('{"foo":"bar"}', $tester->getDisplay()); } + public function testInvalidUtf8IsReplacedInTheJsonOutput(): void + { + $tester = $this->createTester($this->once(), ['name' => "Caf\xe9"]); + + self::assertSame(0, $tester->execute(['processCodes' => ['test.process'], '--output-format' => 'json-stream'])); + self::assertStringContainsString('{"name":"Caf\ufffd"}', $tester->getDisplay()); + } + + public function testAnOutputThatCannotBeEncodedAsJsonIsReportedAndTheNextProcessesAreExecuted(): void + { + $tester = $this->createTester($this->exactly(2), ['value' => \NAN]); + + self::assertSame(0, $tester->execute( + ['processCodes' => ['test.process', 'test.process'], '--output-format' => 'json-stream'], + ['capture_stderr_separately' => true], + )); + self::assertSame(2, substr_count($tester->getErrorOutput(), 'Output not printed: it cannot be encoded as JSON (Inf and NaN cannot be JSON encoded)')); + self::assertSame(2, substr_count($tester->getDisplay(), "Process 'test.process' executed successfully")); + } + public function testAnArrayOutputIsStoredInTheFile(): void { $tester = $this->createTester($this->once(), ['foo' => 'bar']);