cmd/cue/cmd: require imports for tooling

Imports were incorrectly elided before.

Change-Id: I22c8adf9844affcc619714ac435ab85a588b996b
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4386
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/testdata/script/cmd_errcode.txt b/cmd/cue/cmd/testdata/script/cmd_errcode.txt
index 633f94b..9f791b4 100644
--- a/cmd/cue/cmd/testdata/script/cmd_errcode.txt
+++ b/cmd/cue/cmd/testdata/script/cmd_errcode.txt
@@ -9,6 +9,8 @@
 -- task_tool.cue --
 package home
 
+import "tool/exec"
+
 command: errcode: {
 	task: bad: exec.Run & {
 		kind:   "exec"
diff --git a/cmd/cue/cmd/testdata/script/cmd_import.txt b/cmd/cue/cmd/testdata/script/cmd_import.txt
new file mode 100644
index 0000000..d401652
--- /dev/null
+++ b/cmd/cue/cmd/testdata/script/cmd_import.txt
@@ -0,0 +1,19 @@
+! cue cmd pkg
+cmp stderr expect-stderr
+
+-- expect-stderr --
+reference "cli" not found
+-- task_tool.cue --
+package home
+
+// missing imports
+
+command: pkg: {
+	task: {
+		t2: cli.Print & {
+			text: "Hello world!"
+		}
+	}
+}
+
+-- cue.mod --
diff --git a/cmd/cue/cmd/testdata/script/cmd_ref.txt b/cmd/cue/cmd/testdata/script/cmd_ref.txt
index 5141b3f..456f992 100644
--- a/cmd/cue/cmd/testdata/script/cmd_ref.txt
+++ b/cmd/cue/cmd/testdata/script/cmd_ref.txt
@@ -9,6 +9,7 @@
 
 import (
 	"tool/cli"
+	"tool/exec"
 )
 
 command: ref: {
diff --git a/cue/ast.go b/cue/ast.go
index c2bb876..bfbaae6 100644
--- a/cue/ast.go
+++ b/cue/ast.go
@@ -40,7 +40,7 @@
 	// TODO: insert by converting to value first so that the trim command can
 	// also remove top-level fields.
 	// First process single file.
-	v := newVisitor(inst.index, inst.inst, inst.rootStruct, inst.scope)
+	v := newVisitor(inst.index, inst.inst, inst.rootStruct, inst.scope, false)
 	v.astState.astMap[f] = inst.rootStruct
 	// TODO: fix cmd/import to resolve references in the AST before
 	// inserting. For now, we accept errors that did not make it up to the tree.
@@ -80,6 +80,7 @@
 
 	litParser   *litParser
 	resolveRoot *structLit
+	allowAuto   bool // allow builtin packages without import
 
 	// make unique per level to avoid reuse of structs being an issue.
 	astMap map[ast.Node]scope
@@ -102,12 +103,12 @@
 	s.astMap[n] = v
 }
 
-func newVisitor(idx *index, inst *build.Instance, obj, resolveRoot *structLit) *astVisitor {
+func newVisitor(idx *index, inst *build.Instance, obj, resolveRoot *structLit, allowAuto bool) *astVisitor {
 	ctx := idx.newContext()
-	return newVisitorCtx(ctx, inst, obj, resolveRoot)
+	return newVisitorCtx(ctx, inst, obj, resolveRoot, allowAuto)
 }
 
-func newVisitorCtx(ctx *context, inst *build.Instance, obj, resolveRoot *structLit) *astVisitor {
+func newVisitorCtx(ctx *context, inst *build.Instance, obj, resolveRoot *structLit, allowAuto bool) *astVisitor {
 	v := &astVisitor{
 		object: obj,
 	}
@@ -117,6 +118,7 @@
 		inst:        inst,
 		litParser:   &litParser{ctx: ctx},
 		resolveRoot: resolveRoot,
+		allowAuto:   allowAuto,
 		astMap:      map[ast.Node]scope{},
 	}
 	return v
@@ -153,7 +155,7 @@
 					&nodeRef{baseValue: newExpr(n), node: r, label: label}, label}
 			}
 		}
-		if v.inSelector > 0 {
+		if v.inSelector > 0 && v.allowAuto {
 			if p := getBuiltinShorthandPkg(ctx, name); p != nil {
 				return &nodeRef{newExpr(n), p, label}
 			}
diff --git a/cue/builtin.go b/cue/builtin.go
index 3613b96..2d5beca 100644
--- a/cue/builtin.go
+++ b/cue/builtin.go
@@ -106,7 +106,7 @@
 	if err != nil {
 		panic(err)
 	}
-	v := newVisitor(ctx.index, nil, nil, nil)
+	v := newVisitor(ctx.index, nil, nil, nil, false)
 	value := v.walk(expr)
 	return value.evalPartial(ctx)
 }
diff --git a/cue/go.go b/cue/go.go
index 93a8caf..d2a2225 100644
--- a/cue/go.go
+++ b/cue/go.go
@@ -71,7 +71,7 @@
 		field := ctx.labelStr(field)
 		return ctx.mkErr(baseValue{}, "invalid tag %q for field %q: %v", tag, field, err)
 	}
-	v := newVisitor(ctx.index, nil, nil, obj)
+	v := newVisitor(ctx.index, nil, nil, obj, true)
 	return v.walk(expr)
 }
 
@@ -159,7 +159,7 @@
 	if err != nil {
 		panic(err) // cannot happen
 	}
-	v := newVisitor(ctx.index, nil, nil, nil)
+	v := newVisitor(ctx.index, nil, nil, nil, false)
 	return v.walk(expr).evalPartial(ctx)
 }
 
@@ -206,7 +206,7 @@
 		return makeNullable(&top{src.base()}, false).(evaluated)
 
 	case ast.Expr:
-		x := newVisitorCtx(ctx, nil, nil, nil)
+		x := newVisitorCtx(ctx, nil, nil, nil, false)
 		return ctx.manifest(x.walk(v))
 
 	case *big.Int:
diff --git a/cue/instance.go b/cue/instance.go
index 60ceffb..6852a71 100644
--- a/cue/instance.go
+++ b/cue/instance.go
@@ -142,7 +142,7 @@
 		return idx.mkErr(obj, "instance is not a struct")
 	}
 
-	v := newVisitor(idx, nil, nil, obj)
+	v := newVisitor(idx, nil, nil, obj, true)
 	return eval(idx, v.walk(expr))
 }
 
@@ -155,7 +155,7 @@
 	if !ok {
 		return ctx.mkErr(obj, "instance is not a struct")
 	}
-	v := newVisitor(ctx.index, inst.inst, nil, obj)
+	v := newVisitor(ctx.index, inst.inst, nil, obj, true)
 	return v.walk(expr).evalPartial(ctx)
 }
 
diff --git a/cue/parser/interface.go b/cue/parser/interface.go
index 302eb38..de1cf1f 100644
--- a/cue/parser/interface.go
+++ b/cue/parser/interface.go
@@ -179,7 +179,7 @@
 	f.Filename = filename
 	astutil.Resolve(f, pp.errf)
 
-	return
+	return f, pp.errors
 }
 
 // ParseExpr is a convenience function for parsing an expression.
@@ -223,7 +223,7 @@
 	}
 	astutil.ResolveExpr(e, p.errf)
 
-	return e, nil
+	return e, p.errors
 }
 
 // parseExprString is a convenience function for obtaining the AST of an