cue/load: fix bug in loader

Change-Id: I31cf9a8e8a2a5108acd8974fae10444caa5efcf4
diff --git a/cmd/cue/cmd/testdata/tasks/base_tool.cue b/cmd/cue/cmd/testdata/tasks/base_tool.cue
new file mode 100644
index 0000000..1787a94
--- /dev/null
+++ b/cmd/cue/cmd/testdata/tasks/base_tool.cue
@@ -0,0 +1,15 @@
+package home
+
+// deliberately put in another file to test resolving top-level identifiers
+// in different files.
+runBase: {
+	task echo: {
+		kind:   "exec"
+		stdout: string
+	}
+
+	task display: {
+		kind: "print"
+		text: task.echo.stdout
+	}
+}
diff --git a/cmd/cue/cmd/testdata/tasks/task_tool.cue b/cmd/cue/cmd/testdata/tasks/task_tool.cue
index ce37bf1..85468c1 100644
--- a/cmd/cue/cmd/testdata/tasks/task_tool.cue
+++ b/cmd/cue/cmd/testdata/tasks/task_tool.cue
@@ -42,15 +42,3 @@
 		text: task.http.response.body
 	}
 }
-
-runBase: {
-	task echo: {
-		kind:   "exec"
-		stdout: string
-	}
-
-	task display: {
-		kind: "print"
-		text: task.echo.stdout
-	}
-}
diff --git a/cue/ast.go b/cue/ast.go
index 37b92dc..43a3616 100644
--- a/cue/ast.go
+++ b/cue/ast.go
@@ -220,6 +220,20 @@
 			if !ok {
 				return v.error(x, "invalid field name: %v", x)
 			}
+
+			// TODO: if the clauses do not contain a guard, we know that this
+			// field will always be added and we can move the comprehension one
+			// level down. This, in turn, has the advantage that it is more
+			// likely that the cross-reference limitation for field
+			// comprehensions is not violated. To ensure compatibility between
+			// implementations, though, we should relax the spec as well.
+			// The cross-reference rule is simple and this relaxation seems a
+			// bit more complex.
+
+			// TODO: for now we can also consider making this an error if
+			// the list of clauses does not contain if and make a suggestion
+			// to rewrite it.
+
 			if name != "" {
 				yielder.key = &stringLit{newNode(x), name}
 				yielder.value = v.walk(field.Value)
diff --git a/cue/instance.go b/cue/instance.go
index 43db201..87481bf 100644
--- a/cue/instance.go
+++ b/cue/instance.go
@@ -137,6 +137,7 @@
 //
 // Errors regarding conflicts are included in the result, but not reported, so
 // that these will only surface during manifestation. This allows
+// non-conflicting parts to be used.
 func Merge(inst ...*Instance) *Instance {
 	switch len(inst) {
 	case 0:
diff --git a/cue/load/import.go b/cue/load/import.go
index 79813a5..0d2d35c 100644
--- a/cue/load/import.go
+++ b/cue/load/import.go
@@ -401,7 +401,7 @@
 	case isTest:
 		p.TestCUEFiles = append(p.TestCUEFiles, fullPath)
 	case isTool:
-		p.ToolCUEFiles = append(p.TestCUEFiles, fullPath)
+		p.ToolCUEFiles = append(p.ToolCUEFiles, fullPath)
 	default:
 		p.CUEFiles = append(p.CUEFiles, fullPath)
 	}