cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.07k stars 288 forks source link

cmd/cue: fmt error when given files from different packages #2523

Closed selfuryon closed 6 months ago

selfuryon commented 1 year ago

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

$ cue version
cue version 0.5.0

go version go1.20.6
      -buildmode exe
       -compiler gc
       -trimpath true
     CGO_ENABLED 1
          GOARCH amd64
            GOOS linux
         GOAMD64 v1

Does this issue reproduce with the latest stable release?

Yes

What did you do?

I tried to create two cue files in two different packages and format them via cue fmt . and `cue fmt :

cd $(mktemp -d)
(
set -e
cat <<EOD > one.cue
package one

a:    10
EOD
cat <<EOD > two.cue
package two

b:    20
EOD
cue fmt .
cue fmt one.cue two.cue
)

What did you expect to see?

All files are formatted without any errors.

What did you see instead?

When I use cue fmt <file1> <file2> I got this error:

found packages "one" (one.cue) and two (two.cue) in "/tmp/tmp.BsLVVW5zAy"

Why this is important

I added a cue formatting support to treefmt-nix which uses just treefmt to format different files in repository. It works via passing a file list as an argument to formatter (cue fmt). This is the place where I faced with that issue, because in my project I have differernt packages in CUE.

selfuryon commented 1 year ago

Related issues: https://github.com/cue-lang/cue/issues/1938 https://github.com/cue-lang/cue/issues/728

mvdan commented 1 year ago

Thanks for filing this! Definitely a bug; formatting shouldn't care about package names or imports.

For now, I think your best workaround is to format one file at a time. I realise that's slow, but I'm not sure there's a better general workaround right now.