How to fix YAML syntax error: did not find the expected β€œ-” indicator while parsing a block?

I have code written in my .travis.yml written for the Python library. Using lint.travis-ci.org , I found out that there is an indentation problem in my YAML file. Here is the part the error indicates

 install: - if [[ "${TEST_PY3}" == "false" ]]; then pip install Cython; python setup.py build; # To build networkx-metis mkdir core; # For the installation of networkx core cd core; git clone https://github.com/orkohunter/networkx.git; cd networkx/; git checkout addons; python setup.py install; cd ..; fi 

Where am I mistaken? Error says

 syntax error: (<unknown>): did not find expected '-' indicator while parsing a block collection at line 32 column 3 

It would be great if there was a tool like autopep8 for fixing indentation of YAML files.

+7
indentation yaml syntax-error travis-ci
source share
2 answers

You do not have 32 lines in your file (perhaps because you deleted the non-essential data from the example), but the indent level points to the line with fi .

In fact, the problem starts earlier, and what you want to do is specify the action that will be executed as a multi-line string. You can specify them in YAML in several ways, but the purest is to use a literal scalar indicator | "which stores newline characters:

 install: - | if [[ "${TEST_PY3}" == "false" ]]; then pip install Cython; python setup.py build; # To build networkx-metis mkdir core; # For the installation of networkx core cd core; git clone https://github.com/orkohunter/networkx.git; cd networkx/; git checkout addons; python setup.py install; cd ..; fi 

For these errors, there is no automatic YAML resubmission tool.

Reindenters for Python accepts working code and indentes permanently (replacing TAB, always the same indentation per level). Repeatedly subtracting code from code to code with syntax errors either does not work, or may lead to incorrect results.

The reenders for YAML face the same problem: what to do if the input does not make sense (and what is clear to you and to me is not always clear to the program). Just doing everything that is not parsed in a multi-line scalar is not a general solution.

In addition, most YAML analysts throw out some information about reading in files that you won’t want to get lost when re-indenting, including EOL comments, manually created anchor names, key bindings, etc. All without violating the requirements in the specification.

If you want to consistently depart from your (correct) YAML, you can use the yaml utility, which is part of the package [ruamel.yaml][2] (disclaimer: I am the author of this package). Your original input used with yaml round-trip .travis.yml will give:

  ... in "<byte string>", line 3, column 3: - if [[ "${TEST_PY3}" == "false" ... ^ expected <block end>, but found '<scalar>' in "<byte string>", line 6, column 7: mkdir core; # For the installati ... 

Unfortunately, it is not much more useful to find an error, the correct version of .travis.yml passing through yaml round-trip .travis.yml will tell you that it stabilizes in the second round (i.e., it gets lost in the first extra space). And yaml round-trip .travis.yml --save gives you:

 install: - | if [[ "${TEST_PY3}" == "false" ]]; then pip install Cython; python setup.py build; # To build networkx-metis mkdir core; # For the installation of networkx core cd core; git clone https://github.com/orkohunter/networkx.git; cd networkx/; git checkout addons; python setup.py install; cd ..; fi 

Note that there is no YAML comment in this # TO build networkx-metis . This is just part of a multi-line string. However, the comment on the line before the first or after the last will remain.

+3
source share

The error means that you have a syntax error, and this is especially difficult to track, as this can mean several things, indentation, including the missing double quote, or you need to make a double quote with some special characters.

If you track your .travis.yml in the git repository using the travis command, you can easily check previous versions and compare.

For example:

 $ travis lint <(git show HEAD^:.travis.yml ) Warnings for /dev/fd/63: [x] syntax error: (<unknown>): did not find expected '-' indicator while parsing a block collection at line 61 column 3 $ travis lint <(git show HEAD~2:.travis.yml) Hooray, /dev/fd/63 looks valid :) 

Where HEAD~2 checks for 2 commits behind, so keep increasing the number until it works as soon as it is found, and then compare as:

 git diff HEAD~2 .travis.yml 

Otherwise, split it into smaller pieces or continue deleting some partitions until they work.


Using ruby is an alternative way to check for YAML syntax:

 ruby -e "require 'yaml';puts YAML.load_file('.travis.yml')" 

so you don’t need to POST your code every time with travis , which works just like Travis WebLint .


Example

The following syntax is incorrect:

 language: python before_script: - | true # Some comment. true 

as the comment has the wrong indentation according to:

[x] Syntax error :(): did not find the expected indicator β€œ-” while parsing the block collection by column 3 of row 3

Here is the valid syntax:

 language: python before_script: - | true # Some comment. true 

The aforementioned problem arises, especially when editing files in Vim, which indents comments, creating them from the very beginning.

+4
source share

All Articles