ruby / psych

A libyaml wrapper for Ruby
MIT License
566 stars 205 forks source link

wrong line reported in 'did not find expected key while parsing a block mapping' #190

Open djellemah opened 10 years ago

djellemah commented 10 years ago

When parsing this yaml

key_one:
  inner:
  - value1
  - value2

key_two:
    inner:
  - value1
  - value2

the error message

Psych::SyntaxError: (/tmp/oops.yaml): did not find expected key while parsing a block mapping at line 1 column 1

contains the wrong line number. It should be reporting line 7 column 5.

anicholson commented 10 years ago

+1, got bitten by this issue today. Very annoying when the syntax error occured nearly 1500 lines into the file!

anicholson commented 10 years ago

I guess it's winding back to the start of the structure it's trying to parse, rather than flagging the error then and there...

happycollision commented 9 years ago

Super glad I found this issue. Probably saved my an hour of hair pulling. In my case, it seems to be reporting the correct column number, but still reports line 1 incorrectly.

illepic commented 8 years ago

To anyone stumbling through the internet that finds this issue for related reasons: Somewhere in your YAML you have extra spaces (or not enough spaces) before a key value. Thank you all for helping me solve this on a completely different project!

Edit on December 10, 2019: A great YAML linter may help here: https://jsonformatter.org/yaml-formatter.

DaveCollinsJr commented 8 years ago

+1 on the empty space in the yml file. Copy/paste error.

felipecrv commented 8 years ago

@illepic Thank you so much! I was missing an extra space.

rohstar commented 8 years ago

thanks @illepic!

cleverington commented 8 years ago

Thanks @illepic!

Gimcrack commented 8 years ago

Found my syntax error. Thanks! @illepic

BatuhanW commented 8 years ago

For anyone who is looking for this stupid error, I fix error by removing " ' " character.

sanguis commented 8 years ago

so it was a bad copy for me.

In vim I did a search of ^\(\s\s\)\+ to find all instances of good spacing and was able to find my bug

dangan249 commented 8 years ago

+1

aurawindsurfing commented 8 years ago

209:11 should have beed 209:09 ... cheers for pointing that out!

ryanooi commented 8 years ago

Please check spacing. i got the same error because additional spacing.

pooja-mane commented 8 years ago

Hi I also got this error as "did not find expected key while parsing a block mapping at line 3 column 5" My code is as follow create_list: "Create a List" message: "Save Profiles" but when I place a message key in another key it works fine. plz tell me if any one knows solution... I have checked spacing also, as there is no extra spacing in line. Thank you...

elcortez commented 8 years ago

@pooja-mane Can you try something:

pooja-mane commented 8 years ago

@elcortez thank you. I got my mistake in code. create_list: "Create a List" message: "Save Profiles" as I was using massage key as create_list.message which is wrong. as when it searches for for create_list it got a label "Create a List" it wont go to search for message key. I changed my code as create_list: message: "Save Profiles"

einverne commented 8 years ago

Save me a lot of time. And here is a yamllint tool. http://www.yamllint.com/

joney000 commented 8 years ago

+1

mahemoff commented 8 years ago

Here's command-line yamllint. It gives the correct line number.

https://github.com/adrienverge/yamllint

ORESoftware commented 8 years ago

(WHITESPACE AS SYNTAX IS THE DUMBEST THING EVER), thank you

felipecrv commented 7 years ago

@ORESoftware that's the whole point of using YAML. Use JSON instead.

andydempster commented 7 years ago

Here's command-line yamllint. It gives the correct line number. https://github.com/adrienverge/yamllint

Thank you @mahemoff, you just saved me a world of pain!

vfonic commented 7 years ago

Ended up here after missing a closing ' sign.

Let's assume paths: is at line 1.

The error message was: did not find expected key while parsing a block mapping at line 5 column 3 (Psych::SyntaxError)

While the actual error is at line 20.

 1. paths:
 2.   # customer:
 3.   #   path: /news/entertainment-arts-27221191
 4.   # before_capture: 'javascript/interact--casper.js' # (optional) JavaScript file to execute before taking the screenshot of this path.
 5.   customers_login:                '/account/login'
 6.   customers_register:             '/account/register'
 7.   404:                            '/wraith_testing'
 8.   article_konfigurator:           '/blogs/blog/alte-brillen-neu-verglasen'
 9.   article:                        '/blogs/blog/warum-werden-brillenglaeser-entspiegelt'
10.   article_lexikon:                        '/blogs/lexikon/basis' # blog lexikon article
11.   blog_lexikon:                   '/blogs/lexikon' # blog index
12.   blog_lexikon_tag:                   '/blogs/lexikon/tagged/b' # blog lexikon tag
13.   blog:                           '/blogs/blog'
14.   cart:                           '/cart'
15.   index:                          '/'
16.   page_about_us:                  '/pages/uber-topglas'
17.   page_composer_einstarkenglaser: '/pages/einstarkenglaser'
18.   page_composer_gleitsichtglaser: '/pages/gleitsichtglaeser'
19.   page_composer_koln:             '/pages/optiker-koeln'
20.   page_consulting:                '/pages/unsere-brillenglaser-im-detail # HERE'S THE ERROR
21.   page_contact:                   '/pages/kontakt'
22.   page_faq:                       '/pages/haeufige-fragen'
23.   page_functioniert:              '/pages/so-funktionierts'
24.   page_konfigurator:              '/pages/brillenglaeser-online-auswaehlen'
25.   page:                           '/pages/lieferzeiten'
26.   search:                         '/search'
27.   search:                         '/search?type=article,page&q=sehstarke'
juanmirocks commented 7 years ago

Do not write colons within strings as in:

- printf 'commit:'`
goalaleo commented 7 years ago

For me the line number was incorrect when I had unescaped double quotes inside a value...

farfromunique commented 7 years ago

Copy/pasting code from the internet can have WEIRD results. I had to re-write an entire file to fix this issue; my spaces and quotes were right, but there were invisible whitespace characters hidden... somewhere.

emfluenceindia commented 7 years ago

This vagrant error reporting must be looked into! For me reported line number was 74, while it was 84 actually. For beginners like me its a nightmare to understand what's went wrong! It was no copy/paste for me... I forgot to enable the key called plugins while I have user-switching added under it! :) Glad that I could find it out quickly.

Freyert commented 6 years ago
  1. parsing begins
  2. parser constructor
  3. Handler Abstract Class
  4. Document Stream Handler
    • Does not handleHandler#event_location.
  5. But it's parent TreeBuilder does.
  6. Treebuilder#set_location
    • sets the location for an event.
  7. TreeBuilder#set_start_location and TreeBuilder#set_end_location
    • sets the line number where an event starts and ends, but those events are based on the node passed in. What gets passed in when an error is encountered?
    • Looks like the node type is important if we remember the error message:

      did not find expected key while parsing a block mapping at line 2 column 1 (Psych::SyntaxError)

  8. Is it an issue with Node::Mapping? Probably not.
    • there are no calls to instantiate at a Mapping in the TreeBuilder.
  9. Is it an issue with Node::Stream? More likely . . .
    • Streams are made up of child nodes, but its difficult to tell how exactly.
Freyert commented 6 years ago

It seems like this is manifesting itself in a variety of ways:

  1. colon within string
  2. unescaped double quotes within a value
  3. not closing your strings
  4. In general, bad whitespace.
WaKeMaTTa commented 6 years ago

When this happens to me, I use this tool it tells you the line that is wrong:

https://yamlchecker.com/

jmervine commented 6 years ago

I saw this issue ("line 1, column 1") do to a comma at the end of the line after converting json by hand.

E.g.

foo:
- "bar bah",
- "asdf asdf"
mrbrant89 commented 6 years ago

Just had this issue too. It indicated that the error was on line 2, column 1. But it was on line 13 (extra quote). Thanks for the tip, I'm not crazy, there really was an error in there 👍

OkThought commented 6 years ago

Had the same issue, but instead of wrong whitespaces I got comments inside a single command like the following:

  script:
    - if true; then
         # comment
         echo hello;
      fi

The travis lint reported error on line 2, column 1 or on line 1, column 1 during my experiments. While error was on some 23 line :/ I'm glad I solved this puzzle :)

OmariGrant commented 6 years ago

Had the same issue on Windows 10 today. I used https://yamlchecker.com/ as recommended by @WaKeMaTTa

Resolution: No double quotes needed for Windows directory.

vgvassilev commented 6 years ago

I ran into the same problem and decided to open an issue https://github.com/travis-ci/travis-yaml/issues/128

Saket-Kr commented 6 years ago

I had double 'double quotes' in one of my stories. removing that solved the problem.

patricknelson commented 6 years ago

Also: Zero width space characters (​)

I had the same issue after copying/pasting a YAML config from a web page into my IDE and couldn't figure out why it worked fine when I typed it but not when I pasted the contents. In my case, it was loads of nefarious zero width space characters embedded randomly in my copied text which were completely invisible. I didn't realize it until I wrote a little JavaScript loop to iterate over every character in a suspicious string passed into yamllint.com until I found something that was off:

// Copied from very first line of: http://bbc-news.github.io/wraith/configs.html
const string = '​#Headless browser option​';
let output = '';
for(var i = 0; i < string.length; i++) {
    output += '\'' + string.charAt(i) + '\'' + ' (' + string.charCodeAt(i) +  ') ';
}

// Outputs: '​' (8203) '#' (35) 'H' (72) 'e' (101) 'a' ... etc
console.log(output);

Bingo: 8203

You can't even see it in the code above, but it's still there (try it yourself). Once I figured that out, I was able to create a quick script to remove all characters in my YAML config which were outside of my expected character code range and suddenly it started parsing properly.

AdiedX commented 5 years ago

Specifically, I got a yaml did not find expected ',' or ']' error. It wasn't a spacing issue for me:

Original yaml block:

routes:
  - name: svc-name-private
    apply_to: svc-name
    config:
      hosts: [${POD_NAMESPACE}svc-name.${DOMAIN_NAME}]

The string with environment variables needed to be within quotes in the hosts array:

routes:
  - name: svc-name-private
    apply_to: svc-name
    config:
      hosts: ['${POD_NAMESPACE}svc-name.${DOMAIN_NAME}']
TravisHi commented 5 years ago

Just had this when trying to set up a project via homestead. In my case I had incorrectly indented - map: project.test

xofred commented 5 years ago

In my case was extra comma instead of extra/ lack of whitespace😂

  node:
    ip: '127.0.0.1',
    port: 7001
ksac1 commented 5 years ago

Hi .

I am getting this error in my yaml file: "(): did not find expected key while parsing a block mapping at line". Any thoughts about it? Please share. Thanks

jabelk commented 5 years ago

ran into the when vs code gave me extra quotes I did not meant to have like ""value"

datadogmandal commented 5 years ago

i am getting below error level=info ts=2019-07-24T06:16:30.771910421Z caller=main.go:223 fd_limits="(soft=1024, hard=4096)" level=info ts=2019-07-24T06:16:30.773881011Z caller=main.go:504 msg="Starting TSDB ..." level=info ts=2019-07-24T06:16:30.776524147Z caller=main.go:514 msg="TSDB started" level=info ts=2019-07-24T06:16:30.776627826Z caller=main.go:588 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml level=info ts=2019-07-24T06:16:30.776788925Z caller=main.go:398 msg="Stopping scrape discovery manager..." level=info ts=2019-07-24T06:16:30.776809603Z caller=main.go:411 msg="Stopping notify discovery manager..." level=info ts=2019-07-24T06:16:30.776822544Z caller=main.go:432 msg="Stopping scrape manager..." level=info ts=2019-07-24T06:16:30.776920322Z caller=manager.go:460 component="rule manager" msg="Stopping rule manager..." level=info ts=2019-07-24T06:16:30.776940701Z caller=manager.go:466 component="rule manager" msg="Rule manager stopped" level=info ts=2019-07-24T06:16:30.776954733Z caller=notifier.go:512 component=notifier msg="Stopping notification manager..." level=info ts=2019-07-24T06:16:30.776590908Z caller=web.go:382 component=web msg="Start listening for connections" address=0.0.0.0:9090 level=info ts=2019-07-24T06:16:30.782628706Z caller=main.go:394 msg="Scrape discovery manager stopped" level=info ts=2019-07-24T06:16:30.782696744Z caller=main.go:407 msg="Notify discovery manager stopped" level=warn ts=2019-07-24T06:16:30.783021721Z caller=web.go:461 component=web msg="error serving gRPC" err="grpc: the server has been stopped" level=warn ts=2019-07-24T06:16:30.783067808Z caller=web.go:456 component=web msg="error serving HTTP" err="mux: listener closed" level=info ts=2019-07-24T06:16:30.783094204Z caller=main.go:426 msg="Scrape manager stopped" level=info ts=2019-07-24T06:16:30.783113059Z caller=main.go:573 msg="Notifier manager stopped" level=error ts=2019-07-24T06:16:30.788525534Z caller=main.go:582 err="Error loading config couldn't load configuration (--config.file=/etc/prometheus/prometheus.yml): parsing YAML file /etc/prometheus/prometheus.yml: yaml: line 14: did not find expected key" level=info ts=2019-07-24T06:16:30.788587683Z caller=main.go:584 msg="See you next time!"

my yml file code is [root@techops-rnd-srv prometheus]# cat prometheus.yml global: scrape_interval: 15s evaluation_interval: 15s

rule_files:

- "first.rules"

- "second.rules"

scrape_configs:

vargava commented 5 years ago

This online verifier has become a handy tool for me. Points to the offensive line https://codebeautify.org/yaml-validator

HarryCaveMan commented 5 years ago

I am seeing Template format error: YAML not well-formed. (line 324, column 1)...BUT MY FILE IS ONLY 282 LINES, and has been tested using a separate linter and is valid yaml

ViSparks commented 4 years ago

Thanks @illepic!

chongfai13 commented 4 years ago

Thanks! Life saver, it's actually a typo at my .yml file.

MaheshChandra781 commented 4 years ago

To anyone stumbling through the internet that finds this issue for related reasons: Somewhere in your YAML you have extra spaces (or not enough spaces) before a key value. Thank you all for helping me solve this on a completely different project!

Edit on December 10, 2019: A great YAML linter may help here: https://jsonformatter.org/yaml-formatter.

@illepic Thanks! In my case, I found that my YAML file had extra spaces at the end of few lines. I found them out by placing my cursor at the end of those lines. After deleting them, my build was successful!!

thubamamba commented 4 years ago

@illepic Thank you so much! I was missing an extra space.

This worked for me. Thank you.