From e53445efb4376b121e15bbaaad6da90ea742e3cd Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 6 Feb 2023 21:32:03 +0000
Subject: [PATCH] Issue #3283035 by bruno.bicudo, Spokje, _pratik_, longwave,
 joachim, xjm, smustgrave: Exceptions from errors in services.yml files should
 tell you which file

---
 .../Drupal/Core/DependencyInjection/YamlFileLoader.php | 10 +++++++++-
 .../Core/DependencyInjection/YamlFileLoaderTest.php    |  6 ++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php b/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php
index 159f643166b9..879966fda029 100644
--- a/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php
+++ b/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php
@@ -6,6 +6,7 @@
 namespace Drupal\Core\DependencyInjection;
 
 use Drupal\Component\FileCache\FileCacheFactory;
+use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
 use Drupal\Core\Serialization\Yaml;
 use Symfony\Component\DependencyInjection\Alias;
 use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -414,7 +415,14 @@ protected function loadFile($file)
             throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
         }
 
-        return $this->validate(Yaml::decode(file_get_contents($file)), $file);
+        try {
+          $valid_file = $this->validate(Yaml::decode(file_get_contents($file)), $file);
+        }
+        catch (InvalidDataTypeException $e) {
+          throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML: ', $file) . $e->getMessage());
+        }
+
+        return $valid_file;
     }
 
     /**
diff --git a/core/tests/Drupal/Tests/Core/DependencyInjection/YamlFileLoaderTest.php b/core/tests/Drupal/Tests/Core/DependencyInjection/YamlFileLoaderTest.php
index 7354d304c3d9..54d996bd07b6 100644
--- a/core/tests/Drupal/Tests/Core/DependencyInjection/YamlFileLoaderTest.php
+++ b/core/tests/Drupal/Tests/Core/DependencyInjection/YamlFileLoaderTest.php
@@ -169,6 +169,12 @@ public function providerTestExceptions() {
 YAML,
         'A service definition must be an array or a string starting with "@" but string found for service "service" in vfs://drupal/modules/example/example.yml. Check your YAML syntax.',
       ],
+      'YAML must be valid' => [<<<YAML
+   do not:
+      do this for the love of Foo Bar!
+YAML,
+        'The file "vfs://drupal/modules/example/example.yml" does not contain valid YAML',
+      ],
     ];
   }
 
-- 
GitLab