Check Errors in YAML Files using Console Command in Symfony 7

Check Errors in YAML Files using Console Command in Symfony 7

Symfony framework provides static code analysis commands (also known as linters) that enables to check some parts of application such TWIG syntax, service definitions in container, etc.

This tutorial shows how to check errors in YAML files using console command in Symfony 7 application.

Let's say we have the following YAML file:

config/routes.yaml

controllers:
    resource: ../src/Controller/
    resource: ../src/Kernel.php
    type: annotation

The lint:yaml command can be used for checking if YAML files don't have any syntax errors. YAML filename can be provided as argument:

php bin/console lint:yaml config/routes.yaml

The command prints a success message if the YAML file is valid, otherwise an error is displayed:

ERROR  in config/routes.yaml 
>> Duplicate key "resource" detected at line 3 (near "resource: ../src/Kernel.php").

A whole directory can be checked as well by providing directory name as argument:

php bin/console lint:yaml config

The problem can be fixed as follows:

config/routes.yaml

controllers:
    resource: ../src/Controller/
    type: annotation

kernel:
    resource: ../src/Kernel.php
    type: annotation

If you are using a continuous integration, recommended adding lint:yaml command to the list of commands which executed on each build.

Leave a Comment

Cancel reply

Your email address will not be published.