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: require imports for tooling
Browse files Browse the repository at this point in the history
Imports were incorrectly elided before.

Change-Id: I22c8adf9844affcc619714ac435ab85a588b996b
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4386
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Dec 17, 2019
1 parent d8757db commit c82a793
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions cmd/cue/cmd/testdata/script/cmd_errcode.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ message: "Hello world!"
-- task_tool.cue --
package home

import "tool/exec"

command: errcode: {
task: bad: exec.Run & {
kind: "exec"
Expand Down
19 changes: 19 additions & 0 deletions cmd/cue/cmd/testdata/script/cmd_import.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
! cue cmd pkg
cmp stderr expect-stderr

-- expect-stderr --
reference "cli" not found
-- task_tool.cue --
package home

// missing imports

command: pkg: {
task: {
t2: cli.Print & {
text: "Hello world!"
}
}
}

-- cue.mod --
1 change: 1 addition & 0 deletions cmd/cue/cmd/testdata/script/cmd_ref.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package home

import (
"tool/cli"
"tool/exec"
)

command: ref: {
Expand Down
12 changes: 7 additions & 5 deletions cue/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (inst *Instance) insertFile(f *ast.File) errors.Error {
// TODO: insert by converting to value first so that the trim command can
// also remove top-level fields.
// First process single file.
v := newVisitor(inst.index, inst.inst, inst.rootStruct, inst.scope)
v := newVisitor(inst.index, inst.inst, inst.rootStruct, inst.scope, false)
v.astState.astMap[f] = inst.rootStruct
// TODO: fix cmd/import to resolve references in the AST before
// inserting. For now, we accept errors that did not make it up to the tree.
Expand Down Expand Up @@ -80,6 +80,7 @@ type astState struct {

litParser *litParser
resolveRoot *structLit
allowAuto bool // allow builtin packages without import

// make unique per level to avoid reuse of structs being an issue.
astMap map[ast.Node]scope
Expand All @@ -102,12 +103,12 @@ func (s *astState) setScope(n ast.Node, v scope) {
s.astMap[n] = v
}

func newVisitor(idx *index, inst *build.Instance, obj, resolveRoot *structLit) *astVisitor {
func newVisitor(idx *index, inst *build.Instance, obj, resolveRoot *structLit, allowAuto bool) *astVisitor {
ctx := idx.newContext()
return newVisitorCtx(ctx, inst, obj, resolveRoot)
return newVisitorCtx(ctx, inst, obj, resolveRoot, allowAuto)
}

func newVisitorCtx(ctx *context, inst *build.Instance, obj, resolveRoot *structLit) *astVisitor {
func newVisitorCtx(ctx *context, inst *build.Instance, obj, resolveRoot *structLit, allowAuto bool) *astVisitor {
v := &astVisitor{
object: obj,
}
Expand All @@ -117,6 +118,7 @@ func newVisitorCtx(ctx *context, inst *build.Instance, obj, resolveRoot *structL
inst: inst,
litParser: &litParser{ctx: ctx},
resolveRoot: resolveRoot,
allowAuto: allowAuto,
astMap: map[ast.Node]scope{},
}
return v
Expand Down Expand Up @@ -153,7 +155,7 @@ func (v *astVisitor) resolve(n *ast.Ident) value {
&nodeRef{baseValue: newExpr(n), node: r, label: label}, label}
}
}
if v.inSelector > 0 {
if v.inSelector > 0 && v.allowAuto {
if p := getBuiltinShorthandPkg(ctx, name); p != nil {
return &nodeRef{newExpr(n), p, label}
}
Expand Down
2 changes: 1 addition & 1 deletion cue/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func mustParseConstBuiltin(ctx *context, name, val string) evaluated {
if err != nil {
panic(err)
}
v := newVisitor(ctx.index, nil, nil, nil)
v := newVisitor(ctx.index, nil, nil, nil, false)
value := v.walk(expr)
return value.evalPartial(ctx)
}
Expand Down
6 changes: 3 additions & 3 deletions cue/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func parseTag(ctx *context, obj *structLit, field label, tag string) value {
field := ctx.labelStr(field)
return ctx.mkErr(baseValue{}, "invalid tag %q for field %q: %v", tag, field, err)
}
v := newVisitor(ctx.index, nil, nil, obj)
v := newVisitor(ctx.index, nil, nil, obj, true)
return v.walk(expr)
}

Expand Down Expand Up @@ -159,7 +159,7 @@ func parseJSON(ctx *context, b []byte) evaluated {
if err != nil {
panic(err) // cannot happen
}
v := newVisitor(ctx.index, nil, nil, nil)
v := newVisitor(ctx.index, nil, nil, nil, false)
return v.walk(expr).evalPartial(ctx)
}

Expand Down Expand Up @@ -206,7 +206,7 @@ func convertRec(ctx *context, src source, allowDefault bool, x interface{}) eval
return makeNullable(&top{src.base()}, false).(evaluated)

case ast.Expr:
x := newVisitorCtx(ctx, nil, nil, nil)
x := newVisitorCtx(ctx, nil, nil, nil, false)
return ctx.manifest(x.walk(v))

case *big.Int:
Expand Down
4 changes: 2 additions & 2 deletions cue/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func evalExpr(idx *index, x value, expr ast.Expr) evaluated {
return idx.mkErr(obj, "instance is not a struct")
}

v := newVisitor(idx, nil, nil, obj)
v := newVisitor(idx, nil, nil, obj, true)
return eval(idx, v.walk(expr))
}

Expand All @@ -155,7 +155,7 @@ func (inst *Instance) evalExpr(ctx *context, expr ast.Expr) evaluated {
if !ok {
return ctx.mkErr(obj, "instance is not a struct")
}
v := newVisitor(ctx.index, inst.inst, nil, obj)
v := newVisitor(ctx.index, inst.inst, nil, obj, true)
return v.walk(expr).evalPartial(ctx)
}

Expand Down
4 changes: 2 additions & 2 deletions cue/parser/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func ParseFile(filename string, src interface{}, mode ...Option) (f *ast.File, e
f.Filename = filename
astutil.Resolve(f, pp.errf)

return
return f, pp.errors
}

// ParseExpr is a convenience function for parsing an expression.
Expand Down Expand Up @@ -223,7 +223,7 @@ func ParseExpr(filename string, src interface{}, mode ...Option) (ast.Expr, erro
}
astutil.ResolveExpr(e, p.errf)

return e, nil
return e, p.errors
}

// parseExprString is a convenience function for obtaining the AST of an
Expand Down

0 comments on commit c82a793

Please sign in to comment.