Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Symfony stops with: A YAML file cannot contain tabs as indentation

ErrorA YAML file cannot contain tabs as indentation.

Short answer

The YAML parser found a tab as the first character of a line. YAML indentation must be spaces only. The exception names the line number: open that line, replace the tab with spaces, and set your editor to insert spaces for the Tab key in .yaml files.

  • Applies to Symfony 7, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The exception includes the line number and the offending line. Print tabs in that file, showing the line numbers, to find every one.

    grep -nP '^\t' config/routes.yaml config/services.yaml
  2. 2Match what you see, then jump to its fix

    1. 1grep lists lines that start with a tabThe editor inserted a tab when you pressed TabFix
    2. 2The file has no tabs on disk but the error appears on a string you parse at run timeYAML is generated or pasted from code, a template or a documentFix

What does the error look like?

  • A YAML file cannot contain tabs as indentation.
  • Symfony\Component\Yaml\Exception\ParseException

Cause 1 of 2

The editor inserted a tab when you pressed Tab

1Check

The Symfony parser checks whether a line begins with a tab character and throws this exception. Symfony's YAML documentation says indentation must be done with spaces, never with tabs.

grep -rnP '^\t' config/ translations/

2Fix

Replace each leading tab with spaces (two or four, the same as the other lines) and keep one width in the file. Set the editor to insert spaces for .yaml files.

# convert leading tabs to 4 spaces in one file (GNU sed)
sed -i 's/^\t/    /' config/routes.yaml
# .editorconfig
[*.{yaml,yml}]
indent_style = space
indent_size = 4

3Verify

Clear the cache and load a page again.

php bin/console cache:clear

Confirmed by symfony.com

Did this fix it?

Cause 2 of 2

YAML is generated or pasted from code, a template or a document

1Check

Text copied from a web page, a document, or a PHP string built with \t keeps its tabs. The parser sees the tab at the start of a line in the string, not in a file.

var_dump(str_contains($yamlString, "\t"));

2Fix

Build the string with spaces, or convert leading tabs before parsing.

$yamlString = preg_replace('/^\t+/m', '    ', $yamlString);
$data = Symfony\Component\Yaml\Yaml::parse($yamlString);

3Verify

Parse again. It returns an array.

var_dump(is_array($data));

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Add an .editorconfig that sets indent_style to space for yaml and yml files.
  • Add a grep for leading tabs in YAML files to CI, so a tab fails the build and not the deploy.
  • Never indent YAML in a word processor or a chat window and paste it back.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.