From 9e0bb85a1b8a1257b20ec54e4df9fcf3e72a02f7 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 14 Jan 2021 15:38:46 -0800 Subject: [PATCH 1/3] fs: don't panic when handling scratch output results Fixes #22 Signed-off-by: Andrea Luzzardi --- dagger/fs.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/dagger/fs.go b/dagger/fs.go index 7cec80d9..8dc50ca0 100644 --- a/dagger/fs.go +++ b/dagger/fs.go @@ -2,6 +2,7 @@ package dagger import ( "context" + "os" "path" "github.com/moby/buildkit/client/llb" @@ -47,6 +48,12 @@ func (fs FS) ReadFile(ctx context.Context, filename string) ([]byte, error) { if err := (&fs).solve(ctx); err != nil { return nil, err } + // NOTE: llb.Scratch is represented by a `nil` reference. If solve result is + // Scratch, then `fs.output` is `nil`. + if fs.output == nil { + return nil, os.ErrNotExist + } + return fs.output.ReadFile(ctx, bkgw.ReadRequest{Filename: filename}) } @@ -55,6 +62,12 @@ func (fs FS) ReadDir(ctx context.Context, dir string) ([]Stat, error) { if err := (&fs).solve(ctx); err != nil { return nil, err } + + // NOTE: llb.Scratch is represented by a `nil` reference. If solve result is + // Scratch, then `fs.output` is `nil`. + if fs.output == nil { + return []Stat{}, nil + } st, err := fs.output.ReadDir(ctx, bkgw.ReadDirRequest{ Path: dir, }) From 0a91de31467ae596a406b1ae80495dee75f88351 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 14 Jan 2021 17:39:16 -0800 Subject: [PATCH 2/3] test: enable noop Signed-off-by: Andrea Luzzardi --- examples/tests/test.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/tests/test.sh b/examples/tests/test.sh index 3728cfc5..95cbd77d 100755 --- a/examples/tests/test.sh +++ b/examples/tests/test.sh @@ -22,9 +22,8 @@ test::compute(){ "$dagger" compute "$d"/compute/invalid/int test::one "Compute: invalid struct should fail" --exit=1 --stdout= \ "$dagger" compute "$d"/compute/invalid/struct - # XXX https://github.com/blocklayerhq/dagger/issues/22 - #test::one "Compute: noop should succeed" --exit=0 --stdout="{}" \ - # "$dagger" compute "$d"/compute/noop + test::one "Compute: noop should succeed" --exit=0 --stdout='{"empty":{},"realempty":{},"withprops":{}}' \ + "$dagger" compute "$d"/compute/noop # XXX https://github.com/blocklayerhq/dagger/issues/28 #test::one "Compute: unresolved should fail" --exit=1 --stdout= \ # "$dagger" compute "$d"/compute/invalid/undefined_prop From 22fb218b28c0136e1b3360ae6615f578e3c28c26 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 14 Jan 2021 17:52:39 -0800 Subject: [PATCH 3/3] component: do not fail if `#dagger` marker has no compute Signed-off-by: Andrea Luzzardi --- dagger/component.go | 11 ++++++++++- dagger/script.go | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/dagger/component.go b/dagger/component.go index 6acdeb21..874d1512 100644 --- a/dagger/component.go +++ b/dagger/component.go @@ -2,6 +2,7 @@ package dagger import ( "context" + "os" ) type Component struct { @@ -37,7 +38,11 @@ func (c *Component) Validate() error { // Return this component's compute script. func (c *Component) ComputeScript() (*Script, error) { - return c.Value().Get("#dagger.compute").Script() + v := c.Value().Get("#dagger.compute") + if !v.Exists() { + return nil, os.ErrNotExist + } + return v.Script() } // Compute the configuration for this component. @@ -62,6 +67,10 @@ func (c *Component) Compute(ctx context.Context, s Solver, out Fillable) (FS, er func (c *Component) Execute(ctx context.Context, fs FS, out Fillable) (FS, error) { script, err := c.ComputeScript() if err != nil { + // If the component has no script, then do not fail. + if os.IsNotExist(err) { + return fs, nil + } return fs, err } return script.Execute(ctx, fs, out) diff --git a/dagger/script.go b/dagger/script.go index b15752f5..03b6ce11 100644 --- a/dagger/script.go +++ b/dagger/script.go @@ -10,7 +10,7 @@ type Script struct { v *Value } -func (s Script) Validate() error { +func (s *Script) Validate() error { // FIXME this crashes when a script is incomplete or empty return s.Value().Validate("#Script") }