cmd/cue/cmd: load import dependencies of tool files

Fixes #869
Fixes #871

Change-Id: I7c439d298d819bfc20c33f2e028a0216a57c4307
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9303
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go
index 1b51472..0e08f39 100644
--- a/cmd/cue/cmd/common.go
+++ b/cmd/cue/cmd/common.go
@@ -615,6 +615,15 @@
 		inst = cue.Merge(insts...)
 	}
 
+	r := internal.GetRuntime(inst).(*cue.Runtime)
+	for _, b := range binst {
+		for _, i := range b.Imports {
+			if _, err := r.Build(i); err != nil {
+				return nil, err
+			}
+		}
+	}
+
 	inst = inst.Build(ti)
 	return inst, inst.Err
 }
diff --git a/cmd/cue/cmd/testdata/script/cmd_deppkg.txt b/cmd/cue/cmd/testdata/script/cmd_deppkg.txt
new file mode 100644
index 0000000..7e817f8
--- /dev/null
+++ b/cmd/cue/cmd/testdata/script/cmd_deppkg.txt
@@ -0,0 +1,25 @@
+cue cmd foo
+cmp stdout cue_cmd_foo_both.golden
+
+-- cue.mod/module.cue --
+module: "example.org/test"
+-- sub/sub.cue --
+package sub
+
+User: "sub"
+-- x_tool.cue --
+package x
+
+import (
+ "tool/cli"
+
+ "example.org/test/sub"
+)
+
+command: foo: cli.Print & {
+ text: "test \(sub.User)"
+}
+-- cue_eval.golden --
+x: "sub"
+-- cue_cmd_foo_both.golden --
+test sub
\ No newline at end of file