vitessio/vitess · error

malformatted golang image reference

Error message

malformatted golang image reference: %s

What it means

go-upgrade rewrites golang Docker image references in files (e.g. Dockerfiles/workflows) using golangImageRegexp, which expects 3 capture groups (prefix, distro, and the full match context). If a matched image reference doesn't conform to the expected `golang:<tag>` shape, replaceErr is set to this error and the match is left unchanged.

Solutions

  1. Find the file containing the malformed image reference reported by the error and normalize it to the standard `golang:<version>-<distro>` form
  2. If a new legitimate format exists (new registry/distro), extend golangImageRegexp to capture it
  3. Avoid hand-editing image lines; let go-upgrade rewrite them
  4. Re-run go-upgrade to confirm all references are replaced

Example fix

// before (Dockerfile)
FROM golang:1.22-custombuild
// after
FROM golang:1.22-bookworm
Defensive patterns

Strategy: validation

Validate before calling

re := regexp.MustCompile(`golang:\d+\.\d+(\.\d+)?-[a-z0-9]+`)
for _, line := range dockerfileLines {
    if strings.Contains(line, "golang:") && !re.MatchString(line) {
        return fmt.Errorf("normalize image line %q to 'golang:<version>-<distro>' before running go-upgrade", line)
    }
}

Type guard

func isCanonicalGolangRef(ref string) bool {
    return golangImageRegexp.MatchString(ref) && len(golangImageRegexp.FindStringSubmatch(ref)) == 3
}

Try / catch

if err := upgrade.replaceGolangImageReferences(...); err != nil {
    if strings.Contains(err.Error(), "malformatted golang image") {
        return fmt.Errorf("hand-edited image line detected; restore canonical form: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: A file contains a string matched loosely by the image-reference regex but not in the canonical form, e.g. `golang:1.22` with an unexpected suffix, a custom registry form the regex groups differently, or a hand-edited image line like `FROM myrepo/golang:1.22-bookworm-custom` that yields fewer than 3 submatches.

Common situations: Hand-editing a Dockerfile or CI workflow image line; adding a new distro tag format not anticipated by the regex; using a private registry mirror whose reference shape differs from the standard golang image.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/ad2342ace56c56ae. Report an issue: GitHub.

Appendix: source

Thrown at go/tools/go-upgrade/go-upgrade.go:493

		}

		digest, err := resolveGolangImageDigest(goVersion, distro)
		if err != nil {
			return "", err
		}

		digestsByDistro[distro] = digest
	}

	var replaceErr error
	replaced := golangImageRegexp.ReplaceAllStringFunc(content, func(match string) string {
		if replaceErr != nil {
			return match
		}

		submatch := golangImageRegexp.FindStringSubmatch(match)
		if len(submatch) != 3 {
			replaceErr = fmt.Errorf("malformatted golang image reference: %s", match)
			return match
		}

		prefix := submatch[1]
		distro := submatch[2]
		digest, ok := digestsByDistro[distro]
		if !ok {
			replaceErr = fmt.Errorf("missing golang digest for distro %s", distro)
			return match
		}

		return fmt.Sprintf("%s%s@%s", prefix, golangDockerTag(goVersion, distro), digest)
	})
	if replaceErr != nil {
		return "", replaceErr
	}

	return replaced, nil

View on GitHub (pinned to 01a25a7d17)