yaml-diff icon indicating copy to clipboard operation
yaml-diff copied to clipboard

bug with yaml string wrapping syntax

Open dionysius opened this issue 3 years ago • 2 comments

Testcase:

package main

import (
	"testing"

	"github.com/sters/yaml-diff/yamldiff"
)

var yaml1 = `
value: |-
  foo
  bar
  baz
  special
    multiline
`

var yaml2 = `
value: "foo\nbar\nbaz\n\
special\n\
\  multiline"
`

func Test_PodYamlDiff(t *testing.T) {
	t.Parallel()

	yaml1Source, err := yamldiff.Load(yaml1)
	if err != nil {
		t.Error(err)
	}

	yaml2Source, err := yamldiff.Load(yaml2)
	if err != nil {
		t.Error(err)
	}

	diffs := yamldiff.Do(yaml1Source, yaml2Source)
	if len(diffs) > 0 {
		for _, diff := range diffs {
			if diff.Status() != yamldiff.DiffStatusSame {
				t.Log("diff detected\n" + diff.Dump())
				t.Fail()
			}
		}
	}
}

Result:

--- FAIL: Test_PodYamlDiff (0.00s)
    .../main_test.go:41: diff detected
        - value: "foo
        bar
        baz
        special
          multiline"
        + value: "foo
        bar
        baz
        \ special
        \ \  multiline"
        
FAIL

The handling of \n in strings seems to be fine, the issue seems to be:

  • the visual-only trailing \ indicating the line continues on the next line (baz -> special + special -> multiline)
  • and beginning \ indicating begin of space characters (multiline)
        ...\
        \  ...

I ran into a diff issue after using yq to manipulate some yaml files. The problem is not yq tho, the resulting yaml seems to be a correct yaml. If you copy those two yamls to e.g. https://www.yamldiff.com/ it doesn't yield a diff: Screenshot_20221121_143822

dionysius avatar Nov 21 '22 13:11 dionysius

Replaced yq with gojq for the manipulation steps since I anyway wanted to move away from python helpers. gojq --yaml-input --yaml-output seems not to do word wrapping so I don't run into this issue anymore. Bug is still valid though.

dionysius avatar Nov 21 '22 14:11 dionysius

@dionysius It seems caused by goccy/go-yaml. I'm trying to fix it here: https://github.com/goccy/go-yaml/pull/342

sters avatar Jan 28 '23 16:01 sters

I updated go-yaml lib and it seems solved now. I added test case here: https://github.com/sters/yaml-diff/blob/v1.4.0/yamldiff/yaml_test.go#L357-L372

sters avatar Dec 05 '24 05:12 sters