Merge pull request #31 from blocklayerhq/fix-fs-scratch

fs: don't panic when handling scratch output results
This commit is contained in:
Andrea Luzzardi 2021-01-14 18:04:43 -08:00 committed by GitHub
commit 7a837ef8fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 5 deletions

View File

@ -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)

View File

@ -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,
})

View File

@ -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")
}

View File

@ -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