Skip to content

Commit

Permalink
Merge pull request #348 from coryb/force-no-output
Browse files Browse the repository at this point in the history
fix llb.MountOption cast
  • Loading branch information
coryb authored Mar 17, 2023
2 parents 9f4aa69 + f35c260 commit 9f11942
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
11 changes: 10 additions & 1 deletion codegen/builtin_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,17 @@ func (l Local) Call(ctx context.Context, cln *client.Client, val Value, opts Opt
llb.LocalUniqueID(id),
)

st := llb.Local(localPath, localOpts...)
if CalleeBinding(ctx).Binds() == "target" {
// we are mounting this `local` and the `mount` is bound. We are
// currently seeing cache invalidation even if the `local` contents are
// unchanged. A hacky-workaround is to Copy the llb.Local first, then
// mount the Copy, which allows for better caching for Run calls.
st = llb.Scratch().File(llb.Copy(st, "/", "/"))
}

fs := Filesystem{
State: llb.Local(localPath, localOpts...),
State: st,
Platform: DefaultPlatform(ctx),
}
fs.SessionOpts = append(fs.SessionOpts, llbutil.WithSyncedDir(localPath, filesync.SyncedDir{
Expand Down
2 changes: 1 addition & 1 deletion codegen/builtin_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ func (m Mount) Call(ctx context.Context, cln *client.Client, val Value, opts Opt
}
retOpts = append(retOpts, &Mount{Bind: mountpoint, Image: input.Image})
} else {
opts = append(opts, llb.ForceNoOutput)
opts = append(opts, llb.MountOption(llb.ForceNoOutput))
}

retOpts = append(retOpts, &llbutil.MountRunOption{
Expand Down
2 changes: 1 addition & 1 deletion codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func (cg *CodeGen) EmitCallStmt(ctx context.Context, scope *ast.Scope, call *ast
}

// Evaluate args second.
args := cg.Evaluate(ctx, scope, call, b)
args := cg.Evaluate(WithCalleeBinding(ctx, b), scope, call, b)
for i, arg := range call.Arguments() {
ctx = WithArg(ctx, i, arg)
}
Expand Down
20 changes: 20 additions & 0 deletions codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,26 @@ func TestCodeGen(t *testing.T) {
llb.AddMount("/foobar", mnt),
).Root())
},
}, {
"mount local with bind is copied",
[]string{"default"},
`
fs _default() {
image "busybox"
run "find" "/foo" with option {
mount local(".") "/foo" as default
}
}
`, "",
func(ctx context.Context, t *testing.T) solver.Request {
return Expect(t, llb.Image("busybox").Run(
llb.Args([]string{"find", "/foo"}),
llb.AddMount(
"/foo",
llb.Scratch().File(llb.Copy(LocalState(ctx, t, "."), "/", "/")),
),
).Root())
},
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions codegen/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type (
returnTypeKey struct{}
argKey struct{ n int }
bindingKey struct{}
calleeBindingKey struct{}
multiwriterKey struct{}
imageResolverKey struct{}
backtraceKey struct{}
Expand Down Expand Up @@ -89,6 +90,21 @@ func Binding(ctx context.Context) *ast.Binding {
return binding
}

func WithCalleeBinding(ctx context.Context, binding *ast.Binding) context.Context {
if binding != nil {
return context.WithValue(ctx, calleeBindingKey{}, binding)
}
return ctx
}

func CalleeBinding(ctx context.Context) *ast.Binding {
binding, ok := ctx.Value(calleeBindingKey{}).(*ast.Binding)
if !ok {
return &ast.Binding{}
}
return binding
}

func WithArg(ctx context.Context, n int, arg ast.Node) context.Context {
return context.WithValue(ctx, argKey{n}, arg)
}
Expand Down

0 comments on commit 9f11942

Please sign in to comment.