cmd/cue/cmd: improve error reporting of import

Change-Id: I8a063b7283ee67709774cdde90c54169a82e5e09
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2043
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go
index 4a03a17..e852780 100644
--- a/cmd/cue/cmd/common.go
+++ b/cmd/cue/cmd/common.go
@@ -25,6 +25,7 @@
 	"cuelang.org/go/cue/build"
 	"cuelang.org/go/cue/errors"
 	"cuelang.org/go/cue/load"
+	"cuelang.org/go/cue/parser"
 	"github.com/spf13/cobra"
 )
 
@@ -38,15 +39,19 @@
 var cwd = "////"
 
 // printHeader is a hacky and unprincipled way to sanatize the package path.
-func printHeader(w io.Writer, inst *cue.Instance) {
-	head := strings.Replace(inst.Dir, cwd, ".", 1)
+func printHeader(w io.Writer, dir string) {
+	head := strings.Replace(dir, cwd, ".", 1)
 	fmt.Fprintf(w, "--- %s\n", head)
 }
 
 func exitIfErr(cmd *cobra.Command, inst *cue.Instance, err error, fatal bool) {
+	exitOnErr(cmd, inst.Dir, err, fatal)
+}
+
+func exitOnErr(cmd *cobra.Command, file string, err error, fatal bool) {
 	if err != nil {
 		w := &bytes.Buffer{}
-		printHeader(w, inst)
+		printHeader(w, file)
 		errors.Print(w, err)
 
 		// TODO: do something more principled than this.
@@ -69,7 +74,7 @@
 
 var (
 	config = &load.Config{
-		Context: build.NewContext(),
+		Context: build.NewContext(build.ParseOptions(parser.ParseComments)),
 	}
 )
 
diff --git a/cmd/cue/cmd/import.go b/cmd/cue/cmd/import.go
index 05f016d..cf6dadb 100644
--- a/cmd/cue/cmd/import.go
+++ b/cmd/cue/cmd/import.go
@@ -17,7 +17,6 @@
 import (
 	"bytes"
 	"encoding/json"
-	"errors"
 	"fmt"
 	"io"
 	"io/ioutil"
@@ -32,6 +31,7 @@
 	"cuelang.org/go/cue"
 	"cuelang.org/go/cue/ast"
 	"cuelang.org/go/cue/encoding"
+	"cuelang.org/go/cue/errors"
 	"cuelang.org/go/cue/format"
 	"cuelang.org/go/cue/literal"
 	"cuelang.org/go/cue/load"
@@ -266,7 +266,7 @@
 
 	var group errgroup.Group
 
-	group.Go(func() error {
+	group.Go(func() (err error) {
 		if len(args) > 0 && len(filepath.Ext(args[0])) > len(".") {
 			for _, a := range args {
 				group.Go(func() error { return handleFile(cmd, *fPackage, a) })
@@ -311,13 +311,11 @@
 	})
 
 	err := group.Wait()
-	if err != nil {
-		return fmt.Errorf("Import failed: %v", err)
-	}
+	exitOnErr(cmd, "", err, true)
 	return nil
 }
 
-func handleFile(cmd *cobra.Command, pkg, filename string) error {
+func handleFile(cmd *cobra.Command, pkg, filename string) (err error) {
 	re, err := regexp.Compile(*name)
 	if err != nil {
 		return err
diff --git a/cmd/cue/cmd/vet.go b/cmd/cue/cmd/vet.go
index cfceecc..27cbeaa 100644
--- a/cmd/cue/cmd/vet.go
+++ b/cmd/cue/cmd/vet.go
@@ -64,7 +64,7 @@
 		}
 		err := inst.Value().Validate(opt...)
 		if *fVerbose || err != nil {
-			printHeader(w, inst)
+			printHeader(w, inst.Dir)
 		}
 		exitIfErr(cmd, inst, err, false)
 	}