From 2c859b0f06d893ed67219b95b2b9aec8498c200b Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Tue, 21 Apr 2020 22:56:14 -0700 Subject: [PATCH] dockerfile: add more source information to errors Signed-off-by: Tonis Tiigi --- frontend/dockerfile/builder/build.go | 2 +- frontend/dockerfile/dockerfile2llb/convert.go | 20 ++++---- frontend/dockerfile/instructions/commands.go | 15 ++++-- frontend/dockerfile/instructions/parse.go | 13 +++-- frontend/dockerfile/parser/errors.go | 39 ++++++++++++--- frontend/dockerfile/parser/parser.go | 10 ++-- solver/errdefs/errdefs.pb.go | 50 ++++++++----------- 7 files changed, 91 insertions(+), 58 deletions(-) diff --git a/frontend/dockerfile/builder/build.go b/frontend/dockerfile/builder/build.go index 89e5056ddb349..d77a80c42c195 100644 --- a/frontend/dockerfile/builder/build.go +++ b/frontend/dockerfile/builder/build.go @@ -352,7 +352,7 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) { if isNotLocalDockerfile { localNameDockerfile = "" } - err = wrapSource(err, dtDockerfile, filename, localNameDockerfile, el.Ranges) + err = wrapSource(err, dtDockerfile, filename, localNameDockerfile, el.Location) } }() st, img, err := dockerfile2llb.Dockerfile2LLB(ctx, dtDockerfile, dockerfile2llb.ConvertOpt{ diff --git a/frontend/dockerfile/dockerfile2llb/convert.go b/frontend/dockerfile/dockerfile2llb/convert.go index 441075a40e5da..4166033227ea2 100644 --- a/frontend/dockerfile/dockerfile2llb/convert.go +++ b/frontend/dockerfile/dockerfile2llb/convert.go @@ -110,10 +110,10 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, for i, st := range stages { name, err := shlex.ProcessWordWithMap(st.BaseName, metaArgsToMap(optMetaArgs)) if err != nil { - return nil, nil, err + return nil, nil, parser.WithLocation(err, st.Location) } if name == "" { - return nil, nil, errors.Errorf("base name (%s) should not be blank", st.BaseName) + return nil, nil, parser.WithLocation(errors.Errorf("base name (%s) should not be blank", st.BaseName), st.Location) } st.BaseName = name @@ -132,12 +132,12 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, if v := st.Platform; v != "" { v, err := shlex.ProcessWordWithMap(v, metaArgsToMap(optMetaArgs)) if err != nil { - return nil, nil, errors.Wrapf(err, "failed to process arguments for platform %s", v) + return nil, nil, parser.WithLocation(errors.Wrapf(err, "failed to process arguments for platform %s", v), st.Location) } p, err := platforms.Parse(v) if err != nil { - return nil, nil, errors.Wrapf(err, "failed to parse platform %s", v) + return nil, nil, parser.WithLocation(errors.Wrapf(err, "failed to parse platform %s", v), st.Location) } ds.platform = &p } @@ -204,7 +204,7 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, } if has, state := hasCircularDependency(allDispatchStates.states); has { - return nil, nil, fmt.Errorf("circular dependency detected on stage: %s", state.stageName) + return nil, nil, errors.Errorf("circular dependency detected on stage: %s", state.stageName) } if len(allDispatchStates.states) == 1 { @@ -225,7 +225,7 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, eg.Go(func() error { ref, err := reference.ParseNormalizedNamed(d.stage.BaseName) if err != nil { - return errors.Wrapf(err, "failed to parse stage name %q", d.stage.BaseName) + return parser.WithLocation(errors.Wrapf(err, "failed to parse stage name %q", d.stage.BaseName), d.stage.Location) } platform := d.platform if platform == nil { @@ -316,12 +316,12 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, } if d.image.Config.WorkingDir != "" { if err = dispatchWorkdir(d, &instructions.WorkdirCommand{Path: d.image.Config.WorkingDir}, false, nil); err != nil { - return nil, nil, err + return nil, nil, parser.WithLocation(err, d.stage.Location) } } if d.image.Config.User != "" { if err = dispatchUser(d, &instructions.UserCommand{User: d.image.Config.User}, false); err != nil { - return nil, nil, err + return nil, nil, parser.WithLocation(err, d.stage.Location) } } d.state = d.state.Network(opt.ForceNetMode) @@ -346,13 +346,13 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, } if err = dispatchOnBuildTriggers(d, d.image.Config.OnBuild, opt); err != nil { - return nil, nil, err + return nil, nil, parser.WithLocation(err, d.stage.Location) } d.image.Config.OnBuild = nil for _, cmd := range d.commands { if err := dispatch(d, cmd, opt); err != nil { - return nil, nil, err + return nil, nil, parser.WithLocation(err, cmd.Location()) } } diff --git a/frontend/dockerfile/instructions/commands.go b/frontend/dockerfile/instructions/commands.go index 227c4507146c5..bcd495fd6ca00 100644 --- a/frontend/dockerfile/instructions/commands.go +++ b/frontend/dockerfile/instructions/commands.go @@ -5,6 +5,7 @@ import ( "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/strslice" + "github.com/moby/buildkit/frontend/dockerfile/parser" "github.com/pkg/errors" ) @@ -35,6 +36,7 @@ func (kvpo *KeyValuePairOptional) ValueString() string { // Command is implemented by every command present in a dockerfile type Command interface { Name() string + Location() []parser.Range } // KeyValuePairs is a slice of KeyValuePair @@ -42,8 +44,9 @@ type KeyValuePairs []KeyValuePair // withNameAndCode is the base of every command in a Dockerfile (String() returns its source code) type withNameAndCode struct { - code string - name string + code string + name string + location []parser.Range } func (c *withNameAndCode) String() string { @@ -55,8 +58,13 @@ func (c *withNameAndCode) Name() string { return c.name } +// Location of the command in source +func (c *withNameAndCode) Location() []parser.Range { + return c.location +} + func newWithNameAndCode(req parseRequest) withNameAndCode { - return withNameAndCode{code: strings.TrimSpace(req.original), name: req.command} + return withNameAndCode{code: strings.TrimSpace(req.original), name: req.command, location: req.location} } // SingleWordExpander is a provider for variable expansion where 1 word => 1 output @@ -400,6 +408,7 @@ type Stage struct { BaseName string SourceCode string Platform string + Location []parser.Range } // AddCommand to the stage diff --git a/frontend/dockerfile/instructions/parse.go b/frontend/dockerfile/instructions/parse.go index 9496cafadc7b9..93c9767cbb80b 100644 --- a/frontend/dockerfile/instructions/parse.go +++ b/frontend/dockerfile/instructions/parse.go @@ -21,6 +21,7 @@ type parseRequest struct { attributes map[string]bool flags *BFlags original string + location []parser.Range } var parseRunPreHooks []func(*RunCommand, parseRequest) error @@ -48,13 +49,14 @@ func newParseRequestFromNode(node *parser.Node) parseRequest { attributes: node.Attributes, original: node.Original, flags: NewBFlagsWithArgs(node.Flags), + location: node.Location(), } } // ParseInstruction converts an AST to a typed instruction (either a command or a build stage beginning when encountering a `FROM` statement) func ParseInstruction(node *parser.Node) (v interface{}, err error) { defer func() { - err = node.WrapError(err) + err = parser.WithLocation(err, node.Location()) }() req := newParseRequestFromNode(node) switch node.Value { @@ -108,7 +110,7 @@ func ParseCommand(node *parser.Node) (Command, error) { if c, ok := s.(Command); ok { return c, nil } - return nil, node.WrapError(errors.Errorf("%T is not a command type", s)) + return nil, parser.WithLocation(errors.Errorf("%T is not a command type", s), node.Location()) } // UnknownInstruction represents an error occurring when a command is unresolvable @@ -130,7 +132,7 @@ func (e *parseError) Error() string { return fmt.Sprintf("dockerfile parse error line %d: %v", e.node.StartLine, e.inner.Error()) } -func (e *parseError) Unwarp() error { +func (e *parseError) Unwrap() error { return e.inner } @@ -155,11 +157,11 @@ func Parse(ast *parser.Node) (stages []Stage, metaArgs []ArgCommand, err error) case Command: stage, err := CurrentStage(stages) if err != nil { - return nil, nil, n.WrapError(err) + return nil, nil, parser.WithLocation(err, n.Location()) } stage.AddCommand(c) default: - return nil, nil, n.WrapError(errors.Errorf("%T is not a command type", cmd)) + return nil, nil, parser.WithLocation(errors.Errorf("%T is not a command type", cmd), n.Location()) } } @@ -282,6 +284,7 @@ func parseFrom(req parseRequest) (*Stage, error) { SourceCode: code, Commands: []Command{}, Platform: flPlatform.Value, + Location: req.location, }, nil } diff --git a/frontend/dockerfile/parser/errors.go b/frontend/dockerfile/parser/errors.go index b29729d6fdb6a..206448b9d1579 100644 --- a/frontend/dockerfile/parser/errors.go +++ b/frontend/dockerfile/parser/errors.go @@ -2,26 +2,35 @@ package parser import "github.com/pkg/errors" +// ErrorLocation gives a location in source code that caused the error type ErrorLocation struct { - Ranges []Range + Location []Range error } +// Unwrap unwraps to the next error func (e *ErrorLocation) Unwrap() error { return e.error } +// Range is a code section between two positions type Range struct { Start Position End Position } +// Position is a point in source code type Position struct { Line int Character int } -func withLocation(err error, startLine, endLine int) error { +func withLocation(err error, start, end int) error { + return WithLocation(err, toRanges(start, end)) +} + +// WithLocation extends an error with a source code location +func WithLocation(err error, location []Range) error { if err == nil { return nil } @@ -29,10 +38,14 @@ func withLocation(err error, startLine, endLine int) error { if errors.As(err, &el) { return err } - return errors.WithStack(&ErrorLocation{ - error: err, - Ranges: toRanges(startLine, endLine), - }) + var err1 error = &ErrorLocation{ + error: err, + Location: location, + } + if !hasLocalStackTrace(err) { + err1 = errors.WithStack(err1) + } + return err1 } func toRanges(start, end int) (r []Range) { @@ -44,3 +57,17 @@ func toRanges(start, end int) (r []Range) { } return } + +func hasLocalStackTrace(err error) bool { + wrapped, ok := err.(interface { + Unwrap() error + }) + if ok && hasLocalStackTrace(wrapped.Unwrap()) { + return true + } + + _, ok = err.(interface { + StackTrace() errors.StackTrace + }) + return ok +} diff --git a/frontend/dockerfile/parser/parser.go b/frontend/dockerfile/parser/parser.go index 9e51b22e3eda3..dc6d17848a123 100644 --- a/frontend/dockerfile/parser/parser.go +++ b/frontend/dockerfile/parser/parser.go @@ -38,6 +38,11 @@ type Node struct { EndLine int // the line in the original dockerfile where the node ends } +// Location return the location of node in source code +func (node *Node) Location() []Range { + return toRanges(node.StartLine, node.EndLine) +} + // Dump dumps the AST defined by `node` as a list of sexps. // Returns a string suitable for printing. func (node *Node) Dump() string { @@ -63,11 +68,6 @@ func (node *Node) Dump() string { return strings.TrimSpace(str) } -// WrapError returns new error that implements ErrorLocation -func (node *Node) WrapError(err error) error { - return withLocation(err, node.StartLine, node.EndLine) -} - func (node *Node) lines(start, end int) { node.StartLine = start node.EndLine = end diff --git a/solver/errdefs/errdefs.pb.go b/solver/errdefs/errdefs.pb.go index 77ef24cb1c5a9..f62251d9995a8 100644 --- a/solver/errdefs/errdefs.pb.go +++ b/solver/errdefs/errdefs.pb.go @@ -5,7 +5,6 @@ package errdefs import ( fmt "fmt" - _ "github.com/gogo/protobuf/gogoproto" proto "github.com/golang/protobuf/proto" math "math" ) @@ -355,31 +354,26 @@ func init() { func init() { proto.RegisterFile("errdefs.proto", fileDescriptor_689dc58a5060aff5) } var fileDescriptor_689dc58a5060aff5 = []byte{ - // 405 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x52, 0xcd, 0x8a, 0xd4, 0x40, - 0x10, 0x36, 0x66, 0x3a, 0xbb, 0xa9, 0x55, 0xd1, 0xc6, 0x43, 0x58, 0xc4, 0x0d, 0x11, 0x74, 0x10, - 0x37, 0x03, 0xe3, 0xd5, 0xd3, 0xae, 0xec, 0x41, 0x44, 0xa4, 0x57, 0xbd, 0xf7, 0x24, 0x95, 0x6c, - 0x63, 0xd2, 0x3d, 0x74, 0x7a, 0x06, 0x0f, 0xbe, 0x84, 0x6f, 0xe5, 0x23, 0x88, 0x87, 0x39, 0xf8, - 0x24, 0xd2, 0xd5, 0x99, 0x8c, 0xe0, 0x9e, 0x52, 0xdf, 0x4f, 0xd7, 0x57, 0x55, 0x04, 0xee, 0xa3, - 0xb5, 0x35, 0x36, 0x43, 0xb9, 0xb6, 0xc6, 0x19, 0x7e, 0x34, 0xc2, 0xd3, 0xf3, 0x56, 0xb9, 0x9b, - 0xcd, 0xaa, 0xac, 0x4c, 0xbf, 0x68, 0x4d, 0x6b, 0x16, 0xa4, 0xaf, 0x36, 0x0d, 0x21, 0x02, 0x54, - 0x85, 0x77, 0xc5, 0x8f, 0x08, 0xd8, 0xb5, 0x93, 0xd5, 0x57, 0xfe, 0x1c, 0x92, 0xc6, 0xca, 0x1e, - 0x87, 0x2c, 0xca, 0xe3, 0xf9, 0xc9, 0xf2, 0x41, 0xb9, 0x4f, 0xb8, 0xf2, 0xb4, 0x18, 0x55, 0x9e, - 0xc1, 0x51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, 0xbf, 0x00, 0x36, 0x38, 0x69, 0xc3, 0x46, 0x27, - 0xcb, 0x47, 0x53, 0xf0, 0x47, 0x33, 0x28, 0x9f, 0x28, 0x82, 0xce, 0x9f, 0x41, 0x8c, 0xba, 0xa6, - 0xc8, 0x5b, 0x6d, 0x5e, 0x2d, 0xde, 0xc0, 0xf1, 0x9e, 0x98, 0x4e, 0x19, 0x1d, 0x4e, 0xc9, 0x9f, - 0x40, 0x7a, 0x79, 0x23, 0xad, 0xac, 0x1c, 0x5a, 0x6a, 0xc5, 0xc4, 0x81, 0xb8, 0x98, 0xfd, 0xfa, - 0xf3, 0x34, 0x5a, 0x25, 0xf4, 0x3b, 0xbd, 0xfe, 0x1b, 0x00, 0x00, 0xff, 0xff, 0x78, 0xd9, 0x55, - 0x34, 0x97, 0x02, 0x00, 0x00, + // 330 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x52, 0x4f, 0x4b, 0xfb, 0x40, + 0x10, 0x25, 0xbf, 0x74, 0xd3, 0x66, 0xfa, 0x53, 0x74, 0x10, 0x09, 0xe2, 0x21, 0x44, 0xd0, 0x1e, + 0xa4, 0x87, 0x7a, 0xf5, 0x56, 0xe8, 0x49, 0x44, 0xb6, 0xe8, 0x7d, 0x4d, 0xa6, 0x75, 0x31, 0x4d, + 0xca, 0x66, 0x2d, 0x1e, 0xfc, 0x12, 0x7e, 0x63, 0x99, 0x49, 0x9a, 0x1e, 0xf4, 0x36, 0xef, 0x4f, + 0xe6, 0xbd, 0x0c, 0x0b, 0x47, 0xe4, 0x5c, 0x41, 0xab, 0x66, 0xba, 0x75, 0xb5, 0xaf, 0x71, 0xd8, + 0xc1, 0xec, 0x3b, 0x00, 0xb5, 0xf4, 0x26, 0x7f, 0xc7, 0x6b, 0x88, 0x56, 0xce, 0x6c, 0xa8, 0x49, + 0x82, 0x34, 0x9c, 0x8c, 0x67, 0xc7, 0xd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