Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source mapping to codegen'd LLB #154

Merged
merged 1 commit into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ func TestChecker_Check(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
in := strings.NewReader(cleanup(tc.input))

mod, err := parser.Parse(in)
mod, _, err := parser.Parse(in)
require.NoError(t, err)

err = Check(mod)
Expand Down Expand Up @@ -611,14 +611,14 @@ func TestChecker_CheckSelectors(t *testing.T) {
option::image resolveImage() { resolve; }
`

importedModule, err := parser.Parse(strings.NewReader(importedModuleDefinition))
importedModule, _, err := parser.Parse(strings.NewReader(importedModuleDefinition))
require.NoError(t, err)
err = Check(importedModule)
require.NoError(t, err)

in := strings.NewReader(cleanup(tc.input))

module, err := parser.Parse(in)
module, _, err := parser.Parse(in)
require.NoError(t, err)

err = Check(module)
Expand Down
12 changes: 11 additions & 1 deletion cmd/hlb/command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/mattn/go-isatty"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/solver/errdefs"
"github.com/moby/buildkit/util/appcontext"
"github.com/openllb/hlb"
"github.com/openllb/hlb/codegen"
Expand Down Expand Up @@ -67,6 +68,7 @@ var runCommand = &cli.Command{
Targets: c.StringSlice("target"),
LLB: c.Bool("llb"),
LogOutput: c.String("log-output"),
ErrOutput: os.Stderr,
Output: os.Stdout,
})
},
Expand All @@ -78,6 +80,7 @@ type RunOptions struct {
Targets []string
LLB bool
LogOutput string
ErrOutput io.Writer
Output io.Writer

// override defaults sources as necessary
Expand Down Expand Up @@ -205,7 +208,14 @@ func Run(ctx context.Context, cln *client.Client, rc io.ReadCloser, opts RunOpti
return solveReq.Solve(ctx, cln, p.MultiWriter())
})

return p.Wait()
err = p.Wait()
if err != nil {
for _, source := range errdefs.Sources(err) {
source.Print(opts.ErrOutput)
}
return err
}
return nil
}

func ModuleReadCloser(args []string) (io.ReadCloser, error) {
Expand Down
4 changes: 2 additions & 2 deletions codegen/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (cg *CodeGen) setBindingValue(b parser.Binding, value interface{}) error {
}
}

func (cg *CodeGen) EmitBinding(ctx context.Context, scope *parser.Scope, b parser.Binding, args []*parser.Expr, chainStart interface{}) (interface{}, error) {
func (cg *CodeGen) EmitBinding(ctx context.Context, scope *parser.Scope, caller parser.Node, b parser.Binding, args []*parser.Expr, chainStart interface{}) (interface{}, error) {
v, ok := cg.values[b]

// When the binding value is not yet set, emit the containing func to compute a value.
Expand All @@ -49,7 +49,7 @@ func (cg *CodeGen) EmitBinding(ctx context.Context, scope *parser.Scope, b parse
// Codegen can short-circuit control flow by returning ErrBindingCycle up the
// stack. If it matches b then we've finished early and the error is ignored.
var err error
v, err = cg.EmitFuncDecl(ctx, scope, b.Bind.Lexical, args, chainStart)
v, err = cg.EmitFuncDecl(ctx, scope, caller, b.Bind.Lexical, args, chainStart)
if errors.As(err, &cycle) && cycle.Binding == b {
err = nil
v = cg.values[b]
Expand Down
54 changes: 37 additions & 17 deletions codegen/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ func (cg *CodeGen) EmitFilesystemChainStmt(ctx context.Context, scope *parser.Sc
var err error
switch n := obj.Node.(type) {
case *parser.FuncDecl:
v, err = cg.EmitFuncDecl(ctx, scope, n, args, chainStart)
v, err = cg.EmitFuncDecl(ctx, scope, expr, n, args, chainStart)
case *parser.BindClause:
b := n.TargetBinding(expr.Name())
v, err = cg.EmitBinding(ctx, scope, b, args, chainStart)
v, err = cg.EmitBinding(ctx, scope, expr, b, args, chainStart)
case *parser.ImportDecl:
importScope := obj.Data.(*parser.Scope)
importName := expr.Selector.Select.Name
Expand All @@ -105,10 +105,10 @@ func (cg *CodeGen) EmitFilesystemChainStmt(ctx context.Context, scope *parser.Sc

switch m := importObj.Node.(type) {
case *parser.FuncDecl:
v, err = cg.EmitFuncDecl(ctx, scope, m, args, chainStart)
v, err = cg.EmitFuncDecl(ctx, scope, expr, m, args, chainStart)
case *parser.BindClause:
b := m.TargetBinding(importName)
v, err = cg.EmitBinding(ctx, scope, b, args, chainStart)
v, err = cg.EmitBinding(ctx, scope, expr, b, args, chainStart)
default:
return fc, errors.WithStack(ErrCodeGen{m, errors.Errorf("unknown obj type")})
}
Expand Down Expand Up @@ -189,6 +189,9 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
opt := iopt.(llb.ImageOption)
opts = append(opts, opt)
}
for _, opt := range cg.SourceMap(expr) {
opts = append(opts, opt)
}

fc = func(_ llb.State) (llb.State, error) {
return llb.Image(ref, opts...), nil
Expand All @@ -204,6 +207,9 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
opt := iopt.(llb.HTTPOption)
opts = append(opts, opt)
}
for _, opt := range cg.SourceMap(expr) {
opts = append(opts, opt)
}

fc = func(_ llb.State) (llb.State, error) {
return llb.HTTP(url, opts...), nil
Expand All @@ -223,6 +229,10 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
opt := iopt.(llb.GitOption)
opts = append(opts, opt)
}
for _, opt := range cg.SourceMap(expr) {
opts = append(opts, opt)
}

fc = func(_ llb.State) (llb.State, error) {
return llb.Git(remote, ref, opts...), nil
}
Expand All @@ -232,7 +242,7 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
return fc, err
}

path, err = ResolvePathForNode(scope.Node, path)
path, err = parser.ResolvePath(scope.Node, path)
if err != nil {
return fc, err
}
Expand All @@ -247,6 +257,9 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
opt := iopt.(llb.LocalOption)
opts = append(opts, opt)
}
for _, opt := range cg.SourceMap(expr) {
opts = append(opts, opt)
}

if !fi.IsDir() {
filename := filepath.Base(path)
Expand Down Expand Up @@ -350,6 +363,9 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
wantShlex = true
}
}
for _, opt := range cg.SourceMap(expr) {
opts = append(opts, opt)
}

cmd, err := cg.EmitShellCommand(ctx, scope, args, wantShlex)
if err != nil {
Expand Down Expand Up @@ -461,6 +477,7 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
fc = func(st llb.State) (llb.State, error) {
return st.File(
llb.Mkdir(path, os.FileMode(mode), opts...),
cg.SourceMap(expr)...,
), nil
}
case "mkfile":
Expand Down Expand Up @@ -488,6 +505,7 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
fc = func(st llb.State) (llb.State, error) {
return st.File(
llb.Mkfile(path, os.FileMode(mode), []byte(content), opts...),
cg.SourceMap(expr)...,
), nil
}
case "rm":
Expand All @@ -505,6 +523,7 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
fc = func(st llb.State) (llb.State, error) {
return st.File(
llb.Rm(path, opts...),
cg.SourceMap(expr)...,
), nil
}
case "copy":
Expand Down Expand Up @@ -532,6 +551,7 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
fc = func(st llb.State) (llb.State, error) {
return st.File(
llb.Copy(input, src, dest, info),
cg.SourceMap(expr)...,
), nil
}
case "entrypoint":
Expand Down Expand Up @@ -696,7 +716,7 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
return fc, err
}

localPath, err = ResolvePathForNode(scope.Node, localPath)
localPath, err = parser.ResolvePath(scope.Node, localPath)
if err != nil {
return fc, err
}
Expand All @@ -715,7 +735,7 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
return fc, err
}

localPath, err = ResolvePathForNode(scope.Node, localPath)
localPath, err = parser.ResolvePath(scope.Node, localPath)
if err != nil {
return fc, err
}
Expand All @@ -734,7 +754,7 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
return fc, err
}

localPath, err = ResolvePathForNode(scope.Node, localPath)
localPath, err = parser.ResolvePath(scope.Node, localPath)
if err != nil {
return fc, err
}
Expand All @@ -753,7 +773,7 @@ func (cg *CodeGen) EmitFilesystemBuiltinChainStmt(ctx context.Context, scope *pa
return fc, err
}

localPath, err = ResolvePathForNode(scope.Node, localPath)
localPath, err = parser.ResolvePath(scope.Node, localPath)
if err != nil {
return fc, err
}
Expand Down Expand Up @@ -1064,10 +1084,10 @@ func (cg *CodeGen) EmitStringChainStmt(ctx context.Context, scope *parser.Scope,
var err error
switch n := obj.Node.(type) {
case *parser.FuncDecl:
v, err = cg.EmitFuncDecl(ctx, scope, n, args, chainStart)
v, err = cg.EmitFuncDecl(ctx, scope, expr, n, args, chainStart)
case *parser.BindClause:
b := n.TargetBinding(expr.Name())
v, err = cg.EmitBinding(ctx, scope, b, args, chainStart)
v, err = cg.EmitBinding(ctx, scope, expr, b, args, chainStart)
case *parser.ImportDecl:
importScope := obj.Data.(*parser.Scope)
importName := expr.Selector.Select.Name
Expand All @@ -1078,10 +1098,10 @@ func (cg *CodeGen) EmitStringChainStmt(ctx context.Context, scope *parser.Scope,

switch m := importObj.Node.(type) {
case *parser.FuncDecl:
v, err = cg.EmitFuncDecl(ctx, scope, m, args, chainStart)
v, err = cg.EmitFuncDecl(ctx, scope, expr, m, args, chainStart)
case *parser.BindClause:
b := m.TargetBinding(importName)
v, err = cg.EmitBinding(ctx, scope, b, args, chainStart)
v, err = cg.EmitBinding(ctx, scope, expr, b, args, chainStart)
default:
return nil, errors.WithStack(ErrCodeGen{n, errors.Errorf("unknown obj type")})
}
Expand Down Expand Up @@ -1136,10 +1156,10 @@ func (cg *CodeGen) EmitGroupChainStmt(ctx context.Context, scope *parser.Scope,
var v interface{}
switch n := obj.Node.(type) {
case *parser.FuncDecl:
v, err = cg.EmitFuncDecl(ctx, scope, n, args, chainStart)
v, err = cg.EmitFuncDecl(ctx, scope, expr, n, args, chainStart)
case *parser.BindClause:
b := n.TargetBinding(expr.Name())
v, err = cg.EmitBinding(ctx, scope, b, args, chainStart)
v, err = cg.EmitBinding(ctx, scope, expr, b, args, chainStart)
case *parser.ImportDecl:
importScope := obj.Data.(*parser.Scope)
importName := expr.Selector.Select.Name
Expand All @@ -1150,10 +1170,10 @@ func (cg *CodeGen) EmitGroupChainStmt(ctx context.Context, scope *parser.Scope,

switch m := importObj.Node.(type) {
case *parser.FuncDecl:
v, err = cg.EmitFuncDecl(ctx, scope, m, args, chainStart)
v, err = cg.EmitFuncDecl(ctx, scope, expr, m, args, chainStart)
case *parser.BindClause:
b := m.TargetBinding(importName)
v, err = cg.EmitBinding(ctx, scope, b, args, chainStart)
v, err = cg.EmitBinding(ctx, scope, expr, b, args, chainStart)
default:
return gc, errors.WithStack(ErrCodeGen{m, errors.Errorf("unknown obj type")})
}
Expand Down
Loading