-
Notifications
You must be signed in to change notification settings - Fork 278
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
Cue fmt destroys working code #2567
Comments
Here is another test case, found by @rudifa in #2646:
|
EDITED:
The "working code" above is valid Here is a testscript with working code that passes
|
Here is the minimal comprehension example, with its
Re above use of |
I've sent https://review.gerrithub.io/c/cue-lang/cue/+/1173870 as a first fix, to ensure that newlines are always printed in the formatter. This fixes Rudi's latest reproducer, but the original reproducer with an index expression still fails. I think that might be a parser bug, so I've filed #2738. |
The proposed change 1173870 fixes the missing newline at the end of a comment, and issue #2738 proposes to allow a comma inside the index expressions. However, we need to solve yet another problem, that of comments misplaced with respect to elements they describe. Take this reproducer:
Clearly, the misplaced comments don't express what the author of the cue fragments intended. In fact, I believe that in these examples the formater should reproduce the input cue without any alterations. When above reproducer is run with cue built with change 1173870 the updated results still do not correspond to what the author intended:
I believe that these Line comments are misplaced after formatting because the parser did not attach them to the node that they refer to. In fact, the
|
This is similar to the nooverride whiteSpace flag, but only enforces the newline whitespace token to be printed. Note that https://cuelang.org/issue/2567 is not fully fixed, as the original reproducer now runs into https://cuelang.org/issue/2738. Updates cue-lang#2567. Signed-off-by: Daniel Martí <mvdan@mvdan.cc> Change-Id: I85ec0cc40487fdfc90288f075a004dd2e3826c51
Here is another reproducer:
The
shows that the parser gave to all 3 comments |
When list elements are formatted in cue/format, the algorithm prints each element followed by a comment. In case an element has associated comments, they are printed as part of the element. This causes an issue when the comments appear after the element, because the comma will be printed at the end of the comment, and will ulimately be considered part of the comment, causing syntax errors. For example, given the following valid example: list: [ a.b, // selector (a), // paren +1, // unary a[0], // index strings.Sort([1, 2]), // call a.b, // under selector a.b, // multiple // under // selector ] The current formatting logic will produce this: list: [ a.b // selector, (a // paren), +1 // unary, a[0 // index], strings.Sort([1, 2], // call), a.b // under selector, a.b // multiple // under // selector, ] To resolve this, all comments that appear after an element are temporarily disassociated from the element, so that the list formatting section can insert the comma in the correct location. The solution could be cleaner, but a larger refactor is avoided since an overhaul of cue/format is planned. Fixes #2274 Updates #2567 Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Icbbb10800ed3f04bd5adc658b883fa259844b7e9
When list elements are formatted in cue/format, the algorithm prints each element followed by a comment. In case an element has associated comments, they are printed as part of the element. This causes an issue when the comments appear after the element, because the comma will be printed at the end of the comment, and will ulimately be considered part of the comment, causing syntax errors. For example, given the following valid example: list: [ a.b, // selector (a), // paren +1, // unary a[0], // index strings.Sort([1, 2]), // call a.b, // under selector a.b, // multiple // under // selector ] The current formatting logic will produce this: list: [ a.b // selector, (a // paren), +1 // unary, a[0 // index], strings.Sort([1, 2], // call), a.b // under selector, a.b // multiple // under // selector, ] To resolve this, all comments that appear after an element are temporarily disassociated from the element, so that the list formatting section can insert the comma in the correct location. The solution could be cleaner, but a larger refactor is avoided since an overhaul of cue/format is planned. Fixes #2274 Updates #2567 Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Icbbb10800ed3f04bd5adc658b883fa259844b7e9
When list elements are formatted in cue/format, the algorithm prints each element followed by a comment. In case an element has associated comments, they are printed as part of the element. This causes an issue when the comments appear after the element, because the comma will be printed at the end of the comment, and will ulimately be considered part of the comment, causing syntax errors. For example, given the following valid example: list: [ a.b, // selector (a), // paren +1, // unary a[0], // index strings.Sort([1, 2]), // call a.b, // under selector a.b, // multiple // under // selector ] The current formatting logic will produce this: list: [ a.b // selector, (a // paren), +1 // unary, a[0 // index], strings.Sort([1, 2], // call), a.b // under selector, a.b // multiple // under // selector, ] To resolve this, all comments that appear after an element are temporarily disassociated from the element, so that the list formatting section can insert the comma in the correct location. The solution could be cleaner, but a larger refactor is avoided since an overhaul of cue/format is planned. Fixes #2274 Updates #2567 Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Icbbb10800ed3f04bd5adc658b883fa259844b7e9
When list elements are formatted in cue/format, the algorithm prints each element followed by a comment. In case an element has associated comments, they are printed as part of the element. This causes an issue when the comments appear after the element, because the comma will be printed at the end of the comment, and will ulimately be considered part of the comment, causing syntax errors. For example, given the following valid example: list: [ a.b, // selector (a), // paren +1, // unary a[0], // index strings.Sort([1, 2]), // call a.b, // under selector a.b, // multiple // under // selector ] The current formatting logic will produce this: list: [ a.b // selector, (a // paren), +1 // unary, a[0 // index], strings.Sort([1, 2], // call), a.b // under selector, a.b // multiple // under // selector, ] To resolve this, all comments that appear after an element are temporarily disassociated from the element, so that the list formatting section can insert the comma in the correct location. The solution could be cleaner, but a larger refactor is avoided since an overhaul of cue/format is planned. Fixes #2274 Updates #2567 Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Icbbb10800ed3f04bd5adc658b883fa259844b7e9
When list elements are formatted in cue/format, the algorithm prints each element followed by a comment. In case an element has associated comments, they are printed as part of the element. This causes an issue when the comments appear after the element, because the comma will be printed at the end of the comment, and will ulimately be considered part of the comment, causing syntax errors. For example, given the following valid example: list: [ a.b, // selector (a), // paren +1, // unary a[0], // index strings.Sort([1, 2]), // call a.b, // under selector a.b, // multiple // under // selector ] The current formatting logic will produce this: list: [ a.b // selector, (a // paren), +1 // unary, a[0 // index], strings.Sort([1, 2], // call), a.b // under selector, a.b // multiple // under // selector, ] To resolve this, all comments that appear after an element are temporarily disassociated from the element, so that the list formatting section can insert the comma in the correct location. The solution could be cleaner, but a larger refactor is avoided since an overhaul of cue/format is planned. Fixes #2274 Updates #2567 Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Icbbb10800ed3f04bd5adc658b883fa259844b7e9
When list elements are formatted in cue/format, the algorithm prints each element followed by a comment. In case an element has associated comments, they are printed as part of the element. This causes an issue when the comments appear after the element, because the comma will be printed at the end of the comment, and will ulimately be considered part of the comment, causing syntax errors. For example, given the following valid example: list: [ a.b, // selector (a), // paren +1, // unary a[0], // index strings.Sort([1, 2]), // call a.b, // under selector a.b, // multiple // under // selector ] The current formatting logic will produce this: list: [ a.b // selector, (a // paren), +1 // unary, a[0 // index], strings.Sort([1, 2], // call), a.b // under selector, a.b // multiple // under // selector, ] To resolve this, all comments that appear after an element are temporarily disassociated from the element, so that the list formatting section can insert the comma in the correct location. The solution could be cleaner, but a larger refactor is avoided since an overhaul of cue/format is planned. Fixes #2274 Updates #2567 Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Icbbb10800ed3f04bd5adc658b883fa259844b7e9
When list elements are formatted in cue/format, the algorithm prints each element followed by a comma. In case an element has associated comments, they are printed as part of the element. This causes an issue when the comments appear after the element, because the comma will be printed at the end of the comment, and will ulimately be considered part of the comment, causing syntax errors. For example, given the following valid example: list: [ a.b, // selector (a), // paren +1, // unary a[0], // index strings.Sort([1, 2]), // call a.b, // under selector a.b, // multiple // under // selector ] The current formatting logic will produce this: list: [ a.b // selector, (a // paren), +1 // unary, a[0 // index], strings.Sort([1, 2], // call), a.b // under selector, a.b // multiple // under // selector, ] To resolve this, all comments that appear after an element are temporarily disassociated from the element, so that the list formatting section can insert the comma in the correct location. The solution could be cleaner, but a larger refactor is avoided since an overhaul of cue/format is planned. Fixes #2274 Updates #2567 Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Icbbb10800ed3f04bd5adc658b883fa259844b7e9
When list elements are formatted in cue/format, the algorithm prints each element followed by a comma. In case an element has associated comments, they are printed as part of the element. This causes an issue when the comments appear after the element, because the comma will be printed at the end of the comment, and will ulimately be considered part of the comment, causing syntax errors. For example, given the following valid example: list: [ a.b, // selector (a), // paren +1, // unary a[0], // index strings.Sort([1, 2]), // call a.b, // under selector a.b, // multiple // under // selector ] The current formatting logic will produce this: list: [ a.b // selector, (a // paren), +1 // unary, a[0 // index], strings.Sort([1, 2], // call), a.b // under selector, a.b // multiple // under // selector, ] To resolve this, all comments that appear after an element are temporarily disassociated from the element, so that the list formatting section can insert the comma in the correct location. The solution could be cleaner, but a larger refactor is avoided since an overhaul of cue/format is planned. Fixes #2274 Updates #2567 Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Icbbb10800ed3f04bd5adc658b883fa259844b7e9 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193585 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
@mvdan I believe this can be closed now |
@NoamTD just to confirm, did you double check that all the examples above in this thread output valid CUE now? |
Ah, I thought I did but there's a single case left: [
if true // inline comment
{}] this still results in [
if true {}, // inline comment] I'll fix that up and then this can be closed |
When an if comprehension is placed in a list like so: [ if true // a comment {}] cue fmt will generate invalid cue: [ if true {}, // a comment ] The comment is placed on the same line as the closing bracket, causing the bracket to be lost. To fix this we place the bracket on a new line in such cases. Fixes #2567. Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Ie504380677b12f3d879920a88ed8f02b7b3d6035
When an if comprehension is placed in a list like so: [ if true // a comment {}] cue fmt will generate invalid cue: [ if true {}, // a comment ] The comment is placed on the same line as the closing bracket, causing the bracket to be lost. To fix this we place the bracket on a new line in such cases. Fixes #2567. Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Ie504380677b12f3d879920a88ed8f02b7b3d6035
When an if comprehension is placed in a list like so: [ if true // a comment {}] cue fmt will generate invalid cue: [ if true {}, // a comment ] The comment is placed on the same line as the closing bracket, causing the bracket to be lost. To fix this we place the bracket on a new line in such cases. Fixes #2567. Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Change-Id: Ie504380677b12f3d879920a88ed8f02b7b3d6035
This is similar to the nooverride whiteSpace flag, but only enforces the newline whitespace token to be printed. Note that https://cuelang.org/issue/2567 is not fully fixed, as the original reproducer now runs into https://cuelang.org/issue/2738. Updates #2567. Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Co-authored-by: Daniel Martí <mvdan@mvdan.cc> Change-Id: I83bebcca9aeba14f84cbcf4c4befa0677da3fb0f
This is similar to the nooverride whiteSpace flag, but only enforces the newline whitespace token to be printed. Note that https://cuelang.org/issue/2567 is not fully fixed, as the original reproducer now runs into https://cuelang.org/issue/2738. Fixes #2567. Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Co-authored-by: Daniel Martí <mvdan@mvdan.cc> Change-Id: I83bebcca9aeba14f84cbcf4c4befa0677da3fb0f
This is similar to the nooverride whiteSpace flag, but only enforces the newline whitespace token to be printed. Fixes #2567. Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Co-authored-by: Daniel Martí <mvdan@mvdan.cc> Change-Id: I83bebcca9aeba14f84cbcf4c4befa0677da3fb0f
This is similar to the nooverride whiteSpace flag, but only enforces the newline whitespace token to be printed. Fixes #2567. Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Signed-off-by: Daniel Martí <mvdan@mvdan.cc> Co-authored-by: Daniel Martí <mvdan@mvdan.cc> Change-Id: I83bebcca9aeba14f84cbcf4c4befa0677da3fb0f
This is similar to the nooverride whiteSpace flag, but only enforces the newline whitespace token to be printed. Fixes #2567. Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Co-authored-by: Daniel Martí <mvdan@mvdan.cc> Change-Id: I83bebcca9aeba14f84cbcf4c4befa0677da3fb0f
This is similar to the nooverride whiteSpace flag, but only enforces the newline whitespace token to be printed. Fixes #2567. Co-authored-by: Daniel Martí <mvdan@mvdan.cc> Co-authored-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com> Signed-off-by: Daniel Martí <mvdan@mvdan.cc> Change-Id: I83bebcca9aeba14f84cbcf4c4befa0677da3fb0f
Change has been successfully cherry-picked as 9bd3e17 1 is the latest approved patch-set. The change was submitted with unreviewed changes in the following files: ``` The name of the file: cue/format/node.go Insertions: 1, Deletions: 1. @@ -157,7 +157,7 @@ f.print(f.current.parentSep) } if ellipsis != nil { - // ensure that comments associated with original ellipsis are preserved + // ensure that comments associated with the original ellipsis are preserved n := &ast.Ellipsis{} ast.SetComments(n, ast.Comments(ellipsis)) f.decl(n) ``` ``` The name of the file: cue/format/testdata/comments.golden Insertions: 14, Deletions: 0. @@ -136,7 +136,21 @@ // comment including some tabs +// issue #2567 +foo: [ + bar["baz"], //some comment +] + +[ + if true {}, // inline comment +] + { + + foo: { + // some + ... // comment + } // comments ... // surrounding ``` ``` The name of the file: cue/format/testdata/comments.input Insertions: 15, Deletions: 0. @@ -146,9 +146,24 @@ // comment including some tabs + +// issue #2567 +foo: [ + bar["baz"], //some comment +] + +[ + if true // inline comment + {}] + { // comments ... // surrounding // an ellipsis + + foo: { + // some + [string]: _ // comment + } } ``` Patch-set: 4 Subject: cue/format: preserve comments associated with ellipsis Status: merged Commit: 9bd3e17 Tag: autogenerated:gerrit:merged Groups: f8d7609 Label: Code-Review=+2, bf3417339b0b9a28e5f9ba7cbeb6ff776065d2ad Label: TryBot-Result=+1, 15f76bc677cb3507a9007c145115e658f96390b2 Gerrit User 1017882 <1017882@d5d70762-12d0-45a1-890d-524b12d3f735> Label: SUBM=+1, fbe6e8de5e19567b2b7d473a950b1724b7fb0607 Submission-id: 1195884 Submitted-with: OK Submitted-with: Rule-Name: gerrit~DefaultSubmitRule Submitted-with: MAY: Code-Review: Gerrit User 1017720 <1017720@d5d70762-12d0-45a1-890d-524b12d3f735> Submitted-with: MAY: Unity-Result Submitted-with: MAY: Trigger-Unity-New Submitted-with: MAY: Unity-New-Result Submitted-with: MAY: TryBot-Result: Gerrit User 1017882 <1017882@d5d70762-12d0-45a1-890d-524b12d3f735> Submitted-with: MAY: Hold
What version of CUE are you using (
cue version
)?Does this issue reproduce with the latest stable release?
Yes
What did you do?
Apply
cue fmt
on code like thisWhat did you expect to see?
Code should not change, or some whitespace changes
What did you see instead?
Code that doesn't compile
The text was updated successfully, but these errors were encountered: