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

cmd/fmt: fmt emits invalid CUE when commenting struct references in lists #2274

Closed
jpluscplusm opened this issue Feb 18, 2023 · 4 comments
Closed
Labels
fmt Related to formatting functionality. good first issue Good for newcomers NeedsFix

Comments

@jpluscplusm
Copy link
Collaborator

What version of CUE are you using (cue version)?

cue version v0.5.0-beta.5

go version go1.19.3
       -compiler gc
       -trimpath true
     CGO_ENABLED 0
          GOARCH amd64
            GOOS linux
         GOAMD64 v1

Does this issue reproduce with the latest stable release?

Yes.

What did you do?

My editor runs cue fmt when it saves a .cue file.

Just occasionally I was being confused by cue command failures -- happening later on in the development process where I pointed the CUE CLI at a .cue file -- where I was /sure/ I'd not had cue fmt failures when saving the file and exiting ... but then I discovered syntax errors in the file.

Eventually I realised: I wasn't introducing the errors - cue fmt was.

Sometimes cue fmt gets confused about trailing comments, and moves required list item separators inside the comment. Here's a txtar repro where cue fmt emits invalidate CUE:

exec cue vet file.cue
exec cue fmt file.cue
exec cue vet file.cue
-- file.cue --
package p

#Number: two: 2

aList: [
  1,
  #Number.two, // comment
  3,
]

The bug seems to be picky about the lists it infects, and there's a slightly different list/comment scenario I've seen it fail in. Here, the blank line after the comment seems important: removing it stops the bug ocurring.

exec cue vet file.cue
exec cue fmt file.cue
exec cue vet file.cue
-- file.cue --
package p

#Number: two: 2

aList: [
  1,
  #Number.two,
  // comment

  3,
]

Both /seem/ to be related to that struct usage, and I've not managed to get the bug to affect a simpler list form.

What did you expect to see?

I expected to be able to put comments wherever I like; and for cue fmt either to tell me their form is not acceptable, or to emit valid CUE.

What did you see instead?

cue fmt emits invalid CUE.

@jpluscplusm jpluscplusm added NeedsInvestigation Triage Requires triage/attention labels Feb 18, 2023
@jpluscplusm jpluscplusm changed the title cmd/fmt: fmt emits invalid CUE in some situations cmd/fmt: fmt emits invalid CUE when commenting struct references in lists Feb 19, 2023
@jpluscplusm jpluscplusm changed the title cmd/fmt: fmt emits invalid CUE when commenting struct references in lists cmd/fmt: fmt emits invalid CUE when commenting struct references in lists Feb 19, 2023
@uhthomas
Copy link
Contributor

I also observe this. It's annoying as the comma is appended every time cue fmt is run. Eventually there are hundreds of them!

A really minimal example still produces this.

a: [
    "a"
    // b,
]

@myitcv
Copy link
Member

myitcv commented Apr 6, 2023

Thanks @jpluscplusm.

Here's a slight rework of your example to make it clearer what the output from fmt is, and why therefore it is problematic.

# Verify the file is initially valid
exec cue vet file.cue

# Format the file. This should not ever make it invalid.
exec cue fmt file.cue

# For the reader, compare the formatted result with a golden file as part of
# the test to make clear the problem that fmt introduces.
# Remove this line when attempting to fix the bug.
cmp file.cue file.cue.formatted

# Check the file is still valid after fmt
exec cue vet file.cue

-- file.cue --
package p

#Number: two: 2

aList: [
  1,
  #Number.two, // comment
  3,
]
-- file.cue.formatted --
package p

#Number: two: 2

aList: [
	1,
	#Number.two // comment,
	3,
]

Just to clarify however, my script above will fail when this bug is fixed because the cmp step will fail.

@uhthomas
Copy link
Contributor

Another reproducer:

foo: [
	"bar" + 1 // some comment,,,,,,,
]

I don't believe this happens with basic elements though?

@mvdan mvdan removed this from the fmt-redesign milestone Mar 19, 2024
@NoamTD
Copy link
Contributor

NoamTD commented Apr 21, 2024

I took a look at the fmt implementation and managed to reproduce quite a few more cases where this breaks:

exec cue fmt file.cue
cmp file.cue bad-result.cue

-- file.cue --
import "strings"

someList: [
  a.b,                  // comment
  (a),                  // comment
  +1,                   // comment
  a[0],                 // comment
  strings.Sort([1, 2]), // comment
]
-- bad-result.cue --
import "strings"

someList: [
	a.b                  // comment,
	(a                   // comment),
	+1                   // comment,
	a[0                  // comment],
	strings.Sort([1, 2], // comment),
]

cueckoo pushed a commit that referenced this issue Apr 22, 2024
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
cueckoo pushed a commit that referenced this issue Apr 22, 2024
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
cueckoo pushed a commit that referenced this issue Apr 22, 2024
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
cueckoo pushed a commit that referenced this issue Apr 22, 2024
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
cueckoo pushed a commit that referenced this issue Apr 22, 2024
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
cueckoo pushed a commit that referenced this issue Apr 22, 2024
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
cueckoo pushed a commit that referenced this issue Apr 22, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fmt Related to formatting functionality. good first issue Good for newcomers NeedsFix
Projects
None yet
Development

No branches or pull requests

5 participants