Repository: moby/moby (language: go)

Issue:
[regression in master] docker build without --tag tries to tag as empty image name (BuildKit)  
While running internal tests against builds from master, I noticed some tests failing; when trying to build an image without explicitly tagging it (`-t` / `--tag`), BuildKit attempts to tag the image with an empty name, causing  an `invalid reference format` error.

All cases below use the same version of the CLI and buildx (I'm running the `docker:20.10-dind` image with the socket mounted)


```bash
docker --version
Docker version 20.10.16, build aa7e414
```

Note that the example uses a remote source, but I've seen other tests in our internal tests fail with the same (building from a Dockerfile)

On a docker 20.10 daemon with `DOCKER_BUILDKIT=1` (not using buildx);

```bash
DOCKER_BUILDKIT=1 docker build https://github.com/docker/docker-bench-security.git#v1.3.5
[+] Building 14.5s (9/9) FINISHED
 => [internal] load git source https://github.com/docker/docker-bench-security.git#v1.3.5                                                                                                              1.5s
 => [internal] load metadata for docker.io/library/alpine:3.10                                                                                                                                         3.7s
 => [auth] library/alpine:pull token for registry-1.docker.io                                                                                                                                          0.0s
 => [1/5] FROM docker.io/library/alpine:3.10@sha256:451eee8bedcb2f029756dc3e9d73bab0e7943c1ac55cff3a4861c52a0fdd3e98                                                                                   0.8s
 => => resolve docker.io/library/alpine:3.10@sha256:451eee8bedcb2f029756dc3e9d73bab0e7943c1ac55cff3a4861c52a0fdd3e98                                                                                   0.0s
 => => sha256:451eee8bedcb2f029756dc3e9d73bab0e7943c1ac55cff3a4861c52a0fdd3e98 1.64kB / 1.64kB                                                                                                         0.0s
 => => sha256:e515aad2ed234a5072c4d2ef86a1cb77d5bfe4b11aa865d9214875734c4eeb3c 528B / 528B                                                                                                             0.0s
 => => sha256:e7b300aee9f9bf3433d32bc9305bfdd22183beb59d933b48d77ab56ba53a197a 1.47kB / 1.47kB                                                                                                         0.0s
 => => sha256:396c31837116ac290458afcb928f68b6cc1c7bdd6963fc72f52f365a2a89c1b5 2.80MB / 2.80MB                                                                                                         0.5s
 => => extracting sha256:396c31837116ac290458afcb928f68b6cc1c7bdd6963fc72f52f365a2a89c1b5                                                                                                              0.2s
 => [2/5] RUN set -eux;   sed -i 's!http://dl-cdn.alpinelinux.org/!https://alpine.global.ssl.fastly.net/!g' /etc/apk/repositories &&   apk add --no-cache     iproute2     docker-cli     dumb-init    7.5s
 => [3/5] COPY ./*.sh /usr/local/bin/                                                                                                                                                                  0.2s
 => [4/5] COPY ./tests/*.sh /usr/local/bin/tests/                                                                                                                                                      0.1s
 => [5/5] WORKDIR /usr/local/bin                                                                                                                                                                       0.1s
 => exporting to image                                                                                                                                                                                 0.5s
 => => exporting layers                                                                                                                                                                                0.5s
 => => writing image sha256:7349ab5126886ba84770b3cead8c19de5fb897eb6b0a5cd6d7e161db1541dffb                                                                                                           0.0s
```

On master;

Docker CLI hangs;

```bash
DOCKER_BUILDKIT=1 docker build https://github.com/docker/docker-bench-security.git#v1.3.5
[+] Building 8.1s (8/8)
 => CACHED [internal] load git source https://github.com/docker/docker-bench-security.git#v1.3.5                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/alpine:3.10                                                                                                                                         0.0s
 => [1/5] FROM docker.io/library/alpine:3.10                                                                                                                                                           0.0s
 => CACHED [2/5] RUN set -eux;   sed -i 's!http://dl-cdn.alpinelinux.org/!https://alpine.global.ssl.fastly.net/!g' /etc/apk/repositories &&   apk add --no-cache     iproute2     docker-cli     dumb  0.0s
 => CACHED [3/5] COPY ./*.sh /usr/local/bin/                                                                                                                                                           0.0s
 => CACHED [4/5] COPY ./tests/*.sh /usr/local/bin/tests/                                                                                                                                               0.0s
 => CACHED [5/5] WORKDIR /usr/local/bin                                                                                                                                                                0.0s
 => ERROR exporting to image                                                                                                                                                                           0.0s
 => => exporting layers                                                                                                                                                                                0.0s
 => => writing image sha256:accf73094158d5531fa6c29bdf5554f6b7eecbb9864fd6ad5d35eb7e5bae0749                                                                                                           0.0s
 => => naming to                                                                                                                                                                                       8.0s
^CERRO[0008] got 3 SIGTERM/SIGINTs, forcing shutdown
````

With buildx the error also occurs, but it doesn't hang:

```bash
docker buildx version
github.com/docker/buildx v0.8.2 6224def4dd2c3d347eee19db595348c50d7cb491

docker buildx build https://github.com/docker/docker-bench-security.git#v1.3.5
[+] Building 11.8s (8/8) FINISHED
 => [internal] load git source https://github.com/docker/docker-bench-security.git#v1.3.5                                                                                                              2.6s
 => [internal] load metadata for docker.io/library/alpine:3.10                                                                                                                                         0.0s
 => [1/5] FROM docker.io/library/alpine:3.10                                                                                                                                                           0.0s
 => [2/5] RUN set -eux;   sed -i 's!http://dl-cdn.alpinelinux.org/!https://alpine.global.ssl.fastly.net/!g' /etc/apk/repositories &&   apk add --no-cache     iproute2     docker-cli     dumb-init    5.6s
 => [3/5] COPY ./*.sh /usr/local/bin/                                                                                                                                                                  0.1s
 => [4/5] COPY ./tests/*.sh /usr/local/bin/tests/                                                                                                                                                      0.0s
 => [5/5] WORKDIR /usr/local/bin                                                                                                                                                                       0.0s
 => ERROR exporting to image                                                                                                                                                                           0.5s
 => => exporting layers                                                                                                                                                                                0.5s
 => => writing image sha256:619742465254ba9662a5ee8433919f35498bc958af2ae39b76505da63de76dd3                                                                                                           0.0s
 => => naming to                                                                                                                                                                                       3.0s
------
 > exporting to image:
------
error: failed to solve: invalid reference format
```

With BuildKit disabled, it works as expected;

```bash
DOCKER_BUILDKIT=0 docker build https://github.com/docker/docker-bench-security.git#v1.3.5

DOCKER_BUILDKIT=0 docker build https://github.com/docker/docker-bench-security.git#v1.3.5
Sending build context to Docker daemon  136.2kB
Step 1/9 : FROM alpine:3.10
 ---> e7b300aee9f9
Step 2/9 : LABEL   org.label-schema.name="docker-bench-security"   org.label-schema.url="https://dockerbench.com"   org.label-schema.vcs-url="https://github.com/docker/docker-bench-security.git"
 ---> Using cache
 ---> 6a12ab30ca54
Step 3/9 : RUN set -eux;   sed -i 's!http://dl-cdn.alpinelinux.org/!https://alpine.global.ssl.fastly.net/!g' /etc/apk/repositories &&   apk add --no-cache     iproute2     docker-cli     dumb-init
 ---> Using cache
 ---> 11813d801d06
Step 4/9 : COPY ./*.sh /usr/local/bin/
 ---> Using cache
 ---> 1b30d93bf486
Step 5/9 : COPY ./tests/*.sh /usr/local/bin/tests/
 ---> Using cache
 ---> 3adc8d904556
Step 6/9 : HEALTHCHECK CMD exit 0
 ---> Using cache
 ---> 092c4e4e7695
Step 7/9 : WORKDIR /usr/local/bin
 ---> Using cache
 ---> ed88260a96a4
Step 8/9 : ENTRYPOINT [ "/usr/bin/dumb-init", "docker-bench-security.sh" ]
 ---> Using cache
 ---> aa6f3e91bccc
Step 9/9 : CMD [""]
 ---> Using cache
 ---> 709ff8947191
Successfully built 709ff8947191
```



Discussion / hints:
@tonistiigi @crazy-max PTAL

Fix patch:
diff --git a/builder/builder-next/exporter/export.go b/builder/builder-next/exporter/export.go
index f653cbe9ddef0..ee945a1150402 100644
--- a/builder/builder-next/exporter/export.go
+++ b/builder/builder-next/exporter/export.go
@@ -54,7 +54,13 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
 	for k, v := range opt {
 		switch k {
 		case keyImageName:
-			i.targetName = v
+			for _, v := range strings.Split(v, ",") {
+				ref, err := distref.ParseNormalizedNamed(v)
+				if err != nil {
+					return nil, err
+				}
+				i.targetNames = append(i.targetNames, ref)
+			}
 		case keyBuildInfo:
 			if v == "" {
 				i.buildInfo = true
@@ -87,7 +93,7 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
 
 type imageExporterInstance struct {
 	*imageExporter
-	targetName     string
+	targetNames    []distref.Named
 	meta           map[string][]byte
 	buildInfo      bool
 	buildInfoAttrs bool
@@ -197,14 +203,9 @@ func (e *imageExporterInstance) Export(ctx context.Context, inp exporter.Source,
 	_ = configDone(nil)
 
 	if e.opt.ReferenceStore != nil {
-		targetNames := strings.Split(e.targetName, ",")
-		for _, targetName := range targetNames {
-			tagDone := oneOffProgress(ctx, "naming to "+targetName)
-			tref, err := distref.ParseNormalizedNamed(targetName)
-			if err != nil {
-				return nil, err
-			}
-			if err := e.opt.ReferenceStore.AddTag(tref, digest.Digest(id), true); err != nil {
+		for _, targetName := range e.targetNames {
+			tagDone := oneOffProgress(ctx, "naming to "+targetName.String())
+			if err := e.opt.ReferenceStore.AddTag(targetName, digest.Digest(id), true); err != nil {
 				return nil, tagDone(err)
 			}
 			_ = tagDone(nil)


Test patch: