Browse code

Fix the target name issue for multi-stage build

This PR is trying to fix issue #36956.

The stage name is case-insensitive by design, so we should use
`strings.EqualFold()` as the comparison method to eliminate the
case sensitive noise.

Also we need to return a pre-defined error code order to avoid below
message like:
"FIXME: Got an API for which error does not match any expected type!!!:
failed to reach build target dev in Dockerfile"

Signed-off-by: Dennis Chen <dennis.chen@arm.com>

Dennis Chen authored on 2018/04/27 19:40:59
Showing 2 changed files
... ...
@@ -226,7 +226,7 @@ func (b *Builder) build(source builder.Source, dockerfile *parser.Result) (*buil
226 226
 		targetIx, found := instructions.HasStage(stages, b.options.Target)
227 227
 		if !found {
228 228
 			buildsFailed.WithValues(metricsBuildTargetNotReachableError).Inc()
229
-			return nil, errors.Errorf("failed to reach build target %s in Dockerfile", b.options.Target)
229
+			return nil, errdefs.InvalidParameter(errors.Errorf("failed to reach build target %s in Dockerfile", b.options.Target))
230 230
 		}
231 231
 		stages = stages[:targetIx+1]
232 232
 	}
... ...
@@ -390,7 +390,8 @@ func CurrentStage(s []Stage) (*Stage, error) {
390 390
 // HasStage looks for the presence of a given stage name
391 391
 func HasStage(s []Stage, name string) (int, bool) {
392 392
 	for i, stage := range s {
393
-		if stage.Name == name {
393
+		// Stage name is case-insensitive by design
394
+		if strings.EqualFold(stage.Name, name) {
394 395
 			return i, true
395 396
 		}
396 397
 	}