Skip to content

Commit

Permalink
cue/cmd: always write stdin to stdout in cue fmt
Browse files Browse the repository at this point in the history
A recent CL (https://cuelang.org/cl/1193934) modified cue fmt to only
write to files in case they were not well formatted.

A bug was introduced by this change, in which `cue fmt -` would only
write to stdout in case the standard input was not formatted. This is
not correct, as users would expect there to always be output in this
case.

This is fixed by always writing the provided stdin to stdout,
regardless of whether it was well formatted or not.

Fixes #3067.

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: Id69a50c454c3871ec56526de2cd622cdd6ded730
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193969
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
NoamTD authored and myitcv committed Apr 28, 2024
1 parent ebcf2f5 commit 70cc402
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
9 changes: 6 additions & 3 deletions cmd/cue/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -98,6 +99,10 @@ func newFmtCmd(c *Command) *cobra.Command {
file.Source = original
}
cfg.Out = &formatted
if file.Filename == "-" && !doDiff && !check {
// Always write to stdout if the file is read from stdin.
cfg.Out = io.MultiWriter(cfg.Out, stdout)
}

var files []*ast.File
d := encoding.NewDecoder(cmd.ctx, file, &cfg)
Expand Down Expand Up @@ -150,9 +155,7 @@ func newFmtCmd(c *Command) *cobra.Command {
case check:
fmt.Fprintln(stdout, path)
case file.Filename == "-":
if _, err := fmt.Fprint(stdout, formatted.String()); err != nil {
exitOnErr(cmd, err, false)
}
// already written to stdout during encoding
default:
if err := os.WriteFile(file.Filename, formatted.Bytes(), 0644); err != nil {
exitOnErr(cmd, err, false)
Expand Down
4 changes: 3 additions & 1 deletion cmd/cue/cmd/testdata/script/fmt_check.txtar
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
cd standalone

# succeeds when file is formatted
# succeeds with no output when file is formatted
exec cue fmt --check formatted.cue
! stdout .

stdin formatted.cue
exec cue fmt --check -
! stdout .

# fails and displays non formatted files
! exec cue fmt --check not_formatted.cue another/not_formatted.cue
Expand Down
26 changes: 22 additions & 4 deletions cmd/cue/cmd/testdata/script/fmt_stdin.txtar
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
stdin feed
# Verify that `cue fmt -` always writes valid CUE provided
# in stdin to stdout, even if it is well formatted.

stdin not-formatted
exec cue fmt -
cmp stdout formatted

stdin formatted
exec cue fmt -
cmp stdout expect-stdout
cmp stdout formatted

# Verify that `cue fmt -` does not write invalid CUE to stdout.
stdin syntax-error
! exec cue fmt -
! stdout .
cmp stderr syntax-error.stderr-golden

-- feed --
-- syntax-error --
a: {
-- syntax-error.stderr-golden --
expected '}', found 'EOF':
-:1:6
-- not-formatted --
foo : 2
a: {b: 3} // a comment
-- expect-stdout --
-- formatted --
foo: 2
a: {b: 3} // a comment
-- fmt/cue.mod/module.cue --

0 comments on commit 70cc402

Please sign in to comment.