Skip to content

Commit

Permalink
dockerfile: show redirect parser warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Dec 1, 2021
1 parent 7ee783e commit 50963e2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
18 changes: 18 additions & 0 deletions client/llb/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ func (def *Definition) FromPB(x *pb.Definition) {
}
}

func (def *Definition) Head() (digest.Digest, error) {
if len(def.Def) == 0 {
return "", nil
}

last := def.Def[len(def.Def)-1]

var pop pb.Op
if err := (&pop).Unmarshal(last); err != nil {
return "", err
}
if len(pop.Inputs) == 0 {
return "", nil
}

return pop.Inputs[0].Digest, nil
}

func WriteTo(def *Definition, w io.Writer) error {
b, err := def.ToPB().Marshal()
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
return nil, errors.Wrapf(err, "failed to marshal local source")
}

defVtx, err := def.Head()
if err != nil {
return nil, err
}

var sourceMap *llb.SourceMap

eg, ctx2 := errgroup.WithContext(ctx)
Expand Down Expand Up @@ -426,6 +431,7 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
err = wrapSource(err, sourceMap, el.Location)
}
}()

st, img, err := dockerfile2llb.Dockerfile2LLB(ctx, dtDockerfile, dockerfile2llb.ConvertOpt{
Target: opts[keyTarget],
MetaResolver: c,
Expand All @@ -449,6 +455,12 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
LLBCaps: &caps,
SourceMap: sourceMap,
Hostname: opts[keyHostname],
Warn: func(msg string, location *parser.Range) {
if i != 0 {
return
}
c.Warn(ctx, defVtx, msg, warnOpts(sourceMap, location))
},
})

if err != nil {
Expand Down Expand Up @@ -767,6 +779,29 @@ func scopeToSubDir(c *llb.State, fileop bool, dir string) *llb.State {
return &bc
}

func warnOpts(sm *llb.SourceMap, r *parser.Range) client.WarnOpts {
opts := client.WarnOpts{Level: 1}
if r == nil {
return opts
}
opts.SourceInfo = &pb.SourceInfo{
Data: sm.Data,
Filename: sm.Filename,
Definition: sm.Definition.ToPB(),
}
opts.Range = []*pb.Range{{
Start: pb.Position{
Line: int32(r.Start.Line),
Character: int32(r.Start.Character),
},
End: pb.Position{
Line: int32(r.End.Line),
Character: int32(r.End.Character),
},
}}
return opts
}

func wrapSource(err error, sm *llb.SourceMap, ranges []parser.Range) error {
if sm == nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type ConvertOpt struct {
ContextLocalName string
SourceMap *llb.SourceMap
Hostname string
Warn func(msg string, location *parser.Range)
}

func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, *Image, error) {
Expand All @@ -91,6 +92,10 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State,
return nil, nil, err
}

for _, w := range dockerfile.Warnings {
opt.Warn(w.Message, w.Location)
}

proxyEnv := proxyEnvFromBuildArgs(opt.BuildArgs)

stages, metaArgs, err := instructions.Parse(dockerfile.AST)
Expand Down
25 changes: 17 additions & 8 deletions frontend/dockerfile/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,25 @@ func newNodeFromLine(line string, d *directives, comments []string) (*Node, erro
type Result struct {
AST *Node
EscapeToken rune
Warnings []string
Warnings []Warning
}

type Warning struct {
Message string
Location *Range
}

// PrintWarnings to the writer
func (r *Result) PrintWarnings(out io.Writer) {
if len(r.Warnings) == 0 {
return
}
fmt.Fprintf(out, strings.Join(r.Warnings, "\n")+"\n")
for _, w := range r.Warnings {
fmt.Fprintf(out, "[WARNING]: %s\n", w.Message)
}
if len(r.Warnings) > 0 {
fmt.Fprintf(out, "[WARNING]: Empty continuation lines will become errors in a future release.\n")
}
}

// Parse reads lines from a Reader, parses the lines into an AST and returns
Expand All @@ -288,7 +298,7 @@ func Parse(rwc io.Reader) (*Result, error) {
root := &Node{StartLine: -1}
scanner := bufio.NewScanner(rwc)
scanner.Split(scanLines)
warnings := []string{}
warnings := []Warning{}
var comments []string

var err error
Expand Down Expand Up @@ -341,7 +351,10 @@ func Parse(rwc io.Reader) (*Result, error) {
}

if hasEmptyContinuationLine {
warnings = append(warnings, "[WARNING]: Empty continuation line found in:\n "+line)
warnings = append(warnings, Warning{
Message: "Empty continuation line found in: " + line,
Location: &Range{Start: Position{Line: currentLine}, End: Position{Line: currentLine}},
})
}

child, err := newNodeFromLine(line, d, comments)
Expand Down Expand Up @@ -384,10 +397,6 @@ func Parse(rwc io.Reader) (*Result, error) {
comments = nil
}

if len(warnings) > 0 {
warnings = append(warnings, "[WARNING]: Empty continuation lines will become errors in a future release.")
}

if root.StartLine < 0 {
return nil, withLocation(errors.New("file with no instructions"), currentLine, 0)
}
Expand Down

0 comments on commit 50963e2

Please sign in to comment.