Skip to content

Commit

Permalink
tools/flow: ignore faulty tasks outside the root
Browse files Browse the repository at this point in the history
This may only occur with InferTasks, the use of which
is highly discouraged.

Fixes #608

Change-Id: I840534a913e03630caabf3eb58a10a93d51b5a18
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7943
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Dec 15, 2020
1 parent b8c852d commit d5ced74
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@ cmp stdout expect-stdout
-- f1.cue --
package kube

pkg: {
#Def: {
a: int
b: #Role
}
#Role: {
kind: string
name: string
}

}

test: pkg.#Def
test: {
a: 1
b: {
kind: "foo"
name: "bar"
}
}

// A kind at the top-level should not be allowed.
Expand All @@ -26,4 +43,7 @@ command: dump: {

-- expect-stdout --
a: 1
b:
kind: foo
name: bar

3 changes: 3 additions & 0 deletions tools/flow/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func taskFunc(v cue.Value) (flow.Runner, error) {
return nil
}), nil
}
if err != nil && v.Lookup("$id").Exists() {
return nil, err
}

case "valToOut":
return flow.RunnerFunc(func(t *flow.Task) error {
Expand Down
23 changes: 21 additions & 2 deletions tools/flow/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ func (c *Controller) getTask(scope *Task, v cue.Value) *Task {

var errs errors.Error
if err != nil {
c.addErr(err, "invalid task")
errs = errors.Promote(err, "create task")
if !c.inRoot(w) {
// Must be in InferTask mode. In this case we ignore the error.
r = nil
} else {
c.addErr(err, "invalid task")
errs = errors.Promote(err, "create task")
}
}

if r != nil {
Expand Down Expand Up @@ -224,4 +229,18 @@ func (c *Controller) markTaskDependencies(t *Task, n *adt.Vertex) {
})
}

func (c *Controller) inRoot(n *adt.Vertex) bool {
path := cue.MakeValue(c.opCtx, n).Path().Selectors()
root := c.cfg.Root.Selectors()
if len(path) < len(root) {
return false
}
for i, sel := range root {
if path[i] != sel {
return false
}
}
return true
}

var cycleMarker = &Task{}
12 changes: 9 additions & 3 deletions tools/flow/testdata/infer.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ top1: {
$after: top0
}

top3: {
$id: string // not a task
}

root: {
t1: {
$id: "valToOut"
$after: top1
$after: [top1, top3]
}
}
-- out/run/errors --
Expand Down Expand Up @@ -61,10 +65,12 @@ graph TD
-- out/run/t3/value --
{
$id: "valToOut"
$after: {
$after: [{
$id: "valToOut"
$after: {
$id: "valToOut"
}
}
}, {
$id: string
}]
}

0 comments on commit d5ced74

Please sign in to comment.