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

Commit

Permalink
cue: align import with top-level package for -e
Browse files Browse the repository at this point in the history
This allows refering to hidden fields in the top-level
package from -e expressions.

Defines the ImportPath encoding option.

Fixes #904

Change-Id: Ic6c80ca97d50e64b93c054a43c0f91865f7139fb
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9661
Reviewed-by: CUE cueckoo <[email protected]>
Reviewed-by: Marcel van Lohuizen <[email protected]>
  • Loading branch information
mpvl committed May 5, 2021
1 parent 0457356 commit 1f618f0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
13 changes: 11 additions & 2 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ func (b *buildPlan) instances() iterator {
i: -1,
}
default:
i = &instanceIterator{a: []*cue.Instance{b.instance}, i: -1}
i = &instanceIterator{
inst: b.instance,
a: []*cue.Instance{b.instance},
i: -1,
}
b.instance = nil
}
if len(b.expressions) > 0 {
Expand Down Expand Up @@ -217,7 +221,7 @@ func (i *instanceIterator) value() cue.Value {
return v
}
func (i *instanceIterator) instance() *cue.Instance {
if i.inst != nil {
if i.i >= len(i.a) {
return nil
}
return i.a[i.i]
Expand Down Expand Up @@ -352,9 +356,14 @@ func (i *expressionIter) value() cue.Value {
return i.iter.value()
}
v := i.iter.value()
path := ""
if inst := i.iter.instance(); inst != nil {
path = inst.ID()
}
return v.Context().BuildExpr(i.expr[i.i],
cue.Scope(v),
cue.InferBuiltins(true),
cue.ImportPath(path),
)
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/cue/cmd/testdata/script/eval_e_hidden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Issue #904
cue eval -e _a
stdout '34'

cue eval -e _a dep.cue
stdout '34'

cue eval -e _a tst.cue
stdout '34'
-- dep.cue --
package dep

_a: 34
-- tst.cue --
_a: 34
6 changes: 3 additions & 3 deletions cue/build/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ func (inst *Instance) RelPath(f *File) string {

// ID returns the package ID unique for this module.
func (inst *Instance) ID() string {
if inst.PkgName == "" {
return "_"
}
if s := inst.ImportPath; s != "" {
return s
}
if inst.PkgName == "" {
return "_"
}
s := fmt.Sprintf("%s:%s", inst.Module, inst.PkgName)
return s
}
Expand Down
20 changes: 19 additions & 1 deletion cue/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ func Filename(filename string) BuildOption {
return func(o *runtime.Config) { o.Filename = filename }
}

// ImportPath defines the import path to use for building CUE. The import path
// influences the scope in which identifiers occurring in the input CUE are
// defined. Passing the empty string is equal to not specifying this option.
//
// This option is typically not necessary when building using a build.Instance,
// but takes precedence otherwise.
func ImportPath(path string) BuildOption {
return func(o *runtime.Config) { o.ImportPath = path }
}

// InferBuiltins allows unresolved references to bind to builtin packages with a
// unique package name.
//
Expand Down Expand Up @@ -168,8 +178,16 @@ func (c *Context) BuildExpr(x ast.Expr, options ...BuildOption) Value {

ctx := c.ctx()

// TODO: move to runtime?: it probably does not make sense to treat BuildExpr
// and the expression resulting from CompileString differently.
astutil.ResolveExpr(x, errFn)
conjunct, err := compile.Expr(&cfg.Config, r, anonymousPkg, x)

pkgPath := cfg.ImportPath
if pkgPath == "" {
pkgPath = anonymousPkg
}

conjunct, err := compile.Expr(&cfg.Config, r, pkgPath, x)
if err != nil {
return c.makeError(err)
}
Expand Down
1 change: 1 addition & 0 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,7 @@ func (v Value) FillPath(p Path, x interface{}) Value {
expr = x.v
case ast.Expr:
n := getScopePrefix(v, p)
// TODO: inject import path of current package?
expr = resolveExpr(ctx, n, x)
default:
expr = convert.GoValueToValue(ctx, x, true)
Expand Down
9 changes: 7 additions & 2 deletions internal/core/runtime/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import (
)

type Config struct {
Runtime *Runtime
Filename string
Runtime *Runtime
Filename string
ImportPath string

compile.Config
}
Expand Down Expand Up @@ -69,6 +70,10 @@ func (x *Runtime) Build(cfg *Config, b *build.Instance) (v *adt.Vertex, errs err
if cfg != nil {
cc = &cfg.Config
}
if cfg != nil && cfg.ImportPath != "" {
b.ImportPath = cfg.ImportPath
b.PkgName = astutil.ImportPathName(b.ImportPath)
}
v, err = compile.Files(cc, x, b.ID(), b.Files...)
errs = errors.Append(errs, err)

Expand Down

0 comments on commit 1f618f0

Please sign in to comment.