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/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, }) 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") } 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