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

Comments in YAML break annotated source #254

Closed
shakefu opened this issue Oct 6, 2021 · 5 comments · Fixed by #257
Closed

Comments in YAML break annotated source #254

shakefu opened this issue Oct 6, 2021 · 5 comments · Fixed by #257
Labels
bug Something isn't working

Comments

@shakefu
Copy link

shakefu commented Oct 6, 2021

Or at least it seems like the comments are causing issue.

Here's a minimal reproduction:

package main

import (
	"fmt"

	"github.com/goccy/go-yaml"
)

// yamlSrc returns the annotated raw source at pathStr
func yamlSrc(raw []byte, pathStr string) (msg string, err error) {
	path, err := yaml.PathString(pathStr)
	if err != nil {
		return
	}
	source, err := path.AnnotateSource(raw, true)
	if err != nil {
		return
	}
	msg = "\n" + string(source)
	return
}

func main() {
	src := (`# This is my document
doc:
  # It has nice inline comments
  inline:
    # And value comments
    - value
    - other value
    # It isn't super consistent
    - last value
  other:
    - value
  # This comment should be line 12
  line:
    # And below should be line 15
    - fifteen
    - sixteen
  done: true  # Line 17
	`)
	msg, err := yamlSrc([]byte(src), "$.doc.line[0]")
	if err != nil {
		panic(err)
	}
	fmt.Println(msg)

	// Output expected (ish):
	// 13 |     line:
	// 14 |     # And below should be line 15
	// 15 |     - fifteen
	//          ^
	// 16 |     - sixteen
	// 17 |   done: true  # Line 17
	//
	// Actual output:
	// 13 |     line:    - fifteen
	//            ^
	// 16 |     - sixteen
	// 17 |   done: true  # Line 17
	// 18 |
}

And here's a screenshot of the output on OS X go 1.17 github.com/goccy/go-yaml v1.9.3:
image

@shakefu
Copy link
Author

shakefu commented Oct 6, 2021

Here's a tighter example of the miscounting/bad re-render:

package main

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

func main() {
	src := (`
# This is my document
doc:
  # This comment should be line 3
  line:
    # And below should be line 5
    - five
    - six
  end: seven
	`)
	path, _ := yaml.PathString("$.doc.line[0]")
	msg, _ := path.AnnotateSource([]byte(src), true)
	fmt.Println(string(msg))
}

image

@shakefu
Copy link
Author

shakefu commented Oct 6, 2021

Removing the leading newline in the src string still produces a miscount:

func main() {
	src := (`# This is my document
doc:
  # This comment should be line 3
  line:
    # And below should be line 5
    - five
    - six
  end: seven
	`)
	path, _ := yaml.PathString("$.doc.line[0]")
	msg, _ := path.AnnotateSource([]byte(src), true)
	fmt.Println(string(msg))
}

image

@shakefu
Copy link
Author

shakefu commented Oct 6, 2021

Still more compact:

func main() {
	src := (`doc:
  # This comment should be line 2
  line:
    - four
  end: five`)
	path, _ := yaml.PathString("$.doc.line[0]")
	msg, _ := path.AnnotateSource([]byte(src), true)
	fmt.Println(string(msg))
}

image

@shakefu
Copy link
Author

shakefu commented Oct 6, 2021

And without the comment it works fine:

func main() {
	src := (`doc:
  line:
    - three
  end: four`)
	path, _ := yaml.PathString("$.doc.line[0]")
	msg, _ := path.AnnotateSource([]byte(src), true)
	fmt.Println(string(msg))
}

image

@goccy
Copy link
Owner

goccy commented Oct 6, 2021

Thank you for the detailed report. I'll try to investigate this problem .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants