this repo has no description
0
fork

Configure Feed

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

cmd/cue/cmd: fix bug in resolving builtin package shorthands

Regression resulted in supporting only top-level
builtin packages.

Fixes #999

Change-Id: Ia22ed7435e59cf788e0664cff222eae1887c3b0d
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9902
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>

+42 -18
+19
cmd/cue/cmd/testdata/script/eval_e.txt
··· 11 11 ! cue eval foo.bar 12 12 cmp stderr expect/foobar/stderr 13 13 14 + # Issue #999 15 + cue export --out text -e 'yaml.MarshalStream(X)' issue999/x.cue 16 + cmp stdout expect/issue999/stdout 17 + 14 18 -- expect/nonExist/stdout -- 15 19 -- expect/nonExist/stderr -- 16 20 reference "nonExist" not found: ··· 42 46 Settings: {} 43 47 blah: Settings.anyKey 44 48 49 + -- issue999/x.cue -- 50 + X: [ 51 + { 52 + a: 1 53 + }, 54 + { 55 + b: 2 56 + }, 57 + ] 58 + 59 + -- expect/issue999/stdout -- 60 + a: 1 61 + --- 62 + b: 2 63 +
+1 -4
cue/context.go
··· 100 100 func InferBuiltins(elide bool) BuildOption { 101 101 return func(o *runtime.Config) { 102 102 o.Imports = func(x *ast.Ident) (pkgPath string) { 103 - if !o.Runtime.IsBuiltinPackage(x.Name) { 104 - return "" 105 - } 106 - return x.Name 103 + return o.Runtime.BuiltinPackagePath(x.Name) 107 104 } 108 105 } 109 106 }
+21 -13
internal/core/runtime/imports.go
··· 30 30 } 31 31 32 32 func (x *index) RegisterBuiltin(importPath string, f PackageFunc) { 33 - if x.builtins == nil { 34 - x.builtins = map[string]PackageFunc{} 33 + if x.builtinPaths == nil { 34 + x.builtinPaths = map[string]PackageFunc{} 35 + x.builtinShort = map[string]string{} 36 + } 37 + x.builtinPaths[importPath] = f 38 + base := path.Base(importPath) 39 + if _, ok := x.builtinShort[base]; ok { 40 + importPath = "" // Don't allow ambiguous base paths. 35 41 } 36 - x.builtins[importPath] = f 42 + x.builtinShort[base] = importPath 37 43 } 38 44 39 45 var SharedRuntime = &Runtime{index: sharedIndex} 40 46 41 - func (x *Runtime) IsBuiltinPackage(path string) bool { 42 - return x.index.isBuiltin(path) 47 + // BuiltinPackagePath converts a short-form builtin package identifier to its 48 + // full path or "" if this doesn't exist. 49 + func (x *Runtime) BuiltinPackagePath(path string) string { 50 + return x.index.shortBuiltinToPath(path) 43 51 } 44 52 45 53 // sharedIndex is used for indexing builtins and any other labels common to ··· 55 63 imports map[*adt.Vertex]*build.Instance 56 64 importsByPath map[string]*adt.Vertex 57 65 importsByBuild map[*build.Instance]*adt.Vertex 58 - builtins map[string]PackageFunc 66 + builtinPaths map[string]PackageFunc // Full path 67 + builtinShort map[string]string // Commandline shorthand 59 68 60 69 // mutex sync.Mutex 61 70 typeCache sync.Map // map[reflect.Type]evaluated ··· 71 80 return i 72 81 } 73 82 74 - func (x *index) isBuiltin(id string) bool { 75 - if x == nil || x.builtins == nil { 76 - return false 83 + func (x *index) shortBuiltinToPath(id string) string { 84 + if x == nil || x.builtinPaths == nil { 85 + return "" 77 86 } 78 - _, ok := x.builtins[id] 79 - return ok 87 + return x.builtinShort[id] 80 88 } 81 89 82 90 func (r *Runtime) AddInst(path string, key *adt.Vertex, p *build.Instance) { ··· 107 115 return key, nil 108 116 } 109 117 110 - if x.builtins != nil { 111 - if f := x.builtins[importPath]; f != nil { 118 + if x.builtinPaths != nil { 119 + if f := x.builtinPaths[importPath]; f != nil { 112 120 p, err := f(r) 113 121 if err != nil { 114 122 return adt.ToVertex(&adt.Bottom{Err: err}), nil
+1 -1
internal/core/runtime/resolve.go
··· 83 83 name := path.Base(id) 84 84 if imp := p.LookupImport(id); imp != nil { 85 85 name = imp.PkgName 86 - } else if _, ok := idx.builtins[id]; !ok { 86 + } else if _, ok := idx.builtinPaths[id]; !ok { 87 87 errs = errors.Append(errs, 88 88 nodeErrorf(spec, "package %q not found", id)) 89 89 continue