Skip to content

Commit d9c5ae0

Browse files
NoamTDmvdan
authored andcommitted
cmd/cue: fail cue fmt if arg import path doesn't exist
CL https://cuelang.org/cl/1185292 fixed an error in which `cue fmt` would fail if any files had invalid imports. This was done by ignoring errors of type load.PackageError. This introduced a bug. When running `cue fmt ./non-existing`, the command will now succeed with status code 0 - despite the fact that the provided import path is invalid. To fix this we don't ignore `load.PackageError` if no additional files were found, because in this case the import path provided as an argument must have been invalid. Signed-off-by: Noam Dolovich <[email protected]> Change-Id: I87ef60960b61924bf4c9493e5c492caa907ab294 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194124 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent f9c2de9 commit d9c5ae0

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

cmd/cue/cmd/fmt.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ func newFmtCmd(c *Command) *cobra.Command {
7373
for _, inst := range builds {
7474
if inst.Err != nil {
7575
switch {
76-
case errors.As(inst.Err, new(*load.PackageError)):
76+
case errors.As(inst.Err, new(*load.PackageError)) && len(inst.BuildFiles) != 0:
77+
// Ignore package errors if there are files to format.
7778
case errors.As(inst.Err, new(*load.NoFilesError)):
7879
default:
7980
exitOnErr(cmd, inst.Err, false)

cmd/cue/cmd/testdata/script/fmt_err.txtar

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Fail when import path provided as argument is invalid.
2+
! exec cue fmt ./non-existing-file
3+
cmp stderr non-existing-file.golden
4+
15
# Ignore certain package loading errors in cue fmt.
26
cd issue644
37
exec cue fmt x.cue
@@ -17,6 +21,8 @@ cmp x.cue out/x_cue
1721
cmp y.cue out/y_cue
1822
cd ..
1923

24+
-- non-existing-file.golden --
25+
cannot find package "./non-existing-file"
2026
-- cue.mod/module.cue --
2127
module: "mod.test/x"
2228
-- emptypkg/.empty --

0 commit comments

Comments
 (0)