cue icon indicating copy to clipboard operation
cue copied to clipboard

cue fmt indents top-level comments

Open cueckoo opened this issue 4 years ago • 3 comments
trafficstars

Originally opened by @bbkane in https://github.com/cuelang/cue/issues/722

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

$ cue version
cue version 0.3.0-beta.3 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

$ cat fields.cue
// comment
hello: "world"
$ cue fmt

What did you expect to see?

$ cat fields.cue
// comment
hello: "world"

What did you see instead?

$ cat fields.cue
	// comment
hello: "world"

cueckoo avatar Jul 03 '21 10:07 cueckoo

This also applies to cue export --out cue FWIW. This testscript fails:

exec cue export --out cue c.cue
cmp stdout want.cue
-- c.cue --
// Comment
foo: 1
-- want.cue --
// Comment
foo: 1

rogpeppe avatar Feb 25 '22 10:02 rogpeppe

@rogpeppe cue export uses the format package for the output, just like fmt.

mpvl avatar May 17 '22 12:05 mpvl

I've fixed a similar issue in #1704, and I'd like to work on this issue :) @mpvl

FogDong avatar May 17 '22 13:05 FogDong

Elaborating from testscript above by @rogpeppe, this shows similarities and differences between fmt, def and export --out cue.

Are any of these differences intended?

# show the`cue export` handling of comments: run with `testscript -continue 722-4.txtar
force-verbose
env CUEDO_FMT_DEBUGSTR=1
exec cue version

# 3 commands have the same behavior with c.cue
# they preserve the comment but indent it

exec cat c.cue

#A ------------------------------------
exec cue export --out cue c.cue
cmp stdout want.cue         ### fail - indents

#B ------------------------------------
exec cue def c.cue
cmp stdout want.cue         ### fail - indents

#C ------------------------------------
exec cue fmt c.cue          ### fmt is the last because it modifies c.cue in place
cmp c.cue want.cue          ### fail - indents

# 2 commands have the same behavior with 722-2.cue
# they drop the comment

#D ------------------------------------
exec cue export --out cue 722-2.cue
cmp stdout 722-2.cue                    ### fail - drops comment and newline

#E ------------------------------------
exec cue def 722-2.cue
cmp stdout 722-2.cue                    ### fail - drops comment and newline

# cue fmt preserves all unchanged

#F ------------------------------------
exec cue fmt 722-2.cue  ### fmt is the last because it modifies 722-2.cue in place
cmp 722-2.cue 722-2-want-maybe.cue      ### pass - unchanged

# files -------------------------------

-- c.cue --
// Comment
foo: 1
-- want.cue --
// Comment
foo: 1
-- 722-2.cue --
// comment

hello: "world"
-- 722-2-want-maybe.cue --
// comment

hello: "world"
-- end --

rudifa avatar Nov 18 '23 20:11 rudifa

This also affects the playground: https://cuelang.org/play/?id=G3wjR5M0KCi#w=function&i=cue&f=eval&o=cue

I've just started filling out the set of preloadable examples for the playground, and almost every single one of them will need leading comments, explaining what's about to be demonstrated. This bug will give a less than great impression of the playground to newcomers, IMHO!!

jpluscplusm avatar Mar 28 '24 09:03 jpluscplusm