this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <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>

+20 -24
-4
cue/load/config.go
··· 243 243 // a package. 244 244 Tools bool 245 245 246 - // filesMode indicates that files are specified 247 - // explicitly on the command line. 248 - filesMode bool 249 - 250 246 // If DataFiles is set, the loader includes entries for directories that 251 247 // have no CUE files, but have recognized data files that could be converted 252 248 // to CUE.
+6 -6
cue/load/instances.go
··· 62 62 } 63 63 pkgArgs := args[:i] 64 64 otherArgs := args[i:] 65 + otherFiles, err := filetypes.ParseArgs(otherArgs) 66 + if err != nil { 67 + return []*build.Instance{c.newErrInstance(err)} 68 + } 65 69 66 70 // Pass all arguments that look like packages to loadPackages 67 71 // so that they'll be available when looking up the packages ··· 95 99 } 96 100 } 97 101 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)) 104 104 } 105 105 106 106 for _, p := range a {
+10 -11
cue/load/loader.go
··· 94 94 // cueFilesPackage creates a package for building a collection of CUE files 95 95 // (typically named on the command line). 96 96 func (l *loader) cueFilesPackage(files []*build.File) *build.Instance { 97 - cfg := l.cfg 98 - cfg.filesMode = true 97 + 99 98 // 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) 101 100 102 101 for _, bf := range files { 103 102 f := bf.Filename ··· 105 104 continue 106 105 } 107 106 if !filepath.IsAbs(f) { 108 - f = filepath.Join(cfg.Dir, f) 107 + f = filepath.Join(l.cfg.Dir, f) 109 108 } 110 - fi, err := cfg.fileSystem.stat(f) 109 + fi, err := l.cfg.fileSystem.stat(f) 111 110 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)) 113 112 } 114 113 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)) 116 115 } 117 116 } 118 117 119 - fp := newFileProcessor(cfg, pkg, l.tagger) 118 + fp := newFileProcessor(l.cfg, pkg, l.tagger) 120 119 if l.cfg.Package == "*" { 121 120 fp.allPackages = true 122 121 pkg.PkgName = "_" 123 122 } 124 123 for _, file := range files { 125 - fp.add(cfg.Dir, file, allowAnonymous) 124 + fp.add(l.cfg.Dir, file, allowAnonymous|allowExcludedFiles) 126 125 } 127 126 128 127 // TODO: ModImportFromFiles(files) 129 - pkg.Dir = cfg.Dir 128 + pkg.Dir = l.cfg.Dir 130 129 rewriteFiles(pkg, pkg.Dir, true) 131 130 for _, err := range errors.Errors(fp.finalize(pkg)) { // ImportDir(&ctxt, dir, 0) 132 131 var x *NoFilesError ··· 142 141 // } 143 142 144 143 pkg.User = true 145 - l.addFiles(cfg.Dir, pkg) 144 + l.addFiles(l.cfg.Dir, pkg) 146 145 147 146 l.stk.Push("user") 148 147 _ = pkg.Complete()
+2 -1
cue/load/loader_common.go
··· 45 45 importComment importMode = 1 << iota 46 46 47 47 allowAnonymous 48 + allowExcludedFiles 48 49 ) 49 50 50 51 func rewriteFiles(p *build.Instance, root string, isLocal bool) { ··· 184 185 return true 185 186 } 186 187 187 - match, data, err := matchFile(fp.c, file, true, fp.allTags) 188 + match, data, err := matchFile(fp.c, file, true, fp.allTags, mode) 188 189 switch { 189 190 case match: 190 191
+2 -2
cue/load/match.go
··· 49 49 // considers text until the first non-comment. 50 50 // If allTags is non-nil, matchFile records any encountered build tag 51 51 // 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) { 53 53 if fi := cfg.fileSystem.getOverlay(file.Filename); fi != nil { 54 54 if fi.file != nil { 55 55 file.Source = fi.file ··· 73 73 } 74 74 75 75 name := filepath.Base(file.Filename) 76 - if !cfg.filesMode { 76 + if (mode & allowExcludedFiles) == 0 { 77 77 for _, prefix := range []string{".", "_"} { 78 78 if strings.HasPrefix(name, prefix) { 79 79 return false, nil, &excludeError{