cue/load: add tool and test files to BuildFiles if requested

This prepares for hoising the injection logic from
cmd/cue to this package. This, in turn, also prepares
for file-level build tag support.

The main change is that for the "special" reorganzation
of tool files in `cue/cmd` it now removes the tool
files, rather than manually addes them. This is weirder,
but coincides with our intention to get rid of this special
behavior.

Change-Id: I7b61966c709282c7c4b55de303cba8e5a87d2a87
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7061
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go
index bf0321e..a7a75ab 100644
--- a/cmd/cue/cmd/common.go
+++ b/cmd/cue/cmd/common.go
@@ -579,12 +579,19 @@
 
 	ti := binst[0].Context().NewInstance(binst[0].Root, nil)
 	for _, inst := range binst {
-		for _, f := range inst.ToolCUEFiles {
-			if file := inst.Abs(f); !included[file] {
-				_ = ti.AddFile(file, nil)
-				included[file] = true
+		k := 0
+		for _, f := range inst.Files {
+			if strings.HasSuffix(f.Filename, "_tool.cue") {
+				if !included[f.Filename] {
+					_ = ti.AddSyntax(f)
+					included[f.Filename] = true
+				}
+				continue
 			}
+			inst.Files[k] = f
+			k++
 		}
+		inst.Files = inst.Files[:k]
 	}
 	decorateInstances(cmd, tags, append(binst, ti))
 
diff --git a/cmd/cue/cmd/fix.go b/cmd/cue/cmd/fix.go
index 5f5e4d3..30f4be2 100644
--- a/cmd/cue/cmd/fix.go
+++ b/cmd/cue/cmd/fix.go
@@ -81,17 +81,6 @@
 		Tools: true,
 	})
 
-	for _, i := range instances {
-		a := append(i.ToolCUEFiles, i.TestCUEFiles...)
-		for _, f := range a {
-			file := i.Abs(f)
-			_ = i.AddFile(file, nil)
-		}
-		if i.Err != nil {
-			return i.Err
-		}
-	}
-
 	errs := fix.Instances(instances)
 
 	if errs != nil && flagForce.Bool(cmd) {
diff --git a/cmd/cue/cmd/fmt.go b/cmd/cue/cmd/fmt.go
index def1d97..f2990cc 100644
--- a/cmd/cue/cmd/fmt.go
+++ b/cmd/cue/cmd/fmt.go
@@ -52,15 +52,7 @@
 					exitOnErr(cmd, inst.Err, false)
 					continue
 				}
-				all := []*build.File{}
-				all = append(all, inst.BuildFiles...)
-				for _, name := range append(inst.ToolCUEFiles, inst.TestCUEFiles...) {
-					all = append(all, &build.File{
-						Filename: name,
-						Encoding: build.CUE,
-					})
-				}
-				for _, file := range all {
+				for _, file := range inst.BuildFiles {
 					files := []*ast.File{}
 					d := encoding.NewDecoder(file, &cfg)
 					defer d.Close()
diff --git a/cue/load/import.go b/cue/load/import.go
index 009041b..6ee6084 100644
--- a/cue/load/import.go
+++ b/cue/load/import.go
@@ -433,10 +433,18 @@
 	switch {
 	case isTest:
 		p.TestCUEFiles = append(p.TestCUEFiles, fullPath)
-		// TODO: what is the BuildFiles equivalent?
+		if fp.c.loader.cfg.Tests {
+			p.BuildFiles = append(p.BuildFiles, file)
+		} else {
+			p.IgnoredFiles = append(p.IgnoredFiles, file)
+		}
 	case isTool:
 		p.ToolCUEFiles = append(p.ToolCUEFiles, fullPath)
-		// TODO: what is the BuildFiles equivalent?
+		if fp.c.loader.cfg.Tools {
+			p.BuildFiles = append(p.BuildFiles, file)
+		} else {
+			p.IgnoredFiles = append(p.IgnoredFiles, file)
+		}
 	default:
 		p.CUEFiles = append(p.CUEFiles, fullPath)
 		p.BuildFiles = append(p.BuildFiles, file)
diff --git a/cue/load/loader_test.go b/cue/load/loader_test.go
index f3c4ec0..073b45c 100644
--- a/cue/load/loader_test.go
+++ b/cue/load/loader_test.go
@@ -238,7 +238,9 @@
 module: example.org/test
 root:   $CWD/testdata
 dir:    $CWD/testdata/toolonly
-display:./toolonly`,
+display:./toolonly
+files:
+	$CWD/testdata/toolonly/foo_tool.cue`,
 	}, {
 		cfg: &Config{
 			Dir: testdataDir,
diff --git a/cue/load/testdata/toolonly/foo_tool.cue b/cue/load/testdata/toolonly/foo_tool.cue
index 6441366..4a782e7 100644
--- a/cue/load/testdata/toolonly/foo_tool.cue
+++ b/cue/load/testdata/toolonly/foo_tool.cue
@@ -2,7 +2,7 @@
 
 import "tool/cli"
 
-command foo task: {
+command: foo: task: {
 	foo: cli.Print & {
 		text: "foo"
 	}