Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cmd/cue/cmd: fix bug in resolving builtin package shorthands
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mpvl committed May 25, 2021
1 parent 37bf801 commit 3b0a537
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
19 changes: 19 additions & 0 deletions cmd/cue/cmd/testdata/script/eval_e.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ cmp stdout expect/incomplete/stdout
! cue eval foo.bar
cmp stderr expect/foobar/stderr

# Issue #999
cue export --out text -e 'yaml.MarshalStream(X)' issue999/x.cue
cmp stdout expect/issue999/stdout

-- expect/nonExist/stdout --
-- expect/nonExist/stderr --
reference "nonExist" not found:
Expand Down Expand Up @@ -42,3 +46,18 @@ package example
Settings: {}
blah: Settings.anyKey

-- issue999/x.cue --
X: [
{
a: 1
},
{
b: 2
},
]

-- expect/issue999/stdout --
a: 1
---
b: 2

5 changes: 1 addition & 4 deletions cue/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ func ImportPath(path string) BuildOption {
func InferBuiltins(elide bool) BuildOption {
return func(o *runtime.Config) {
o.Imports = func(x *ast.Ident) (pkgPath string) {
if !o.Runtime.IsBuiltinPackage(x.Name) {
return ""
}
return x.Name
return o.Runtime.BuiltinPackagePath(x.Name)
}
}
}
Expand Down
34 changes: 21 additions & 13 deletions internal/core/runtime/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,24 @@ func RegisterBuiltin(importPath string, f PackageFunc) {
}

func (x *index) RegisterBuiltin(importPath string, f PackageFunc) {
if x.builtins == nil {
x.builtins = map[string]PackageFunc{}
if x.builtinPaths == nil {
x.builtinPaths = map[string]PackageFunc{}
x.builtinShort = map[string]string{}
}
x.builtins[importPath] = f
x.builtinPaths[importPath] = f
base := path.Base(importPath)
if _, ok := x.builtinShort[base]; ok {
importPath = "" // Don't allow ambiguous base paths.
}
x.builtinShort[base] = importPath
}

var SharedRuntime = &Runtime{index: sharedIndex}

func (x *Runtime) IsBuiltinPackage(path string) bool {
return x.index.isBuiltin(path)
// BuiltinPackagePath converts a short-form builtin package identifier to its
// full path or "" if this doesn't exist.
func (x *Runtime) BuiltinPackagePath(path string) string {
return x.index.shortBuiltinToPath(path)
}

// sharedIndex is used for indexing builtins and any other labels common to
Expand All @@ -55,7 +63,8 @@ type index struct {
imports map[*adt.Vertex]*build.Instance
importsByPath map[string]*adt.Vertex
importsByBuild map[*build.Instance]*adt.Vertex
builtins map[string]PackageFunc
builtinPaths map[string]PackageFunc // Full path
builtinShort map[string]string // Commandline shorthand

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

func (x *index) isBuiltin(id string) bool {
if x == nil || x.builtins == nil {
return false
func (x *index) shortBuiltinToPath(id string) string {
if x == nil || x.builtinPaths == nil {
return ""
}
_, ok := x.builtins[id]
return ok
return x.builtinShort[id]
}

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

if x.builtins != nil {
if f := x.builtins[importPath]; f != nil {
if x.builtinPaths != nil {
if f := x.builtinPaths[importPath]; f != nil {
p, err := f(r)
if err != nil {
return adt.ToVertex(&adt.Bottom{Err: err}), nil
Expand Down
2 changes: 1 addition & 1 deletion internal/core/runtime/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func resolveFile(
name := path.Base(id)
if imp := p.LookupImport(id); imp != nil {
name = imp.PkgName
} else if _, ok := idx.builtins[id]; !ok {
} else if _, ok := idx.builtinPaths[id]; !ok {
errs = errors.Append(errs,
nodeErrorf(spec, "package %q not found", id))
continue
Expand Down

0 comments on commit 3b0a537

Please sign in to comment.