cmd/cue/cmd: track use of indexing op for dependencies

This uses the new Reference api instead of the old References method.

Change-Id: Ia6c17245018580e58a56763c8d830e4c3e6adb24
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4001
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/custom.go b/cmd/cue/cmd/custom.go
index 4fb7d92..b8670a4 100644
--- a/cmd/cue/cmd/custom.go
+++ b/cmd/cue/cmd/custom.go
@@ -154,9 +154,9 @@
 
 	// Mark dependencies for unresolved nodes.
 	for _, t := range queue {
-		tasks.Lookup(t.name).Walk(func(v cue.Value) bool {
-			// if v.IsIncomplete() {
-			for _, r := range v.References() {
+		task := tasks.Lookup(t.name)
+		task.Walk(func(v cue.Value) bool {
+			for _, r := range appendReferences(nil, root, v) {
 				if dep, ok := index[keyForReference(r)]; ok {
 					v := root.Lookup(r...)
 					if v.IsIncomplete() && v.Kind() != cue.StructKind {
@@ -164,7 +164,6 @@
 					}
 				}
 			}
-			// }
 			return true
 		}, nil)
 	}
@@ -214,6 +213,28 @@
 	return g.Wait()
 }
 
+func appendReferences(a [][]string, root *cue.Instance, v cue.Value) [][]string {
+	inst, path := v.Reference()
+	if path != nil && inst == root {
+		a = append(a, path)
+		return a
+	}
+
+	switch op, args := v.Expr(); op {
+	case cue.NoOp:
+		v.Walk(nil, func(w cue.Value) {
+			if v != w {
+				a = appendReferences(a, root, w)
+			}
+		})
+	default:
+		for _, arg := range args {
+			a = appendReferences(a, root, arg)
+		}
+	}
+	return a
+}
+
 func isCyclic(tasks []*task) bool {
 	cc := cycleChecker{
 		visited: make([]bool, len(tasks)),
@@ -258,7 +279,7 @@
 	dep   map[*task]bool
 }
 
-var oldKinds = map[string]string{
+var legacyKinds = map[string]string{
 	"exec":       "tool/exec.Run",
 	"http":       "tool/http.Do",
 	"print":      "tool/cli.Print",
@@ -272,7 +293,7 @@
 	if err != nil {
 		return nil, err
 	}
-	if k, ok := oldKinds[kind]; ok {
+	if k, ok := legacyKinds[kind]; ok {
 		kind = k
 	}
 	rf := itask.Lookup(kind)
diff --git a/cmd/cue/cmd/testdata/script/cmd_print.txt b/cmd/cue/cmd/testdata/script/cmd_print.txt
index a42de26..1f032b3 100644
--- a/cmd/cue/cmd/testdata/script/cmd_print.txt
+++ b/cmd/cue/cmd/testdata/script/cmd_print.txt
@@ -10,6 +10,7 @@
 
 import (
 	"tool/exec"
+	"tool/cli"
 	"strings"
 )