From 9e0bb85a1b8a1257b20ec54e4df9fcf3e72a02f7 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 14 Jan 2021 15:38:46 -0800 Subject: [PATCH] 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, })