symfony/routing · error · InvalidArgumentException

The file " " does not contain valid YAML

Error message

The file "%s" does not contain valid YAML: 

What it means

YamlFileLoader::load() wraps the YAML parse in a try/catch and rethrows an InvalidArgumentException when symfony/yaml's ParseException occurs, prefixing the file path. The original parse error (bad indentation, invalid syntax, bad encoding) is preserved as the previous exception and appended in the message.

Solutions

  1. Read the appended ParseException message for the exact line/column and fix the YAML indentation/syntax there
  2. Validate the file: php -r "(new Symfony\Component\Yaml\Parser())->parseFile('routes.yaml');" or run a YAML linter
  3. Replace tabs with spaces (YAML forbids tabs)
  4. Convert legacy short controller syntax to the current [Controller::class, 'method'] form

Example fix

// routes.yaml before (invalid: tab indentation)
index:
	 path: /
	 controller: App\Controller\HomeController::index
// after
index:
    path: /
    controller: App\Controller\HomeController::index
Defensive patterns

Strategy: try-catch

Validate before calling

try { (new \Symfony\Component\Yaml\Parser())->parseFile($path); } catch (\Symfony\Component\Yaml\Exception\ParseException $e) { echo $e->getMessage(); }

Type guard

null

Try / catch

try { $loader->load($file); } catch (\InvalidArgumentException $e) { if (str_contains($e->getMessage(), 'valid YAML')) { log($e->getPrevious()?->getMessage()); } throw $e; }

Prevention

When it happens

Trigger: A routes.yaml with YAML syntax errors: wrong indentation, tabs instead of spaces, unescaped special characters, duplicate keys in strict mode, invalid UTF-8, or malformed flow syntax like [controller: action].

Common situations: Editing routes.yaml by hand; copying examples with mixed indentation; CI parsing files produced by templating tools that inject invalid YAML; Symfony 5.3+ deprecation-triggering syntax or old short array controller syntax like [AppBundle:Blog:list] not parseable.

Related errors


AI-assisted analysis of symfony/routing@83fa223250 (2026-09-14). Data as JSON: /api/errors/e9de95c0a73a4a0a. Report an issue: GitHub.

Appendix: source

Thrown at Loader/YamlFileLoader.php:57

     */
    public function load(mixed $file, ?string $type = null): RouteCollection
    {
        $path = $this->locator->locate($file);

        if (!stream_is_local($path)) {
            throw new \InvalidArgumentException(\sprintf('This is not a local file "%s".', $path));
        }

        if (!file_exists($path)) {
            throw new \InvalidArgumentException(\sprintf('File "%s" not found.', $path));
        }

        $this->yamlParser ??= new YamlParser();

        try {
            $parsedConfig = $this->yamlParser->parseFile($path, Yaml::PARSE_CONSTANT);
        } catch (ParseException $e) {
            throw new \InvalidArgumentException(\sprintf('The file "%s" does not contain valid YAML: ', $path).$e->getMessage(), 0, $e);
        }

        $collection = new RouteCollection();
        $collection->addResource(new FileResource($path));

        // empty file
        if (null === $parsedConfig) {
            return $collection;
        }

        // not an array
        if (!\is_array($parsedConfig)) {
            throw new \InvalidArgumentException(\sprintf('The file "%s" must contain a YAML array.', $path));
        }

        $this->loadContent($collection, $parsedConfig, $path, $file);

        return $collection;

View on GitHub (pinned to 83fa223250)