Skip to content

Commit e888508

Browse files
committed
cue/load: prepare for files package refactor
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 <[email protected]> Change-Id: If6c342757e97a3752edb3fefea64ad4bdfdbe23f Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194761 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 443106f commit e888508

File tree

5 files changed

+20
-24
lines changed

5 files changed

+20
-24
lines changed

cue/load/config.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,6 @@ type Config struct {
243243
// a package.
244244
Tools bool
245245

246-
// filesMode indicates that files are specified
247-
// explicitly on the command line.
248-
filesMode bool
249-
250246
// If DataFiles is set, the loader includes entries for directories that
251247
// have no CUE files, but have recognized data files that could be converted
252248
// to CUE.

cue/load/instances.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func Instances(args []string, c *Config) []*build.Instance {
6262
}
6363
pkgArgs := args[:i]
6464
otherArgs := args[i:]
65+
otherFiles, err := filetypes.ParseArgs(otherArgs)
66+
if err != nil {
67+
return []*build.Instance{c.newErrInstance(err)}
68+
}
6569

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

98-
if len(otherArgs) > 0 {
99-
files, err := filetypes.ParseArgs(otherArgs)
100-
if err != nil {
101-
return []*build.Instance{c.newErrInstance(err)}
102-
}
103-
a = append(a, l.cueFilesPackage(files))
102+
if len(otherFiles) > 0 {
103+
a = append(a, l.cueFilesPackage(otherFiles))
104104
}
105105

106106
for _, p := range a {

cue/load/loader.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,39 +94,38 @@ func (l *loader) errPkgf(importPos []token.Pos, format string, args ...interface
9494
// cueFilesPackage creates a package for building a collection of CUE files
9595
// (typically named on the command line).
9696
func (l *loader) cueFilesPackage(files []*build.File) *build.Instance {
97-
cfg := l.cfg
98-
cfg.filesMode = true
97+
9998
// ModInit() // TODO: support modules
100-
pkg := l.cfg.Context.NewInstance(cfg.Dir, l.loadFunc)
99+
pkg := l.cfg.Context.NewInstance(l.cfg.Dir, l.loadFunc)
101100

102101
for _, bf := range files {
103102
f := bf.Filename
104103
if f == "-" {
105104
continue
106105
}
107106
if !filepath.IsAbs(f) {
108-
f = filepath.Join(cfg.Dir, f)
107+
f = filepath.Join(l.cfg.Dir, f)
109108
}
110-
fi, err := cfg.fileSystem.stat(f)
109+
fi, err := l.cfg.fileSystem.stat(f)
111110
if err != nil {
112-
return cfg.newErrInstance(errors.Wrapf(err, token.NoPos, "could not find file %v", f))
111+
return l.cfg.newErrInstance(errors.Wrapf(err, token.NoPos, "could not find file %v", f))
113112
}
114113
if fi.IsDir() {
115-
return cfg.newErrInstance(errors.Newf(token.NoPos, "file is a directory %v", f))
114+
return l.cfg.newErrInstance(errors.Newf(token.NoPos, "file is a directory %v", f))
116115
}
117116
}
118117

119-
fp := newFileProcessor(cfg, pkg, l.tagger)
118+
fp := newFileProcessor(l.cfg, pkg, l.tagger)
120119
if l.cfg.Package == "*" {
121120
fp.allPackages = true
122121
pkg.PkgName = "_"
123122
}
124123
for _, file := range files {
125-
fp.add(cfg.Dir, file, allowAnonymous)
124+
fp.add(l.cfg.Dir, file, allowAnonymous|allowExcludedFiles)
126125
}
127126

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

144143
pkg.User = true
145-
l.addFiles(cfg.Dir, pkg)
144+
l.addFiles(l.cfg.Dir, pkg)
146145

147146
l.stk.Push("user")
148147
_ = pkg.Complete()

cue/load/loader_common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const (
4545
importComment importMode = 1 << iota
4646

4747
allowAnonymous
48+
allowExcludedFiles
4849
)
4950

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

187-
match, data, err := matchFile(fp.c, file, true, fp.allTags)
188+
match, data, err := matchFile(fp.c, file, true, fp.allTags, mode)
188189
switch {
189190
case match:
190191

cue/load/match.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (e excludeError) Is(err error) bool { return err == errExclude }
4949
// considers text until the first non-comment.
5050
// If allTags is non-nil, matchFile records any encountered build tag
5151
// by setting allTags[tag] = true.
52-
func matchFile(cfg *Config, file *build.File, returnImports bool, allTags map[string]bool) (match bool, data []byte, err errors.Error) {
52+
func matchFile(cfg *Config, file *build.File, returnImports bool, allTags map[string]bool, mode importMode) (match bool, data []byte, err errors.Error) {
5353
if fi := cfg.fileSystem.getOverlay(file.Filename); fi != nil {
5454
if fi.file != nil {
5555
file.Source = fi.file
@@ -73,7 +73,7 @@ func matchFile(cfg *Config, file *build.File, returnImports bool, allTags map[st
7373
}
7474

7575
name := filepath.Base(file.Filename)
76-
if !cfg.filesMode {
76+
if (mode & allowExcludedFiles) == 0 {
7777
for _, prefix := range []string{".", "_"} {
7878
if strings.HasPrefix(name, prefix) {
7979
return false, nil, &excludeError{

0 commit comments

Comments
 (0)