From e87a150f41a05918a5ddfac362b0087aade149ac Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 27 Jun 2021 14:31:50 +0200 Subject: [PATCH] Make sure schema issues are always reported correctly, but not in init when Composer is not required, fixes #9986 --- src/Composer/Console/Application.php | 6 +++--- src/Composer/Factory.php | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 177f7220a2ac..77f1ba85cad0 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -420,9 +420,9 @@ public function getComposer($required = true, $disablePlugins = null) exit(1); } } catch (JsonValidationException $e) { - $errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors()); - $message = $e->getMessage() . ':' . PHP_EOL . $errors; - throw new JsonValidationException($message); + if ($required) { + throw $e; + } } } diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 6c9891d34130..f4fe0627413c 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -37,6 +37,7 @@ use Composer\Autoload\AutoloadGenerator; use Composer\Package\Version\VersionParser; use Composer\Downloader\TransportException; +use Composer\Json\JsonValidationException; use Seld\JsonLint\JsonParser; /** @@ -308,7 +309,13 @@ public function createComposer(IOInterface $io, $localConfig = null, $disablePlu throw new \InvalidArgumentException($message.PHP_EOL.$instructions); } - $file->validateSchema(JsonFile::LAX_SCHEMA); + try { + $file->validateSchema(JsonFile::LAX_SCHEMA); + } catch (JsonValidationException $e) { + $errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors()); + $message = $e->getMessage() . ':' . PHP_EOL . $errors; + throw new JsonValidationException($message); + } $jsonParser = new JsonParser; try { $jsonParser->parse(file_get_contents($localConfig), JsonParser::DETECT_KEY_CONFLICTS);