Skip to content

Commit

Permalink
cmd/cue: print paths relative to the current directory in fmt --check
Browse files Browse the repository at this point in the history
Fixes a bug introduced in https://cuelang.org/cl/1191220 where badly formatted paths
were printed relative to the cue module root instead of
the current working directory.

Signed-off-by: Noam Dolovich <[email protected]>
Change-Id: I70d258bff20175059117a6301ef9d8b01391b60a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1191475
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
NoamTD authored and mvdan committed Apr 5, 2024
1 parent fc836b1 commit 79ded70
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
14 changes: 11 additions & 3 deletions cmd/cue/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"fmt"
"os"
"path/filepath"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
Expand Down Expand Up @@ -121,17 +122,24 @@ func newFmtCmd(c *Command) *cobra.Command {
}

if check && !bytes.Equal(formatted.Bytes(), original) {
badlyFormattedFiles = append(badlyFormattedFiles, inst.RelPath(file))
badlyFormattedFiles = append(badlyFormattedFiles, file.Filename)
}
}
}

if check && len(badlyFormattedFiles) > 0 {
cwd, _ := os.Getwd()
stdout := cmd.OutOrStdout()
for _, f := range badlyFormattedFiles {
if f != "-" {
fmt.Fprintln(stdout, f)
if f == "-" {
continue
}

relPath, err := filepath.Rel(cwd, f)
if err != nil {
relPath = f
}
fmt.Fprintln(stdout, relPath)
}
os.Exit(1)
}
Expand Down
27 changes: 23 additions & 4 deletions cmd/cue/cmd/testdata/script/fmt_check.txtar
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cd standalone

# succeeds when file is formatted
exec cue fmt --check formatted.cue

Expand All @@ -18,13 +20,30 @@ stdin not_formatted.cue
! exec cue fmt --check -
! stdout .

-- formatted.cue --
cd ../module

# files are printed relative to CWD
! exec cue fmt --check ./...
cmpenv stdout stdout.golden
cd example
! exec cue fmt --check ./...
cmp stdout stdout.golden

-- standalone/formatted.cue --
foo: "bar"
-- not_formatted.cue --
-- standalone/not_formatted.cue --
foo: "bar"
-- another/not_formatted.cue --
-- standalone/another/not_formatted.cue --
bar: "baz"
x: 1
-- expected-output --
-- standalone/expected-output --
not_formatted.cue
another${/}not_formatted.cue
-- module/cue.mod/module.cue --
module: "example.com"
-- module/stdout.golden --
example${/}not_formatted.cue
-- module/example/not_formatted.cue --
foo: "bar"
-- module/example/stdout.golden --
not_formatted.cue

0 comments on commit 79ded70

Please sign in to comment.