cue icon indicating copy to clipboard operation
cue copied to clipboard

cmd/cue: fmt is less flexible than gofmt

Open mvdan opened this issue 2 years ago • 5 comments

Is your feature request related to a problem? Please describe.

Coming from Go, we have go fmt and gofmt - the former works on packages, and the latter works on files or stdin and has more features, such as the -d flag to see the difference that formatting would make without writing it to disk.

CUE learns from go fmt with cue fmt, but it does not have a cuefmt. So the ability to format entire directories (ignoring package patterns), stdin, or to show diffs, is currently not present.

Describe the solution you'd like

I think it would be reasonable to teach cue fmt these features, for example via flags like -d or -diff, and -f or -file to work on files rather than packages.

Describe alternatives you've considered

We could also introduce a cuefmt, but it feels somewhat unnecessary, and in Go people get very confused between the two tools.

I would even argue that formatting with files and directories is better than with packages - formatting packages can make it harder when one wants to format multiple modules at once (e.g. monorepos). But that would be a form of breaking change.

Additional context

One fmt to rule them all - @Lyoness

mvdan avatar May 24 '22 14:05 mvdan

One other flag that would be nice is cue fmt -l, to print the filenames that were modified, borrowing from gofmt -l.

mvdan avatar May 24 '22 14:05 mvdan

See #363 for previous coverage.

seh avatar May 24 '22 15:05 seh

Here are two compensatory steps from a GitHub Actions workflow job that I wrote:

# At present, "cue fmt" only mutates, and has no reporting mode.
# See:
# - https://github.com/cuelang/cue/issues/363
# - https://github.com/cuelang/cue/pull/247
- name: Format CUE files
  run: |
    cue fmt ./...
- name: Ensure no CUE files were reformatted
  run: |
    test -z "$(git status --porcelain)" || (git status --short; git diff; false)

I don't like it one bit.

seh avatar May 24 '22 15:05 seh

See #363 for previous coverage.

Ah, thank you - I didn't see that one. I guess this issue is a more general feature request, as I also want support for files/directories/stdin. Arguably that's a separate feature request from -d or -l for diffs or listing files to modify, but in general it's all about features we lack when compared to gofmt.

mvdan avatar May 24 '22 15:05 mvdan

I am trying this issue (maybe partially) now.

What I did

  1. prevent overwriting if contents are not changed
    • https://github.com/cue-lang/cue/issues/363
  2. Show diff as -d option
    • with either github.com/google/go-cmp/cmp or github.com/kylelemons/godebug/diff
    • this does not write any files, just for check
  3. List file nmae with -l option
    • Like go fmt output
    • the behaviour without any flags is almost same as it is
  4. Introduce concrete Source type
    • this is not related with fmt directly.
    • may be better to split into another patch?
      • change is trivial but the size is large.

Any advice that you can give me would be greatly appreciated.

cc @myitcv

satotake avatar Aug 25 '22 12:08 satotake