this repo has no description
0
fork

Configure Feed

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

cue/load: implement SkipImports mode

This flag specifies that no imports or dependencies should
be loaded. We use that flag in `cue fmt` to avoid awkward
checks about errors. It also avoids `cue fmt` doing lots
of redundant work only to discard the results.

Fixes #3329
Fixes #728

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I5a2c82809e594b06fdd4ea2eedd2d417f1c17572
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198555
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+94 -32
+2 -8
cmd/cue/cmd/fmt.go
··· 64 64 Tools: true, 65 65 AllCUEFiles: true, 66 66 Package: "*", 67 + SkipImports: true, 67 68 }) 68 69 if len(builds) == 0 { 69 70 return errors.Newf(token.NoPos, "invalid args") 70 71 } 71 - 72 72 for _, inst := range builds { 73 73 if err := inst.Err; err != nil { 74 - switch { 75 - case errors.As(err, new(*load.PackageError)) && len(inst.BuildFiles) != 0: 76 - // Ignore package errors if there are files to format. 77 - case errors.As(err, new(*load.NoFilesError)): 78 - default: 79 - return err 80 - } 74 + return err 81 75 } 82 76 for _, file := range inst.BuildFiles { 83 77 shouldFormat := inst.User || file.Filename == "-" || filepath.Dir(file.Filename) == inst.Dir
+28
cmd/cue/cmd/testdata/script/fmt_issue3329.txtar
··· 1 + # Issue 3329: this failed with a "no dependency found" error. 2 + 3 + exec cue fmt ./... 4 + cmp bar/bar.cue bar.cue.golden 5 + cmp bar/nonpkg.cue nonpkg.cue.golden 6 + 7 + -- bar.cue.golden -- 8 + package bar 9 + 10 + x: true 11 + -- nonpkg.cue.golden -- 12 + import "cue.example/foo/bar" 13 + 14 + bar 15 + -- cue.mod/module.cue -- 16 + module: "cue.example/foo" 17 + language: { 18 + version: "v0.9.0" 19 + } 20 + -- bar/bar.cue -- 21 + package bar 22 + 23 + x: true 24 + 25 + -- bar/nonpkg.cue -- 26 + import "cue.example/foo/bar" 27 + 28 + bar
+25 -13
cue/load/config.go
··· 249 249 // a package. 250 250 Tools bool 251 251 252 + // SkipImports causes the loading to ignore all imports and dependencies. 253 + // The registry will never be consulted. Any external package paths 254 + // mentioned on the command line will result in an error. 255 + // The [cue/build.Instance.Imports] field will be empty. 256 + SkipImports bool 257 + 252 258 // If DataFiles is set, the loader includes entries for directories that 253 259 // have no CUE files, but have recognized data files that could be converted 254 260 // to CUE. ··· 376 382 } else if !filepath.IsAbs(c.ModuleRoot) { 377 383 c.ModuleRoot = filepath.Join(c.Dir, c.ModuleRoot) 378 384 } 379 - // Note: if cueexperiment.Flags.Modules _isn't_ set but c.Registry 380 - // is, we consider that a good enough hint that modules support 381 - // should be enabled and hence don't return an error in that case. 382 - if cueexperiment.Flags.Modules && c.Registry == nil { 383 - registry, err := modconfig.NewRegistry(&modconfig.Config{ 384 - Env: c.Env, 385 - }) 386 - if err != nil { 387 - // If there's an error in the registry configuration, 388 - // don't error immediately, but only when we actually 389 - // need to resolve modules. 390 - registry = errorRegistry{err} 385 + if c.SkipImports { 386 + // We should never use the registry in SkipImports mode 387 + // but nil it out to be sure. 388 + c.Registry = nil 389 + } else { 390 + // Note: if cueexperiment.Flags.Modules _isn't_ set but c.Registry 391 + // is, we consider that a good enough hint that modules support 392 + // should be enabled and hence don't return an error in that case. 393 + if cueexperiment.Flags.Modules && c.Registry == nil { 394 + registry, err := modconfig.NewRegistry(&modconfig.Config{ 395 + Env: c.Env, 396 + }) 397 + if err != nil { 398 + // If there's an error in the registry configuration, 399 + // don't error immediately, but only when we actually 400 + // need to resolve modules. 401 + registry = errorRegistry{err} 402 + } 403 + c.Registry = registry 391 404 } 392 - c.Registry = registry 393 405 } 394 406 if err := c.loadModule(); err != nil { 395 407 return nil, err
+11 -4
cue/load/import.go
··· 53 53 // is present. 54 54 // _ anonymous files (which may be marked with _) 55 55 // * all packages 56 - func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance { 56 + func (l *loader) importPkg(pos token.Pos, p *build.Instance) (_ret []*build.Instance) { 57 57 retErr := func(errs errors.Error) []*build.Instance { 58 58 // XXX: move this loop to ReportError 59 59 for _, err := range errors.Errors(errs) { ··· 270 270 return nil 271 271 } 272 272 273 - func (l *loader) loadFunc(pos token.Pos, path string) *build.Instance { 273 + func (l *loader) loadFunc() build.LoadFunc { 274 + if l.cfg.SkipImports { 275 + return nil 276 + } 277 + return l._loadFunc 278 + } 279 + 280 + func (l *loader) _loadFunc(pos token.Pos, path string) *build.Instance { 274 281 impPath := importPath(path) 275 282 if isLocalImport(path) { 276 283 return l.cfg.newErrInstance(errors.Newf(pos, "relative import paths not allowed (%q)", path)) ··· 293 300 panic(fmt.Errorf("non-relative import path %q passed to newRelInstance", path)) 294 301 } 295 302 296 - p := l.cfg.Context.NewInstance(path, l.loadFunc) 303 + p := l.cfg.Context.NewInstance(path, l.loadFunc()) 297 304 p.PkgName = pkgName 298 305 p.DisplayPath = filepath.ToSlash(path) 299 306 // p.ImportPath = string(dir) // compute unique ID. ··· 369 376 370 377 func (l *loader) newInstance(pos token.Pos, p importPath) *build.Instance { 371 378 dir, modPath, err := l.absDirFromImportPath(pos, p) 372 - i := l.cfg.Context.NewInstance(dir, l.loadFunc) 379 + i := l.cfg.Context.NewInstance(dir, l.loadFunc()) 373 380 i.Err = errors.Append(i.Err, err) 374 381 i.Dir = dir 375 382
+13 -6
cue/load/instances.go
··· 104 104 if err != nil { 105 105 return []*build.Instance{c.newErrInstance(err)} 106 106 } 107 - pkgs, err := loadPackages(ctx, c, expandedPaths, otherFiles, tg) 108 - if err != nil { 109 - return []*build.Instance{c.newErrInstance(err)} 107 + 108 + var pkgs *modpkgload.Packages 109 + if !c.SkipImports { 110 + pkgs, err = loadPackages(ctx, c, expandedPaths, otherFiles, tg) 111 + if err != nil { 112 + return []*build.Instance{c.newErrInstance(err)} 113 + } 110 114 } 111 115 l := newLoader(c, tg, pkgs) 112 116 113 117 if c.Context == nil { 114 - c.Context = build.NewContext( 115 - build.Loader(l.loadFunc), 118 + opts := []build.Option{ 116 119 build.ParseFile(c.ParseFile), 117 - ) 120 + } 121 + if f := l.loadFunc(); l != nil { 122 + opts = append(opts, build.Loader(f)) 123 + } 124 + c.Context = build.NewContext(opts...) 118 125 } 119 126 120 127 a := []*build.Instance{}
+1 -1
cue/load/loader.go
··· 82 82 // (typically named on the command line). 83 83 func (l *loader) cueFilesPackage(files []*build.File) *build.Instance { 84 84 // ModInit() // TODO: support modules 85 - pkg := l.cfg.Context.NewInstance(l.cfg.Dir, l.loadFunc) 85 + pkg := l.cfg.Context.NewInstance(l.cfg.Dir, l.loadFunc()) 86 86 87 87 for _, bf := range files { 88 88 f := bf.Filename
+14
cue/load/loader_test.go
··· 277 277 imports: 278 278 mod.test/catch: $CWD/testdata/testmod/cue.mod/pkg/mod.test/catch/catch.cue 279 279 mod.test/helper:helper1: $CWD/testdata/testmod/cue.mod/pkg/mod.test/helper/helper1.cue`}, { 280 + name: "PackageWithImportsWithSkipImportsConfig", 281 + cfg: &Config{ 282 + Dir: testdataDir, 283 + Tools: true, 284 + SkipImports: true, 285 + }, 286 + args: []string{"./imports"}, 287 + want: `path: mod.test/test/imports@v0 288 + module: mod.test/test@v0 289 + root: $CWD/testdata/testmod 290 + dir: $CWD/testdata/testmod/imports 291 + display:./imports 292 + files: 293 + $CWD/testdata/testmod/imports/imports.cue`}, { 280 294 name: "OnlyToolFiles", 281 295 cfg: dirCfg, 282 296 args: []string{"./toolonly"},