cue icon indicating copy to clipboard operation
cue copied to clipboard

cmd/cue: fmt indents too much sometimes

Open cueckoo opened this issue 4 years ago • 6 comments

Originally opened by @uhthomas in https://github.com/cuelang/cue/issues/1018

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

$ cue version
cue version v0.4.0 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

Format the following CUE source:

names: {
	categories: [
		"prometheus-operator",
	]
        kind:     "ThanosRuler"
        listKind: "ThanosRulerList"
        plural:   "thanosrulers"
        singular: "thanosruler"
}, scope: "Namespaced"

What did you expect to see?

No change -- having scope positioned like that is weird for sure though. This is because of https://github.com/cuelang/cue/issues/826

What did you see instead?

names: {
	categories: [
		"prometheus-operator",
	]
		kind:     "ThanosRuler"
		listKind: "ThanosRulerList"
		plural:   "thanosrulers"
		singular: "thanosruler"
}, scope: "Namespaced"

cueckoo avatar Jul 03 '21 10:07 cueckoo

Original reply by @myitcv in https://github.com/cuelang/cue/issues/1018#issuecomment-850477996

Likely related/identical to https://github.com/cuelang/cue/issues/1006

cueckoo avatar Jul 03 '21 11:07 cueckoo

Original reply by @uhthomas in https://github.com/cuelang/cue/issues/1018#issuecomment-850729954

Looks like it's actually even more common than I had first thought. Take this snippet:

// Some comment
some_long_key: "some_value"

Formatting results in

		// Some comment
some_long_key: "some_value"

Is this a regression? I don't remember seeing such behavior before.

cueckoo avatar Jul 03 '21 11:07 cueckoo

I just encountered this issue. Adding one more example:

$ cat <<EOF > /tmp/example.cue
heredoc> // First comment.
one: 1
// Second comment.
two: 2
EOF
$ cue export --out cue /tmp/example.cue
	// First comment.
one: 1
// Second comment.
two: 2
$ cue version
cue version 0.4.0 darwin/amd64

brandonbloom avatar Jan 22 '22 02:01 brandonbloom

Thanks, @brandonbloom! Good to see you here on GitHub and Slack!

Thanks for taking the time to add an example.

Just as an FYI, we try to encourage reproducers as txtar. From a purely selfish perspective as maintainers, this allows us to literally copy-paste from the GitHub issue into a CUE test case. Please see https://github.com/cue-lang/cue/wiki/Creating-test-or-performance-reproducers for more details. The approach described in the wiki is also not specific to CUE; for example the Go project relies heavily on txtar too.

myitcv avatar Jan 27 '22 09:01 myitcv

cue export --out cue ./example.cue > out.cue
-- example.cue --
	// First comment.
one: 1
// Second comment.
two: 2
-- out.cue --
	// First comment.
one: 1
// Second comment.
two: 2

brandonbloom avatar Jan 27 '22 17:01 brandonbloom

@brandonbloom the syntax of commands in a txtar archive is a limited shell-like subset called testscript.

So the repro in your case would look something like this:

# The starting state of x.cue is what
# we expect the result of cue fmt to be
cp x.cue x.cue.golden

exec cue fmt x.cue
cmp x.cue x.cue.golden

-- x.cue --
// First comment.
one: 1

// Second comment.
two: 2

which gives the output:

# The starting state of x.cue is what
# we expect the result of cue fmt to be (0.014s)
> cp x.cue x.cue.golden
> exec cue fmt x.cue
> cmp x.cue x.cue.golden
--- x.cue
+++ x.cue.golden
@@ -1,4 +1,4 @@
-       // First comment.
+// First comment.
 one: 1

 // Second comment.

FAIL: /tmp/testscript4093825254/repro.txt/script.txt:6: x.cue and x.cue.golden differ
error running repro.txt in /tmp/testscript4093825254/repro.txt

when run through cmd/testscript (which is a convenience wrapper for testing repro scripts, mentioned in the wiki).

myitcv avatar Jan 28 '22 06:01 myitcv