Skip to content

Commit 3b0a537

Browse files
committed
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 <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent 37bf801 commit 3b0a537

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

cmd/cue/cmd/testdata/script/eval_e.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ cmp stdout expect/incomplete/stdout
1111
! cue eval foo.bar
1212
cmp stderr expect/foobar/stderr
1313

14+
# Issue #999
15+
cue export --out text -e 'yaml.MarshalStream(X)' issue999/x.cue
16+
cmp stdout expect/issue999/stdout
17+
1418
-- expect/nonExist/stdout --
1519
-- expect/nonExist/stderr --
1620
reference "nonExist" not found:
@@ -42,3 +46,18 @@ package example
4246
Settings: {}
4347
blah: Settings.anyKey
4448

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+

cue/context.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ func ImportPath(path string) BuildOption {
100100
func InferBuiltins(elide bool) BuildOption {
101101
return func(o *runtime.Config) {
102102
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)
107104
}
108105
}
109106
}

internal/core/runtime/imports.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,24 @@ func RegisterBuiltin(importPath string, f PackageFunc) {
3030
}
3131

3232
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{}
3536
}
36-
x.builtins[importPath] = f
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.
41+
}
42+
x.builtinShort[base] = importPath
3743
}
3844

3945
var SharedRuntime = &Runtime{index: sharedIndex}
4046

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)
4351
}
4452

4553
// sharedIndex is used for indexing builtins and any other labels common to
@@ -55,7 +63,8 @@ type index struct {
5563
imports map[*adt.Vertex]*build.Instance
5664
importsByPath map[string]*adt.Vertex
5765
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
5968

6069
// mutex sync.Mutex
6170
typeCache sync.Map // map[reflect.Type]evaluated
@@ -71,12 +80,11 @@ func newIndex() *index {
7180
return i
7281
}
7382

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 ""
7786
}
78-
_, ok := x.builtins[id]
79-
return ok
87+
return x.builtinShort[id]
8088
}
8189

8290
func (r *Runtime) AddInst(path string, key *adt.Vertex, p *build.Instance) {
@@ -107,8 +115,8 @@ func (r *Runtime) LoadImport(importPath string) (*adt.Vertex, errors.Error) {
107115
return key, nil
108116
}
109117

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 {
112120
p, err := f(r)
113121
if err != nil {
114122
return adt.ToVertex(&adt.Bottom{Err: err}), nil

internal/core/runtime/resolve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func resolveFile(
8383
name := path.Base(id)
8484
if imp := p.LookupImport(id); imp != nil {
8585
name = imp.PkgName
86-
} else if _, ok := idx.builtins[id]; !ok {
86+
} else if _, ok := idx.builtinPaths[id]; !ok {
8787
errs = errors.Append(errs,
8888
nodeErrorf(spec, "package %q not found", id))
8989
continue

0 commit comments

Comments
 (0)