From d22a7608c2ce700d69c797d09ff4c0873a9a438d Mon Sep 17 00:00:00 2001 From: Ben Einaudi Date: Sat, 26 Oct 2019 20:04:20 +0200 Subject: [PATCH] Fix failure when using capital letters in image alias in 'FROM ... AS' instruction The third library moby/buildkit lowers the image alias used in 'FROM .. AS' instruction. This change fixes this issue by making the resolve of dependencies agnostic to case. Fixes #592 Fixes #770 --- pkg/dockerfile/dockerfile.go | 3 ++- pkg/dockerfile/dockerfile_test.go | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/dockerfile/dockerfile.go b/pkg/dockerfile/dockerfile.go index 9929f7f4d9..1f1314fb4d 100644 --- a/pkg/dockerfile/dockerfile.go +++ b/pkg/dockerfile/dockerfile.go @@ -203,6 +203,7 @@ func targetStage(stages []instructions.Stage, target string) (int, error) { // resolveStages resolves any calls to previous stages with names to indices // Ex. --from=second_stage should be --from=1 for easier processing later on +// As third party library lowers stage name in FROM instruction, this function resolves stage case insensitively. func resolveStages(stages []instructions.Stage) { nameToIndex := make(map[string]string) for i, stage := range stages { @@ -214,7 +215,7 @@ func resolveStages(stages []instructions.Stage) { switch c := cmd.(type) { case *instructions.CopyCommand: if c.From != "" { - if val, ok := nameToIndex[c.From]; ok { + if val, ok := nameToIndex[strings.ToLower(c.From)]; ok { c.From = val } diff --git a/pkg/dockerfile/dockerfile_test.go b/pkg/dockerfile/dockerfile_test.go index a5cdfc7745..d903fdb52a 100644 --- a/pkg/dockerfile/dockerfile_test.go +++ b/pkg/dockerfile/dockerfile_test.go @@ -197,8 +197,14 @@ func Test_resolveStages(t *testing.T) { FROM scratch AS second COPY --from=0 /hi /hi2 - FROM scratch + FROM scratch AS tHiRd COPY --from=second /hi2 /hi3 + COPY --from=1 /hi2 /hi3 + + FROM scratch + COPY --from=thIrD /hi3 /hi4 + COPY --from=third /hi3 /hi4 + COPY --from=2 /hi3 /hi4 ` stages, _, err := Parse([]byte(dockerfile)) if err != nil { @@ -209,11 +215,14 @@ func Test_resolveStages(t *testing.T) { if index == 0 { continue } - copyCmd := stage.Commands[0].(*instructions.CopyCommand) expectedStage := strconv.Itoa(index - 1) - if copyCmd.From != expectedStage { - t.Fatalf("unexpected copy command: %s resolved to stage %s, expected %s", copyCmd.String(), copyCmd.From, expectedStage) + for _, command := range stage.Commands { + copyCmd := command.(*instructions.CopyCommand) + if copyCmd.From != expectedStage { + t.Fatalf("unexpected copy command: %s resolved to stage %s, expected %s", copyCmd.String(), copyCmd.From, expectedStage) + } } + } }