Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue/build: remove support for file lists
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mpvl committed Apr 11, 2021
1 parent f1d3d81 commit 660b090
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 108 deletions.
22 changes: 10 additions & 12 deletions cue/build/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,6 @@ type Instance struct {
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
Expand All @@ -123,6 +111,16 @@ type Instance struct {
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 == "" {
Expand Down
19 changes: 10 additions & 9 deletions cue/load/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,25 @@ func (e *NoFilesError) Msg() (string, []interface{}) { return e.Error(), nil }
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++
}
}

// 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 += ")"
Expand Down
95 changes: 10 additions & 85 deletions cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,50 +263,16 @@ func (l *loader) loadFunc() build.LoadFunc {
}
}

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)
normalizeFiles(p.InvalidFiles)
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))
Expand Down Expand Up @@ -340,12 +306,14 @@ func newFileProcessor(c *Config, p *build.Instance) *fileProcessor {
}

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
}
Expand All @@ -357,7 +325,7 @@ func (fp *fileProcessor) finalize(p *build.Instance) errors.Error {
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
}

Expand Down Expand Up @@ -388,7 +356,6 @@ func (fp *fileProcessor) add(pos token.Pos, root string, file *build.File, mode
// 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
}
Expand All @@ -399,11 +366,9 @@ func (fp *fileProcessor) add(pos token.Pos, root string, file *build.File, mode
}
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
}
Expand Down Expand Up @@ -440,7 +405,6 @@ func (fp *fileProcessor) add(pos token.Pos, root string, file *build.File, mode
case pkg != "_":

default:
p.IgnoredCUEFiles = append(p.IgnoredCUEFiles, fullPath)
p.IgnoredFiles = append(p.IgnoredFiles, file)
return false // don't mark as added
}
Expand All @@ -450,8 +414,7 @@ func (fp *fileProcessor) add(pos token.Pos, root string, file *build.File, mode
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
}
}
Expand All @@ -462,7 +425,6 @@ func (fp *fileProcessor) add(pos token.Pos, root string, file *build.File, mode
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
}
Expand Down Expand Up @@ -513,48 +475,23 @@ func (fp *fileProcessor) add(pos token.Pos, root string, file *build.File, mode
}
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)
Expand Down Expand Up @@ -653,18 +590,6 @@ func cleanImports(m map[string][]token.Pos) ([]string, map[string][]token.Pos) {
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 {
Expand Down
2 changes: 1 addition & 1 deletion cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ files:
},
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
Expand Down
2 changes: 1 addition & 1 deletion cue/load/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (l *loader) matchPackagesInFS(pattern, pkgName string) *match {
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
Expand Down

0 comments on commit 660b090

Please sign in to comment.