cue: fix instance error handling

Change-Id: I58f26d6526ca2ff972caa3db97072e3dec113332
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7066
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/testdata/script/issue477.txt b/cmd/cue/cmd/testdata/script/issue477.txt
new file mode 100644
index 0000000..901b549
--- /dev/null
+++ b/cmd/cue/cmd/testdata/script/issue477.txt
@@ -0,0 +1,19 @@
+! cue cmd print
+
+cmp stderr expect-stderr
+
+-- x_tool.cue --
+package x
+
+import (
+    "strings"
+    "tool/cli"
+)
+
+command: print: cli.Print & {
+    text: "this is a test"
+}
+
+-- expect-stderr --
+imported and not used: "strings":
+    ./x_tool.cue:4:5
diff --git a/cue/instance.go b/cue/instance.go
index 7eb1958..493c2d4 100644
--- a/cue/instance.go
+++ b/cue/instance.go
@@ -259,10 +259,10 @@
 
 	i := newInstance(idx, p, v)
 	if rErr != nil {
-		i.setListOrError(err)
+		i.setListOrError(rErr)
 	}
 	if i.Err != nil {
-		i.setListOrError(err)
+		i.setListOrError(i.Err)
 	}
 
 	if err != nil {