A YAML file cannot contain tabs as indentation

57,487

Solution 1

A YAML file use spaces as indentation, you can use 2 or 4 spaces for indentation, but no tab. In other words, tab indentation is forbidden:

Why does YAML forbid tabs?

Tabs have been outlawed since they are treated differently by different editors and tools. And since indentation is so critical to proper interpretation of YAML, this issue is just too tricky to even attempt.

(source: YAML FAQ (thanks to Destiny Architect for the link))

For example, the Symfony configuration file can be written with 2 or 4 spaces as indentation:

4 spaces

doctrine:
    dbal:
        default_connection: default

2 spaces

doctrine:
  dbal:
    default_connection: default

Solution 2

IF you are using EditorConfig make sure to add this to your .editorconfig file

[*.yml]
indent_style = space
indent_size = 4

you can change indent_size to 2, depends on your preferences

Solution 3

Can you try cache:clear or try using path instead of pattern.

The path option is new in Symfony2.2, pattern is used in older versions.

community_online_shop_homepage:
    path: /
    defaults: { _controller: CommunityOnlineShopBundle:Page:index }
_login:
    path: /login
    defaults: { _controller: CommunityOnlineShopBundle:Page:login }
Share:
57,487
odbhut.shei.chhele
Author by

odbhut.shei.chhele

আমাদের এই বাংলাদেশে ছিল তার বাড়ি কাউকে কিছু না বলে অভিমানে দূর দেশে দিল পারি

Updated on July 08, 2022

Comments

  • odbhut.shei.chhele
    odbhut.shei.chhele almost 2 years

    This is my first work with Symfony 2. All I am trying to do here is whenever the user clicks on the submit button he will go to another page.

    But my index page isn't loading. They are saying there is something wrong with my routing file, specifically:

    A YAML file cannot contain tabs as indentation

    I don't know what I have done wrong. Here is my routing file.

    community_online_shop_homepage:
        pattern: /
        defaults: { _controller: CommunityOnlineShopBundle:Page:index }
    _login:
        pattern: /login
        defaults: { _controller: CommunityOnlineShopBundle:Page:login}
    
  • Destiny Architect
    Destiny Architect over 9 years
    yaml.org/faq.html gives apparent official answer to “Why does YAML forbid tabs?”, and I tend to concur, indeed, inspired by this, have then been doing the same in the other programming languages I code.
  • A.L
    A.L over 9 years
    @Destiny Architect thanks, I'll add it to the answer.
  • Oscar
    Oscar over 7 years
    So how does a parser know if the file is using two or four spaces per indentation level? What if a file mixes the two? How will the parser validate the file? And what happens if it encounters three spaces?
  • A.L
    A.L over 7 years
    @Oscar I don't know. Did you try to mix 2 and 4 spaces? 3 spaces should trigger an error.
  • Oscar
    Oscar over 7 years
    No. I haven't used a YAML parser yet. I was wondering if anyone else knew, because the insistence on spaces seems to increase ambiguity rather than decrease it.
  • A.L
    A.L over 7 years
    @Oscar: about the 2 or 4 spaces, if think that it's simple : the spaces in front of dbal indicates that this is an array. Since doctrine was the root, dbal can only be the first level. So YAML can count the spaces before the first level and know if there is 2 or 4 spaces on each line.
  • Oscar
    Oscar over 7 years
    What happens if the first indentation is two spaces, and the second one is four spaces? Does the parser flag an error because a level appears to be missing?
  • Kulvar
    Kulvar about 5 years
    @DestinyArchitect It's a fallacious argument. Bad code will always look bad. Tabs are not the cause of bad identation. Tabs look being customizable and using less bytes makes them better than spaces. And every argument for spaces can be debunked. Present me any code and I'll give you a nicely formatted one using only tabs.
  • Youda008
    Youda008 about 5 years
    Using tabs is a headache only for people who can't use them properly.
  • yota
    yota almost 5 years
    this is the biggest mistake of guido... he should have outlawed the space indentation ;)
  • Andreas Linnert
    Andreas Linnert almost 5 years
    I wonder: since "indent_size" is a user preference, should it be included in the file at all?
  • Alireza
    Alireza over 4 years
    No, and this is an argument for tab indentation: using tabs, each member of the team can see the indentation according to their preference, while using tabs, everyone working on the same code base should be consistent
  • Victor Yarema
    Victor Yarema over 4 years
    Time to add examples with all flavors of space fans indentations: 1, 3, 6, 8, <whatever>. Some people just can't understand simplest abstract things like tabs.
  • Neonit
    Neonit over 4 years
    The answer is correct, so I upvoted it, but the argument is very bad. Problems do only occur, if tab and space indentation is mixed in one file. This should be forbidden. Otherwise it is perfectly and easily possible to treat all tabs in a tabs-only indented file as one would treat single-space-indentation. The actual reason must be incompetence or personal preference. The latter is not a problem at all, but not saying it is just pathetic. I'm disappointed, especially because I think YAML is otherwise brilliant.
  • Jasen
    Jasen almost 4 years
    instead of encoding the file format in metadata use a vim modeline in a comment near the top of the file
  • MestreLion
    MestreLion over 2 years
    @amcgregor: great points! For the sake of argument, then let's just state the "lowest common denominator" between Python and YAML: both have sintatically meaningful whitespace, even if in Python that's just for indentation. It sets both apart from, say, C and Java.
  • ekerner
    ekerner over 2 years
    Guido was green at the time. Tabs are not only more manageable as indentation, but require fewer keystrokes to code and navigate, and reduce the file size/payload.