Skip to content

Commit

Permalink
cmd/cue: fail cue fmt if arg import path doesn't exist
Browse files Browse the repository at this point in the history
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 <noam.tzvi.dolovich@gmail.com>
Change-Id: I87ef60960b61924bf4c9493e5c492caa907ab294
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194124
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
NoamTD authored and mvdan committed May 1, 2024
1 parent f9c2de9 commit d9c5ae0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/cue/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func newFmtCmd(c *Command) *cobra.Command {
for _, inst := range builds {
if inst.Err != nil {
switch {
case errors.As(inst.Err, new(*load.PackageError)):
case errors.As(inst.Err, new(*load.PackageError)) && len(inst.BuildFiles) != 0:
// Ignore package errors if there are files to format.
case errors.As(inst.Err, new(*load.NoFilesError)):
default:
exitOnErr(cmd, inst.Err, false)
Expand Down
6 changes: 6 additions & 0 deletions cmd/cue/cmd/testdata/script/fmt_err.txtar
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Fail when import path provided as argument is invalid.
! exec cue fmt ./non-existing-file
cmp stderr non-existing-file.golden

# Ignore certain package loading errors in cue fmt.
cd issue644
exec cue fmt x.cue
Expand All @@ -17,6 +21,8 @@ cmp x.cue out/x_cue
cmp y.cue out/y_cue
cd ..

-- non-existing-file.golden --
cannot find package "./non-existing-file"
-- cue.mod/module.cue --
module: "mod.test/x"
-- emptypkg/.empty --
Expand Down

0 comments on commit d9c5ae0

Please sign in to comment.