From ce3131723ac76a96962c893b0273159779b7cec0 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 20 Aug 2026 15:59:11 +1000 Subject: [PATCH] Pr 2825 (#2836) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * disable hard line wrapping The formatting defaults of the used YAML library changed. Since yq has no mechanism for the user to override them, change them back to what we used before. * Clamp YAML indent and close dumper after encoding. Clamp indent to the library's 2-9 range, wrap dumper setup errors with %w, and always close the dumper after dump. Co-authored-by: Cursor --------- Co-authored-by: Maximilian Gaß Co-authored-by: Cursor --- pkg/yqlib/encoder_yaml.go | 27 +++++++++++++++++++++------ pkg/yqlib/operator_load_test.go | 2 +- pkg/yqlib/operator_multiply_test.go | 4 ++-- pkg/yqlib/operator_select_test.go | 2 +- pkg/yqlib/operator_sort_keys_test.go | 2 +- pkg/yqlib/yaml_test.go | 6 ++++++ 6 files changed, 32 insertions(+), 11 deletions(-) diff --git a/pkg/yqlib/encoder_yaml.go b/pkg/yqlib/encoder_yaml.go index b46ae444..8f84edaf 100644 --- a/pkg/yqlib/encoder_yaml.go +++ b/pkg/yqlib/encoder_yaml.go @@ -2,6 +2,7 @@ package yqlib import ( "bytes" + "fmt" "io" "strings" @@ -49,23 +50,37 @@ func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error { destination = tempBuffer } - var encoder = yaml.NewEncoder(destination) + indent := ye.prefs.Indent + if indent < 2 { + indent = 2 + } else if indent > 9 { + indent = 9 + } - encoder.SetIndent(ye.prefs.Indent) - if ye.prefs.CompactSequenceIndent { - encoder.CompactSeqIndent() + dumper, err := yaml.NewDumper(destination, + yaml.WithV3Defaults(), + yaml.WithIndent(indent), + yaml.WithCompactSeqIndent(ye.prefs.CompactSequenceIndent), + yaml.WithLineWidth(-1), + ) + if err != nil { + return fmt.Errorf("configure YAML encoding: %w", err) } target, err := node.MarshalYAML() - if err != nil { + _ = dumper.Close() return err } trailingContent := target.FootComment target.FootComment = "" - if err := encoder.Encode(target); err != nil { + err = dumper.Dump(target) + if closeErr := dumper.Close(); err == nil { + err = closeErr + } + if err != nil { return err } diff --git a/pkg/yqlib/operator_load_test.go b/pkg/yqlib/operator_load_test.go index c7a8480a..aab885ae 100644 --- a/pkg/yqlib/operator_load_test.go +++ b/pkg/yqlib/operator_load_test.go @@ -77,7 +77,7 @@ var loadScenarios = []expressionScenario{ document: `{something: {file: "thing.yml"}, over: {here: [{file: "thing.yml"}]}}`, expression: `(.. | select(has("file"))) |= load("../../examples/" + .file)`, expected: []string{ - "D0, P[], (!!map)::{something: {a: apple is included, b: cool.}, over: {here: [{a: apple is included,\n b: cool.}]}}\n", + "D0, P[], (!!map)::{something: {a: apple is included, b: cool.}, over: {here: [{a: apple is included, b: cool.}]}}\n", }, }, { diff --git a/pkg/yqlib/operator_multiply_test.go b/pkg/yqlib/operator_multiply_test.go index fbd556bf..721a1f5b 100644 --- a/pkg/yqlib/operator_multiply_test.go +++ b/pkg/yqlib/operator_multiply_test.go @@ -83,7 +83,7 @@ var nodeWithFooter = `a: apple var document = `a: &cat {name: cat} b: {name: dog} -c: +c: <<: *cat ` @@ -514,7 +514,7 @@ var multiplyOperatorScenarios = []expressionScenario{ environmentVariables: map[string]string{"originalPath": ".myArray", "otherPath": ".newArray", "idPath": ".a"}, expression: mergeExpression, expected: []string{ - "D0, P[], (!!map)::{myArray: [{a: apple, b: appleB2}, {a: kiwi, b: kiwiB}, {a: banana, b: bananaB, c: bananaC},\n {a: dingo, c: dingoC}], something: else}\n", + "D0, P[], (!!map)::{myArray: [{a: apple, b: appleB2}, {a: kiwi, b: kiwiB}, {a: banana, b: bananaB, c: bananaC}, {a: dingo, c: dingoC}], something: else}\n", }, }, { diff --git a/pkg/yqlib/operator_select_test.go b/pkg/yqlib/operator_select_test.go index b9bcbbcd..35214f39 100644 --- a/pkg/yqlib/operator_select_test.go +++ b/pkg/yqlib/operator_select_test.go @@ -98,7 +98,7 @@ var selectOperatorScenarios = []expressionScenario{ document: `[{animal: cat, legs: {cool: true}}, {animal: fish}]`, expression: `(.[] | select(.legs.cool == true).canWalk) = true | (.[] | .alive.things) = "yes"`, expected: []string{ - "D0, P[], (!!seq)::[{animal: cat, legs: {cool: true}, canWalk: true, alive: {things: yes}}, {animal: fish,\n alive: {things: yes}}]\n", + "D0, P[], (!!seq)::[{animal: cat, legs: {cool: true}, canWalk: true, alive: {things: yes}}, {animal: fish, alive: {things: yes}}]\n", }, }, { diff --git a/pkg/yqlib/operator_sort_keys_test.go b/pkg/yqlib/operator_sort_keys_test.go index 562994d4..c895b194 100644 --- a/pkg/yqlib/operator_sort_keys_test.go +++ b/pkg/yqlib/operator_sort_keys_test.go @@ -37,7 +37,7 @@ var sortKeysOperatorScenarios = []expressionScenario{ document: `{bParent: {c: dog, array: [3,1,2]}, aParent: {z: donkey, x: [{c: yum, b: delish}, {b: ew, a: apple}]}}`, expression: `sort_keys(..)`, expected: []string{ - "D0, P[], (!!map)::{aParent: {x: [{b: delish, c: yum}, {a: apple, b: ew}], z: donkey}, bParent: {array: [\n 3, 1, 2], c: dog}}\n", + "D0, P[], (!!map)::{aParent: {x: [{b: delish, c: yum}, {a: apple, b: ew}], z: donkey}, bParent: {array: [3, 1, 2], c: dog}}\n", }, }, } diff --git a/pkg/yqlib/yaml_test.go b/pkg/yqlib/yaml_test.go index bee7856f..15c9a826 100644 --- a/pkg/yqlib/yaml_test.go +++ b/pkg/yqlib/yaml_test.go @@ -106,6 +106,12 @@ var yamlFormatScenarios = []formatScenario{ input: "[1, 2]", expected: "[1, 2]\n", }, + { + description: "long line", + skipDoc: true, + input: "field: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt", + expected: "field: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt\n", + }, } var yamlParseScenarios = []expressionScenario{