Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony routing-file validation #30

Closed
bobvandevijver opened this issue Dec 8, 2016 · 7 comments
Closed

Symfony routing-file validation #30

bobvandevijver opened this issue Dec 8, 2016 · 7 comments

Comments

@bobvandevijver
Copy link

Hi,

First of all, great project!

I want to use this project to lint Symfony routing files. Below is an example of such a file:

# app/config/routing.yml
blog_list:
    path:      /blog
    defaults:  { _controller: AppBundle:Blog:list }

blog_show:
    path:      /blog/{slug}
    defaults:  { _controller: AppBundle:Blog:show }

However, apparently the : in the controller name is not considered as valid syntax. The output below is from the linter with default configuration on the snippet above:

test.yml
  2:1       warning  missing document start "---"  (document-start)
  3:15      error    too many spaces after colon  (colons)
  4:15      error    too many spaces after colon  (colons)
  4:17      error    too many spaces inside braces  (braces)
  4:40      error    syntax error: found unexpected ':'

A solution for the linter would be to place the value in "'s, but that breaks stuff in other parts.

Do you have any suggestion on how to fix this problem? I would really like to use this linter, but as long as it generates the syntax errors it is useless for my project...

@adrienverge
Copy link
Owner

Hi @bobvandevijver,

Thanks, I wasn't aware of that limitation.

The problem comes from pyyaml, the Python YAML library (on which yamllint relies). It can be reproduced using:

$ python
>>> import yaml
>>> yaml.load('{ key: va:lue }')

yaml.scanner.ScannerError: while scanning a plain scalar

found unexpected ':'
  in "<string>", line 1, column 10:
    { key: va:lue }

I've looked into the official spec, to check that your YAML code is valid. It is. The spec states that plain scalars cannot contain ": ", but can contain ":":

Plain scalars must never contain the “: ” and “ #” character combinations.

Normally, YAML insists the “:” mapping value indicator be separated from the value by white space. A benefit of this restriction is that the “:” character can be used inside plain scalars, as long as it is not followed by white space. This allows for unquoted URLs and timestamps. It is also a potential source for confusion as “a:1” is a plain scalar and not a key: value pair.

So I suggest you report this bug at pyyaml, so this gets fixed upstream. Feel free to link this issue for reference.

In the meantime, you could use this equivalent notation (that does not break pyyaml):

blog_list:
    path: /blog
    defaults:  
        _controller: AppBundle:Blog:list

@adrienverge
Copy link
Owner

By digging further I found this open pull request at pyyaml: yaml/pyyaml#45

Hopefully it will get merged and your problem will disappear!

@freddrake
Copy link

If you want to keep the single-line format, you can quote the controller name:

# app/config/routing.yml
blog_list:
    path:      /blog
    defaults:  { _controller: "AppBundle:Blog:list" }

blog_show:
    path:      /blog/{slug}
    defaults:  { _controller: "AppBundle:Blog:show" }

@bobvandevijver
Copy link
Author

@adrienverge Thank you for the information, lets hope that it will be merged soon 😃

@freddrake I know, but with the quotes the autocomplete in my IDE no longer works. Besides, I would have to change quite a lot of route names...

@freddrake
Copy link

@bobvandevijver: I agree getting the fix landed would be best.

@bobvandevijver
Copy link
Author

As a temporary solution I'm using the following command/regex in order to replace the "errors" in the yaml files (might anyone be interested):

find src -type f -name *.yml -exec sed -ri 's/(_controller: )([a-zA-Z]+:[a-zA-Z\/]+:[a-zA-Z]+)/\1\"\2\"/g' {} \;

@adrienverge
Copy link
Owner

yaml/pyyaml#45 has been merged and released, and the bug doesn't appear anymore on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants