cmd/cue/cmd: report instance-level errors

Issue #115.

Change-Id: I24a9fdaa4711886b66d2be0c386703df433b184b
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3341
Reviewed-by: roger peppe <rogpeppe@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/fmt.go b/cmd/cue/cmd/fmt.go
index c569f0f..8e6ee1a 100644
--- a/cmd/cue/cmd/fmt.go
+++ b/cmd/cue/cmd/fmt.go
@@ -32,6 +32,10 @@
 `,
 		RunE: mkRunE(c, func(cmd *Command, args []string) error {
 			for _, inst := range load.Instances(args, nil) {
+				if inst.Err != nil {
+					exitOnErr(cmd, inst.Err, false)
+					continue
+				}
 				all := []string{}
 				all = append(all, inst.CUEFiles...)
 				all = append(all, inst.ToolCUEFiles...)
diff --git a/cmd/cue/cmd/fmt_test.go b/cmd/cue/cmd/fmt_test.go
new file mode 100644
index 0000000..7ecefab
--- /dev/null
+++ b/cmd/cue/cmd/fmt_test.go
@@ -0,0 +1,22 @@
+// Copyright 2019 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package cmd
+
+import "testing"
+
+func TestFmt(t *testing.T) {
+	cmd := newFmtCmd(newRootCmd())
+	runCommand(t, cmd, "fmt")
+}
diff --git a/cmd/cue/cmd/testdata/fmt/error.cue b/cmd/cue/cmd/testdata/fmt/error.cue
new file mode 100644
index 0000000..60465d6
--- /dev/null
+++ b/cmd/cue/cmd/testdata/fmt/error.cue
@@ -0,0 +1,4 @@
+import a.b "foo"
+
+a: 2
+bb: 3
\ No newline at end of file
diff --git a/cmd/cue/cmd/testdata/fmt/fmt.out b/cmd/cue/cmd/testdata/fmt/fmt.out
new file mode 100644
index 0000000..231cb6d
--- /dev/null
+++ b/cmd/cue/cmd/testdata/fmt/fmt.out
@@ -0,0 +1,2 @@
+expected 'STRING', found '.':
+    ./testdata/fmt/error.cue:1:9