Skip to content

Parsing error when there are multiple spaces in front of a comment on a block-string #550

@jandubois

Description

@jandubois

I find it a bit hard to describe, but the example below will hopefully clarify it. You can parse a line like - field: | # comment (or any other block style indicator, like |- etc), but if there is more than one space between the block style indicator and the # sign, then there is a parsing error.

Here is a test.go program to demonstrate:

package main

import (
	"fmt"
	"github.com/goccy/go-yaml"
)

func main() {
	document1 := `
foo: | # comment
  bar
`
	document2 := `
foo: |  # comment
  bar
`
	var s map[string]string
	if err := yaml.Unmarshal([]byte(document1), &s); err != nil {
		fmt.Print(err)
	} else {
		fmt.Print(s["foo"])
	}
	if err := yaml.Unmarshal([]byte(document2), &s); err != nil {
		fmt.Print(err)
	} else {
		fmt.Print(s["foo"])
	}
}

Using Go 1.23.2.

With go-yaml v1.13.6 I get:

$ go run test.go
bar
[2:6] found invalid token
>  2 | foo: |  # comment
            ^
   3 |   bar

And with v1.15.2 I get

$ go run test.go
bar
[2:6] invalid header option: " "
>  2 | foo: |  # comment
            ^
   3 |   bar

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingparser

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions