cue/build: remove support for file lists

This is a breaking change! We do not have "go fix"
support because:
1) it is anticipated that these weren't really used by users
2) they are easy to fix by hand, yet somewhat tricky to
do with Go fix.

Users are expected to use the lists of type []*File.

Tool or test files can be found in IgnoredFiles, if the
Test or Tool option were not used.

Use Instance.RelPath to get the relative path of a file.

Change-Id: Ib430fce26562c584129ac9183ebf72ccd1466054
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9361
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
diff --git a/cue/build/instance.go b/cue/build/instance.go
index 0f95b92..d4ef4ad 100644
--- a/cue/build/instance.go
+++ b/cue/build/instance.go
@@ -102,18 +102,6 @@
 	Standard bool // Is a builtin package
 	User     bool // True if package was created from individual files.
 
-	// Deprecated: use BuildFiles
-	CUEFiles []string // .cue source files
-	// Deprecated: use BuildFiles and OrphanedFiles
-	DataFiles []string // recognized data files (.json, .yaml, etc.)
-
-	// The intent is to also deprecate the following fields in favor of
-	// IgnoredFiles and UnknownFiles.
-	TestCUEFiles    []string // .cue test files (_test.cue)
-	ToolCUEFiles    []string // .cue tool files (_tool.cue)
-	IgnoredCUEFiles []string // .cue source files ignored for this build
-	InvalidCUEFiles []string // .cue source files with detected problems (parse error, wrong package name, and so on)
-
 	// Dependencies
 	ImportPaths []string
 	ImportPos   map[string][]token.Pos // line information for Imports
@@ -123,6 +111,16 @@
 	Match      []string
 }
 
+// RelPath reports the path of f relative to the root of the instance's module
+// directory. The full path is returned if a relative path could not be found.
+func (inst *Instance) RelPath(f *File) string {
+	p, err := filepath.Rel(inst.Root, f.Filename)
+	if err != nil {
+		return f.Filename
+	}
+	return p
+}
+
 // ID returns the package ID unique for this module.
 func (inst *Instance) ID() string {
 	if inst.PkgName == "" {
diff --git a/cue/load/errors.go b/cue/load/errors.go
index a431282..0b74316 100644
--- a/cue/load/errors.go
+++ b/cue/load/errors.go
@@ -105,8 +105,8 @@
 func (e *NoFilesError) Error() string {
 	// Count files beginning with _, which we will pretend don't exist at all.
 	dummy := 0
-	for _, name := range e.Package.IgnoredCUEFiles {
-		if strings.HasPrefix(name, "_") {
+	for _, f := range e.Package.IgnoredFiles {
+		if strings.HasPrefix(filepath.Base(f.Filename), "_") {
 			dummy++
 		}
 	}
@@ -114,15 +114,16 @@
 	// path := shortPath(e.Package.Root, e.Package.Dir)
 	path := e.Package.DisplayPath
 
-	if len(e.Package.IgnoredCUEFiles) > dummy {
+	if len(e.Package.IgnoredFiles) > dummy {
 		// CUE files exist, but they were ignored due to build constraints.
 		msg := "build constraints exclude all CUE files in " + path + " (ignored: "
-		files := e.Package.IgnoredCUEFiles
-		if len(files) > 4 {
-			files = append(files[:4], "...")
-		}
-		for i, f := range files {
-			files[i] = filepath.ToSlash(f)
+		var files []string
+		for i, f := range e.Package.IgnoredFiles {
+			if i == 4 {
+				files = append(files[:4], "...")
+				break
+			}
+			files = append(files, filepath.ToSlash(e.Package.RelPath(f)))
 		}
 		msg += strings.Join(files, ", ")
 		msg += ")"
diff --git a/cue/load/import.go b/cue/load/import.go
index 9f6e9ad..657bcad 100644
--- a/cue/load/import.go
+++ b/cue/load/import.go
@@ -263,28 +263,9 @@
 	}
 }
 
-func normPrefix(root, path string, isLocal bool) string {
-	root = filepath.Clean(root)
-	prefix := ""
-	if isLocal {
-		prefix = "." + string(filepath.Separator)
-	}
-	if !strings.HasSuffix(root, string(filepath.Separator)) &&
-		strings.HasPrefix(path, root) {
-		path = prefix + path[len(root)+1:]
-	}
-	return path
-}
-
 func rewriteFiles(p *build.Instance, root string, isLocal bool) {
 	p.Root = root
 
-	normalizeFilenames(root, p.CUEFiles, isLocal)
-	normalizeFilenames(root, p.TestCUEFiles, isLocal)
-	normalizeFilenames(root, p.ToolCUEFiles, isLocal)
-	normalizeFilenames(root, p.IgnoredCUEFiles, isLocal)
-	normalizeFilenames(root, p.InvalidCUEFiles, isLocal)
-
 	normalizeFiles(p.BuildFiles)
 	normalizeFiles(p.IgnoredFiles)
 	normalizeFiles(p.OrphanedFiles)
@@ -292,21 +273,6 @@
 	normalizeFiles(p.UnknownFiles)
 }
 
-func normalizeFilenames(root string, a []string, isLocal bool) {
-	for i, path := range a {
-		if strings.HasPrefix(path, root) {
-			a[i] = normPrefix(root, path, isLocal)
-		}
-	}
-	sortParentsFirst(a)
-}
-
-func sortParentsFirst(s []string) {
-	sort.Slice(s, func(i, j int) bool {
-		return len(filepath.Dir(s[i])) < len(filepath.Dir(s[j]))
-	})
-}
-
 func normalizeFiles(a []*build.File) {
 	sort.Slice(a, func(i, j int) bool {
 		return len(filepath.Dir(a[i].Filename)) < len(filepath.Dir(a[j].Filename))
@@ -340,12 +306,14 @@
 }
 
 func countCUEFiles(c *Config, p *build.Instance) int {
-	count := len(p.CUEFiles)
-	if c.Tools {
-		count += len(p.ToolCUEFiles)
-	}
-	if c.Tests {
-		count += len(p.TestCUEFiles)
+	count := len(p.BuildFiles)
+	for _, f := range p.IgnoredFiles {
+		if c.Tools && strings.HasSuffix(f.Filename, "_tool.cue") {
+			count++
+		}
+		if c.Tests && strings.HasSuffix(f.Filename, "_test.cue") {
+			count++
+		}
 	}
 	return count
 }
@@ -357,7 +325,7 @@
 	if countCUEFiles(fp.c, p) == 0 &&
 		!fp.c.DataFiles &&
 		(p.PkgName != "_" || !fp.allPackages) {
-		fp.err = errors.Append(fp.err, &NoFilesError{Package: p, ignored: len(p.IgnoredCUEFiles) > 0})
+		fp.err = errors.Append(fp.err, &NoFilesError{Package: p, ignored: len(p.IgnoredFiles) > 0})
 		return fp.err
 	}
 
@@ -388,7 +356,6 @@
 	// badFile := func(p *build.Instance, err errors.Error) bool {
 	badFile := func(err errors.Error) bool {
 		fp.err = errors.Append(fp.err, err)
-		p.InvalidCUEFiles = append(p.InvalidCUEFiles, fullPath)
 		p.InvalidFiles = append(p.InvalidFiles, file)
 		return true
 	}
@@ -399,11 +366,9 @@
 	}
 	if !match {
 		if file.Encoding == build.CUE && file.Interpretation == "" {
-			p.IgnoredCUEFiles = append(p.IgnoredCUEFiles, fullPath)
 			p.IgnoredFiles = append(p.IgnoredFiles, file)
 		} else {
 			p.OrphanedFiles = append(p.OrphanedFiles, file)
-			p.DataFiles = append(p.DataFiles, fullPath)
 		}
 		return false // don't mark as added
 	}
@@ -440,7 +405,6 @@
 	case pkg != "_":
 
 	default:
-		p.IgnoredCUEFiles = append(p.IgnoredCUEFiles, fullPath)
 		p.IgnoredFiles = append(p.IgnoredFiles, file)
 		return false // don't mark as added
 	}
@@ -450,8 +414,7 @@
 			if err != nil {
 				fp.err = errors.Append(fp.err, err)
 			}
-			p.IgnoredCUEFiles = append(p.InvalidCUEFiles, fullPath)
-			p.IgnoredFiles = append(p.InvalidFiles, file)
+			p.IgnoredFiles = append(p.IgnoredFiles, file)
 			return false
 		}
 	}
@@ -462,7 +425,6 @@
 			fp.firstFile = base
 		} else if pkg != p.PkgName {
 			if fp.ignoreOther {
-				p.IgnoredCUEFiles = append(p.IgnoredCUEFiles, fullPath)
 				p.IgnoredFiles = append(p.IgnoredFiles, file)
 				return false
 			}
@@ -513,48 +475,23 @@
 	}
 	switch {
 	case isTest:
-		p.TestCUEFiles = append(p.TestCUEFiles, fullPath)
 		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)
 		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)
 	}
 	return true
 }
 
-func nameExt(name string) string {
-	i := strings.LastIndex(name, ".")
-	if i < 0 {
-		return ""
-	}
-	return name[i:]
-}
-
-// hasCUEFiles reports whether dir contains any files with names ending in .go.
-// For a vendor check we must exclude directories that contain no .go files.
-// Otherwise it is not possible to vendor just a/b/c and still import the
-// non-vendored a/b. See golang.org/issue/13832.
-func hasCUEFiles(ctxt *fileSystem, dir string) bool {
-	ents, _ := ctxt.readDir(dir)
-	for _, ent := range ents {
-		if !ent.IsDir() && strings.HasSuffix(ent.Name(), cueSuffix) {
-			return true
-		}
-	}
-	return false
-}
-
 func findimportComment(data []byte) (s string, line int) {
 	// expect keyword package
 	word, data := parseWord(data)
@@ -653,18 +590,6 @@
 	return all, m
 }
 
-// // Import is shorthand for Default.Import.
-// func Import(path, srcDir string, mode ImportMode) (*Package, error) {
-// 	return Default.Import(path, srcDir, mode)
-// }
-
-// // ImportDir is shorthand for Default.ImportDir.
-// func ImportDir(dir string, mode ImportMode) (*Package, error) {
-// 	return Default.ImportDir(dir, mode)
-// }
-
-var slashslash = []byte("//")
-
 // isLocalImport reports whether the import path is
 // a local import path, like ".", "..", "./foo", or "../foo".
 func isLocalImport(path string) bool {
diff --git a/cue/load/loader_test.go b/cue/load/loader_test.go
index 17177fe..43b9be2 100644
--- a/cue/load/loader_test.go
+++ b/cue/load/loader_test.go
@@ -247,7 +247,7 @@
 		},
 		args: args("./toolonly"),
 		want: `
-err:    build constraints exclude all CUE files in ./toolonly (ignored: anon.cue, test.cue)
+err:    build constraints exclude all CUE files in ./toolonly (ignored: anon.cue, test.cue, toolonly/foo_tool.cue)
 path:   example.org/test/toolonly:foo
 module: example.org/test
 root:   $CWD/testdata
diff --git a/cue/load/search.go b/cue/load/search.go
index a6443d9..04a98b2 100644
--- a/cue/load/search.go
+++ b/cue/load/search.go
@@ -207,7 +207,7 @@
 		inst := c.newRelInstance(token.NoPos, dir, pkgName)
 		pkgs := l.importPkg(token.NoPos, inst)
 		for _, p := range pkgs {
-			if err := p.Err; err != nil && (p == nil || len(p.InvalidCUEFiles) == 0) {
+			if err := p.Err; err != nil && (p == nil || len(p.InvalidFiles) == 0) {
 				switch err.(type) {
 				case nil:
 					break