hashicorp/packer · error

AddArtifactToBuild

Error message

AddArtifactToBuild: %w

What it means

Wraps a failure from Version.Build while resolving the named build, inside AddArtifactToBuild. It means the build referenced by buildName could not be found (no associated build for that name) or was not a valid registry Build, so the artifact could not be attached.

Solutions

  1. Ensure the build was initialized for this version before publishing artifacts (check the component name matches the builder's name)
  2. Run a single Packer invocation per bucket/version to avoid split-brain registry state
  3. Check for typos in build/component names in the template
  4. Read the wrapped error: 'no associated build found for the name X' vs invalid Build type to pick the right fix
Defensive patterns

Strategy: type-guard

Validate before calling

// Before publishing artifacts, confirm the build exists on this version
if !version.HasBuild(buildName) {
    return fmt.Errorf("build %q not initialized for this version", buildName)
}

Type guard

if b, err := version.Build(name); err != nil || b == nil {
    // build missing: initialize before AddArtifactToBuild
}

Try / catch

if err := version.AddArtifactToBuild(name, imgs...); err != nil {
    if strings.Contains(err.Error(), "no associated build found") {
        // initialize the build for this version first, then retry
    }
}

Prevention

When it happens

Trigger: AddArtifactToBuild is called (via UpdateArtifactForBuild) with a buildName that has no entry in version.builds — e.g. artifacts arriving for a component whose build was never created for this version/fingerprint.

Common situations: Multiple Packer builds running against the same bucket where one finishes and publishes artifacts for a component name that another run's version instance does not know; component name mismatches between builder and registry state.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/118acd6485b8305c. Report an issue: GitHub.

Appendix: source

Thrown at internal/hcp/registry/types.version.go:99

	if !ok {
		return nil, fmt.Errorf("the build for the component %q does not appear to be a valid registry Build", buildName)
	}

	return b, nil
}

// HasBuild checks if version has a stored build associated with buildName.
func (version *Version) HasBuild(buildName string) bool {
	_, ok := version.builds.Load(buildName)

	return ok
}

// AddArtifactToBuild appends one or more artifacts to the build referred to by buildName.
func (version *Version) AddArtifactToBuild(buildName string, artifacts ...packerSDKRegistry.Image) error {
	build, err := version.Build(buildName)
	if err != nil {
		return fmt.Errorf("AddArtifactToBuild: %w", err)
	}

	err = build.AddArtifacts(artifacts...)
	if err != nil {
		return fmt.Errorf("AddArtifactToBuild: %w", err)
	}

	version.StoreBuild(buildName, build)
	return nil
}

// AddLabelsToBuild merges the contents of data to the labels associated with the build referred to by buildName.
func (version *Version) AddLabelsToBuild(buildName string, data map[string]string) error {
	build, err := version.Build(buildName)
	if err != nil {
		return fmt.Errorf("AddLabelsToBuild: %w", err)
	}

View on GitHub (pinned to eb36e3c3e4)