Skip to content

Commit

Permalink
cue/load: prepare for files package refactor
Browse files Browse the repository at this point in the history
Currently when files are specified on the command line,
they're considered after all packages have been evaluated
with respect to modules. We need to change that so that
we consider imports from files before creating the loader,
which requires factoring the `cueFilesPackage` method
out from `loader`.

This change makes some preparations for that by:
- parsing file arguments before invoking `loadPackages`
- passing the `filesMode` boolean as an argument rather
than indirectly in `Config`, where it doesn't really feel
like it belongs anyway, as `Config` isn't really the right
place for values that change over time.

For #3144
For #3147

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: If6c342757e97a3752edb3fefea64ad4bdfdbe23f
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194761
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
rogpeppe committed May 15, 2024
1 parent 443106f commit e888508
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
4 changes: 0 additions & 4 deletions cue/load/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@ type Config struct {
// a package.
Tools bool

// filesMode indicates that files are specified
// explicitly on the command line.
filesMode bool

// If DataFiles is set, the loader includes entries for directories that
// have no CUE files, but have recognized data files that could be converted
// to CUE.
Expand Down
12 changes: 6 additions & 6 deletions cue/load/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func Instances(args []string, c *Config) []*build.Instance {
}
pkgArgs := args[:i]
otherArgs := args[i:]
otherFiles, err := filetypes.ParseArgs(otherArgs)
if err != nil {
return []*build.Instance{c.newErrInstance(err)}
}

// Pass all arguments that look like packages to loadPackages
// so that they'll be available when looking up the packages
Expand Down Expand Up @@ -95,12 +99,8 @@ func Instances(args []string, c *Config) []*build.Instance {
}
}

if len(otherArgs) > 0 {
files, err := filetypes.ParseArgs(otherArgs)
if err != nil {
return []*build.Instance{c.newErrInstance(err)}
}
a = append(a, l.cueFilesPackage(files))
if len(otherFiles) > 0 {
a = append(a, l.cueFilesPackage(otherFiles))
}

for _, p := range a {
Expand Down
21 changes: 10 additions & 11 deletions cue/load/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,39 +94,38 @@ func (l *loader) errPkgf(importPos []token.Pos, format string, args ...interface
// cueFilesPackage creates a package for building a collection of CUE files
// (typically named on the command line).
func (l *loader) cueFilesPackage(files []*build.File) *build.Instance {
cfg := l.cfg
cfg.filesMode = true

// ModInit() // TODO: support modules
pkg := l.cfg.Context.NewInstance(cfg.Dir, l.loadFunc)
pkg := l.cfg.Context.NewInstance(l.cfg.Dir, l.loadFunc)

for _, bf := range files {
f := bf.Filename
if f == "-" {
continue
}
if !filepath.IsAbs(f) {
f = filepath.Join(cfg.Dir, f)
f = filepath.Join(l.cfg.Dir, f)
}
fi, err := cfg.fileSystem.stat(f)
fi, err := l.cfg.fileSystem.stat(f)
if err != nil {
return cfg.newErrInstance(errors.Wrapf(err, token.NoPos, "could not find file %v", f))
return l.cfg.newErrInstance(errors.Wrapf(err, token.NoPos, "could not find file %v", f))
}
if fi.IsDir() {
return cfg.newErrInstance(errors.Newf(token.NoPos, "file is a directory %v", f))
return l.cfg.newErrInstance(errors.Newf(token.NoPos, "file is a directory %v", f))
}
}

fp := newFileProcessor(cfg, pkg, l.tagger)
fp := newFileProcessor(l.cfg, pkg, l.tagger)
if l.cfg.Package == "*" {
fp.allPackages = true
pkg.PkgName = "_"
}
for _, file := range files {
fp.add(cfg.Dir, file, allowAnonymous)
fp.add(l.cfg.Dir, file, allowAnonymous|allowExcludedFiles)
}

// TODO: ModImportFromFiles(files)
pkg.Dir = cfg.Dir
pkg.Dir = l.cfg.Dir
rewriteFiles(pkg, pkg.Dir, true)
for _, err := range errors.Errors(fp.finalize(pkg)) { // ImportDir(&ctxt, dir, 0)
var x *NoFilesError
Expand All @@ -142,7 +141,7 @@ func (l *loader) cueFilesPackage(files []*build.File) *build.Instance {
// }

pkg.User = true
l.addFiles(cfg.Dir, pkg)
l.addFiles(l.cfg.Dir, pkg)

l.stk.Push("user")
_ = pkg.Complete()
Expand Down
3 changes: 2 additions & 1 deletion cue/load/loader_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
importComment importMode = 1 << iota

allowAnonymous
allowExcludedFiles
)

func rewriteFiles(p *build.Instance, root string, isLocal bool) {
Expand Down Expand Up @@ -184,7 +185,7 @@ func (fp *fileProcessor) add(root string, file *build.File, mode importMode) (ad
return true
}

match, data, err := matchFile(fp.c, file, true, fp.allTags)
match, data, err := matchFile(fp.c, file, true, fp.allTags, mode)
switch {
case match:

Expand Down
4 changes: 2 additions & 2 deletions cue/load/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (e excludeError) Is(err error) bool { return err == errExclude }
// considers text until the first non-comment.
// If allTags is non-nil, matchFile records any encountered build tag
// by setting allTags[tag] = true.
func matchFile(cfg *Config, file *build.File, returnImports bool, allTags map[string]bool) (match bool, data []byte, err errors.Error) {
func matchFile(cfg *Config, file *build.File, returnImports bool, allTags map[string]bool, mode importMode) (match bool, data []byte, err errors.Error) {
if fi := cfg.fileSystem.getOverlay(file.Filename); fi != nil {
if fi.file != nil {
file.Source = fi.file
Expand All @@ -73,7 +73,7 @@ func matchFile(cfg *Config, file *build.File, returnImports bool, allTags map[st
}

name := filepath.Base(file.Filename)
if !cfg.filesMode {
if (mode & allowExcludedFiles) == 0 {
for _, prefix := range []string{".", "_"} {
if strings.HasPrefix(name, prefix) {
return false, nil, &excludeError{
Expand Down

0 comments on commit e888508

Please sign in to comment.