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
This commit is contained in:
Ben Einaudi 2019-10-26 20:04:20 +02:00 committed by Ben Einaudi
parent 034ac9e258
commit d22a7608c2
2 changed files with 15 additions and 5 deletions

View File

@ -203,6 +203,7 @@ func targetStage(stages []instructions.Stage, target string) (int, error) {
// resolveStages resolves any calls to previous stages with names to indices // resolveStages resolves any calls to previous stages with names to indices
// Ex. --from=second_stage should be --from=1 for easier processing later on // 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) { func resolveStages(stages []instructions.Stage) {
nameToIndex := make(map[string]string) nameToIndex := make(map[string]string)
for i, stage := range stages { for i, stage := range stages {
@ -214,7 +215,7 @@ func resolveStages(stages []instructions.Stage) {
switch c := cmd.(type) { switch c := cmd.(type) {
case *instructions.CopyCommand: case *instructions.CopyCommand:
if c.From != "" { if c.From != "" {
if val, ok := nameToIndex[c.From]; ok { if val, ok := nameToIndex[strings.ToLower(c.From)]; ok {
c.From = val c.From = val
} }

View File

@ -197,8 +197,14 @@ func Test_resolveStages(t *testing.T) {
FROM scratch AS second FROM scratch AS second
COPY --from=0 /hi /hi2 COPY --from=0 /hi /hi2
FROM scratch FROM scratch AS tHiRd
COPY --from=second /hi2 /hi3 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)) stages, _, err := Parse([]byte(dockerfile))
if err != nil { if err != nil {
@ -209,12 +215,15 @@ func Test_resolveStages(t *testing.T) {
if index == 0 { if index == 0 {
continue continue
} }
copyCmd := stage.Commands[0].(*instructions.CopyCommand)
expectedStage := strconv.Itoa(index - 1) expectedStage := strconv.Itoa(index - 1)
for _, command := range stage.Commands {
copyCmd := command.(*instructions.CopyCommand)
if copyCmd.From != expectedStage { if copyCmd.From != expectedStage {
t.Fatalf("unexpected copy command: %s resolved to stage %s, expected %s", copyCmd.String(), copyCmd.From, expectedStage) t.Fatalf("unexpected copy command: %s resolved to stage %s, expected %s", copyCmd.String(), copyCmd.From, expectedStage)
} }
} }
}
} }
func Test_targetStage(t *testing.T) { func Test_targetStage(t *testing.T) {