this repo has no description
0
fork

Configure Feed

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

cue/load: be more permissive with file loading

- allow single _tool.cue files
- allow hidden cue files (.foo.cue)

Fixes #127
Fixes #123
Fixes #136

Change-Id: I1909d078cf42dbde9e83fdaf8fafe31e5a7473cf
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3482
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>

+93 -16
+4 -4
cmd/cue/cmd/common.go
··· 81 81 } 82 82 83 83 func buildFromArgs(cmd *Command, args []string) []*cue.Instance { 84 - binst := loadFromArgs(cmd, args) 84 + binst := loadFromArgs(cmd, args, nil) 85 85 if binst == nil { 86 86 return nil 87 87 } 88 88 return buildInstances(cmd, binst) 89 89 } 90 90 91 - func loadFromArgs(cmd *Command, args []string) []*build.Instance { 92 - binst := load.Instances(args, nil) 91 + func loadFromArgs(cmd *Command, args []string, cfg *load.Config) []*build.Instance { 92 + binst := load.Instances(args, cfg) 93 93 if len(binst) == 0 { 94 94 return nil 95 95 } ··· 135 135 } 136 136 137 137 func buildTools(cmd *Command, args []string) (*cue.Instance, error) { 138 - binst := loadFromArgs(cmd, args) 138 + binst := loadFromArgs(cmd, args, &load.Config{Tools: true}) 139 139 if len(binst) == 0 { 140 140 return nil, nil 141 141 }
+1 -1
cmd/cue/cmd/get_go.go
··· 319 319 320 320 func extract(cmd *Command, args []string) error { 321 321 // determine module root: 322 - binst := loadFromArgs(cmd, []string{"."})[0] 322 + binst := loadFromArgs(cmd, []string{"."}, nil)[0] 323 323 324 324 if err := initInterfaces(); err != nil { 325 325 return err
+9
cmd/cue/cmd/testdata/script/hidden.txt
··· 1 + cue eval 2 + cmp stdout expect-stdout 3 + 4 + -- expect-stdout -- 5 + a: 42 6 + -- .foo.cue -- 7 + package foo 8 + 9 + a: 42
+15
cmd/cue/cmd/testdata/script/toolonly.txt
··· 1 + cue cmd foo 2 + cmp stdout expect-stdout 3 + 4 + -- expect-stdout -- 5 + foo 6 + -- foo_tool.cue -- 7 + package foo 8 + 9 + import "tool/cli" 10 + 11 + command foo task: { 12 + foo: cli.Print & { 13 + text: "foo" 14 + } 15 + }
+1 -1
cmd/cue/cmd/trim.go
··· 102 102 internal.DropOptional = true 103 103 defer func() { internal.DropOptional = false }() 104 104 105 - binst := loadFromArgs(cmd, args) 105 + binst := loadFromArgs(cmd, args, nil) 106 106 if binst == nil { 107 107 return nil 108 108 }
+1 -1
cmd/cue/cmd/vet.go
··· 85 85 } 86 86 87 87 func doVet(cmd *Command, args []string) error { 88 - builds := loadFromArgs(cmd, args) 88 + builds := loadFromArgs(cmd, args, nil) 89 89 if builds == nil { 90 90 return nil 91 91 }
+13 -3
cue/load/errors.go
··· 116 116 117 117 // TODO(localize) 118 118 func (e *noCUEError) Error() string { 119 - // Count files beginning with _ and ., which we will pretend don't exist at all. 119 + // Count files beginning with _, which we will pretend don't exist at all. 120 120 dummy := 0 121 121 for _, name := range e.Package.IgnoredCUEFiles { 122 - if strings.HasPrefix(name, "_") || strings.HasPrefix(name, ".") { 122 + if strings.HasPrefix(name, "_") { 123 123 dummy++ 124 124 } 125 125 } ··· 129 129 130 130 if len(e.Package.IgnoredCUEFiles) > dummy { 131 131 // CUE files exist, but they were ignored due to build constraints. 132 - return "build constraints exclude all CUE files in " + path 132 + msg := "build constraints exclude all CUE files in " + path + " (ignored: " 133 + files := e.Package.IgnoredCUEFiles 134 + if len(files) > 4 { 135 + files = append(files[:4], "...") 136 + } 137 + for i, f := range files { 138 + files[i] = filepath.ToSlash(f) 139 + } 140 + msg += strings.Join(files, ", ") 141 + msg += ")" 142 + return msg 133 143 } 134 144 // if len(e.Package.TestCUEFiles) > 0 { 135 145 // // Test CUE files exist, but we're not interested in them.
+12 -1
cue/load/import.go
··· 256 256 } 257 257 } 258 258 259 + func countCUEFiles(c *Config, p *build.Instance) int { 260 + count := len(p.CUEFiles) 261 + if c.Tools { 262 + count += len(p.ToolCUEFiles) 263 + } 264 + if c.Tests { 265 + count += len(p.TestCUEFiles) 266 + } 267 + return count 268 + } 269 + 259 270 func (fp *fileProcessor) finalize() errors.Error { 260 271 p := fp.pkg 261 272 if fp.err != nil { 262 273 return fp.err 263 274 } 264 - if len(p.CUEFiles) == 0 && !fp.c.DataFiles { 275 + if countCUEFiles(fp.c, p) == 0 && !fp.c.DataFiles { 265 276 fp.err = errors.Append(fp.err, &noCUEError{Package: p, Dir: p.Dir, Ignored: len(p.IgnoredCUEFiles) > 0}) 266 277 return fp.err 267 278 }
+27 -3
cue/load/loader_test.go
··· 37 37 t.Fatal(err) 38 38 } 39 39 testdataDir := filepath.Join(cwd, testdata) 40 - dirCfg := &Config{Dir: testdataDir} 40 + dirCfg := &Config{ 41 + Dir: testdataDir, 42 + Tools: true, 43 + } 41 44 42 45 args := str.StringList 43 46 testCases := []struct { ··· 92 95 cfg: dirCfg, 93 96 args: args("./anon"), 94 97 want: ` 95 - err: build constraints exclude all CUE files in ./anon 98 + err: build constraints exclude all CUE files in ./anon (ignored: anon/anon.cue) 96 99 path: example.org/test/anon 97 100 module: example.org/test 98 101 root: $CWD/testdata ··· 150 153 cfg: dirCfg, 151 154 args: args("example.org/test/hello:nonexist"), 152 155 want: ` 153 - err: build constraints exclude all CUE files in example.org/test/hello:nonexist 156 + err: build constraints exclude all CUE files in example.org/test/hello:nonexist (ignored: hello/test.cue, anon.cue, test.cue) 154 157 path: example.org/test/hello:nonexist 155 158 module: example.org/test 156 159 root: $CWD/testdata ··· 215 218 imports: 216 219 acme.com/catch: $CWD/testdata/pkg/acme.com/catch/catch.cue 217 220 acme.com/helper:helper1: $CWD/testdata/pkg/acme.com/helper/helper1.cue`, 221 + }, { 222 + cfg: dirCfg, 223 + args: args("./toolonly"), 224 + want: ` 225 + path: example.org/test/toolonly:foo 226 + module: example.org/test 227 + root: $CWD/testdata 228 + dir: $CWD/testdata/toolonly 229 + display:./toolonly`, 230 + }, { 231 + cfg: &Config{ 232 + Dir: testdataDir, 233 + }, 234 + args: args("./toolonly"), 235 + want: ` 236 + err: build constraints exclude all CUE files in ./toolonly (ignored: anon.cue, test.cue) 237 + path: example.org/test/toolonly:foo 238 + module: example.org/test 239 + root: $CWD/testdata 240 + dir: $CWD/testdata/toolonly 241 + display:./toolonly`, 218 242 }} 219 243 for i, tc := range testCases { 220 244 // if i != 5 {
+1 -2
cue/load/match.go
··· 43 43 // If allTags is non-nil, matchFile records any encountered build tag 44 44 // by setting allTags[tag] = true. 45 45 func matchFile(cfg *Config, dir, name string, returnImports, allFiles bool, allTags map[string]bool) (match bool, data []byte, filename string, err errors.Error) { 46 - if strings.HasPrefix(name, "_") || 47 - strings.HasPrefix(name, ".") { 46 + if strings.HasPrefix(name, "_") { 48 47 return 49 48 } 50 49
+9
cue/load/testdata/toolonly/foo_tool.cue
··· 1 + package foo 2 + 3 + import "tool/cli" 4 + 5 + command foo task: { 6 + foo: cli.Print & { 7 + text: "foo" 8 + } 9 + }