From 78601fefd668fc8fbeb777f4a4cd59f484fbb556 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Mon, 1 Feb 2021 19:12:38 -0800 Subject: [PATCH] fix dependencies between tasks due to using an old snapshot of cue.Value, components relying on dependent tasks were failing because of non-concretness. Signed-off-by: Andrea Luzzardi --- dagger/env.go | 36 +++++++----- dagger/op.go | 5 +- dagger/script.go | 1 + .../tests/dependencies/interpolation/main.cue | 51 +++++++++++++++++ examples/tests/dependencies/simple/main.cue | 52 ++++++++++++++++++ .../tests/dependencies/unmarshal/main.cue | 55 +++++++++++++++++++ examples/tests/test.sh | 11 ++++ 7 files changed, 195 insertions(+), 16 deletions(-) create mode 100644 examples/tests/dependencies/interpolation/main.cue create mode 100644 examples/tests/dependencies/simple/main.cue create mode 100644 examples/tests/dependencies/unmarshal/main.cue diff --git a/dagger/env.go b/dagger/env.go index b8eb6ce0..eb144c57 100644 --- a/dagger/env.go +++ b/dagger/env.go @@ -280,24 +280,30 @@ func (env *Env) Walk(ctx context.Context, fn EnvWalkFunc) (*Value, error) { } // Cueflow match func flowMatchFn := func(v cue.Value) (cueflow.Runner, error) { - lg := lg. - With(). - Str("path", v.Path().String()). - Logger() - ctx := lg.WithContext(ctx) - - lg.Debug().Msg("Env.Walk: processing") - // FIXME: get directly from state Value ? Locking issue? - val := env.cc.Wrap(v, flowInst) - c, err := NewComponent(val) - if os.IsNotExist(err) { - // Not a component: skip - return nil, nil - } - if err != nil { + if _, err := NewComponent(env.cc.Wrap(v, flowInst)); err != nil { + if os.IsNotExist(err) { + // Not a component: skip + return nil, nil + } return nil, err } return cueflow.RunnerFunc(func(t *cueflow.Task) error { + lg := lg. + With(). + Str("path", t.Path().String()). + Logger() + ctx := lg.WithContext(ctx) + + c, err := NewComponent(env.cc.Wrap(t.Value(), flowInst)) + if err != nil { + return err + } + for _, dep := range t.Dependencies() { + lg. + Debug(). + Str("dependency", dep.Path().String()). + Msg("dependency detected") + } return fn(ctx, c, NewFillable(t)) }), nil } diff --git a/dagger/op.go b/dagger/op.go index 7636ce90..3cc2335b 100644 --- a/dagger/op.go +++ b/dagger/op.go @@ -165,7 +165,10 @@ func (op *Op) Exec(ctx context.Context, fs FS, out *Fillable) (FS, error) { Dir string Always bool } - op.v.Decode(&cmd) + + if err := op.v.Decode(&cmd); err != nil { + return fs, err + } // marker for status events // FIXME opts = append(opts, llb.WithCustomName(op.v.Path().String())) diff --git a/dagger/script.go b/dagger/script.go index 93391662..76860db8 100644 --- a/dagger/script.go +++ b/dagger/script.go @@ -64,6 +64,7 @@ func (s *Script) Execute(ctx context.Context, fs FS, out *Fillable) (FS, error) log. Ctx(ctx). Warn(). + Err(err). Int("op", idx). // FIXME: tell user which inputs are missing (by inspecting references) Msg("script is missing inputs and has not been fully executed") diff --git a/examples/tests/dependencies/interpolation/main.cue b/examples/tests/dependencies/interpolation/main.cue new file mode 100644 index 00000000..c2050af1 --- /dev/null +++ b/examples/tests/dependencies/interpolation/main.cue @@ -0,0 +1,51 @@ +package testing + +A: { + result: string + + #dagger: compute: [ + { + do: "fetch-container" + ref: "alpine" + }, + { + do: "exec" + args: ["sh", "-c", """ + echo '{"result": "from A"}' > /tmp/out + """, + ] + dir: "/" + }, + { + do: "export" + // Source path in the container + source: "/tmp/out" + format: "json" + }, + ] +} + +B: { + result: string + + #dagger: compute: [ + { + do: "fetch-container" + ref: "alpine" + }, + { + do: "exec" + args: ["sh", "-c", """ + echo "{\\"result\\": \\"dependency \(A.result)\\"}" > /tmp/out + """, + ] + dir: "/" + }, + { + do: "export" + // Source path in the container + source: "/tmp/out" + format: "json" + }, + ] +} diff --git a/examples/tests/dependencies/simple/main.cue b/examples/tests/dependencies/simple/main.cue new file mode 100644 index 00000000..31eab026 --- /dev/null +++ b/examples/tests/dependencies/simple/main.cue @@ -0,0 +1,52 @@ +package testing + +A: { + result: string + + #dagger: compute: [ + { + do: "fetch-container" + ref: "alpine" + }, + { + do: "exec" + args: ["sh", "-c", """ + echo '{"result": "from A"}' > /tmp/out + """, + ] + dir: "/" + }, + { + do: "export" + // Source path in the container + source: "/tmp/out" + format: "json" + }, + ] +} + +B: { + result: string + + #dagger: compute: [ + { + do: "fetch-container" + ref: "alpine" + }, + { + do: "exec" + env: DATA: A.result + args: ["sh", "-c", """ + echo "{\\"result\\": \\"dependency $DATA\\"}" > /tmp/out + """, + ] + dir: "/" + }, + { + do: "export" + // Source path in the container + source: "/tmp/out" + format: "json" + }, + ] +} diff --git a/examples/tests/dependencies/unmarshal/main.cue b/examples/tests/dependencies/unmarshal/main.cue new file mode 100644 index 00000000..62195618 --- /dev/null +++ b/examples/tests/dependencies/unmarshal/main.cue @@ -0,0 +1,55 @@ +package testing + +import "encoding/json" + +A: { + string + + #dagger: compute: [ + { + do: "fetch-container" + ref: "alpine" + }, + { + do: "exec" + args: ["sh", "-c", """ + echo '{"hello": "world"}' > /tmp/out + """, + ] + dir: "/" + }, + { + do: "export" + // Source path in the container + source: "/tmp/out" + format: "string" + }, + ] +} + +unmarshalled: json.Unmarshal(A) + +B: { + result: string + + #dagger: compute: [ + { + do: "fetch-container" + ref: "alpine" + }, + { + do: "exec" + args: ["sh", "-c", """ + echo "{\\"result\\": \\"unmarshalled.hello=\(unmarshalled.hello)\\"}" > /tmp/out + """, + ] + dir: "/" + }, + { + do: "export" + // Source path in the container + source: "/tmp/out" + format: "json" + }, + ] +} diff --git a/examples/tests/test.sh b/examples/tests/test.sh index a27e49f3..92cbbb5c 100755 --- a/examples/tests/test.sh +++ b/examples/tests/test.sh @@ -41,6 +41,17 @@ test::compute(){ "$dagger" "${DAGGER_BINARY_ARGS[@]}" compute "$d"/compute/success/overload/wrapped } +test::dependencies(){ + local dagger="$1" + + test::one "Dependencies: simple direct dependency" --exit=0 --stdout='{"A":{"result":"from A"},"B":{"result":"dependency from A"}}' \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" compute "$d"/dependencies/simple + test::one "Dependencies: interpolation" --exit=0 --stdout='{"A":{"result":"from A"},"B":{"result":"dependency from A"}}' \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" compute "$d"/dependencies/interpolation + test::one "Dependencies: json.Unmarshal" --exit=0 --stdout='{"A":"{\"hello\": \"world\"}\n","B":{"result":"unmarshalled.hello=world"},"unmarshalled":{"hello":"world"}}' \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" compute "$d"/dependencies/unmarshal +} + test::fetchcontainer(){ local dagger="$1"