hashicorp/packer · error

failed to create decoder for HCP Packer artifact: %w

Error message

failed to create decoder for HCP Packer artifact: %w

What it means

doCompleteBuild uses a mapstructure decoder to convert each artifact's HCP state into []packerSDKRegistry.Image. If mapstructure.NewDecoder itself fails — which for this fixed configuration essentially only happens with an invalid decoder config (e.g. Result not a settable pointer) — the artifact upload aborts with this wrapped error. In practice this is almost always an internal/programming error rather than a user misconfiguration.

Source

Thrown at internal/hcp/registry/types.bucket.go:773

	return artifacts, err
}

func (bucket *Bucket) doCompleteBuild(
	ctx context.Context,
	buildName string,
	packerSDKArtifacts []packerSDK.Artifact,
	ui packerSDK.Ui,
	buildErr error,
) ([]packerSDK.Artifact, error) {
	for _, art := range packerSDKArtifacts {
		var sdkImages []packerSDKRegistry.Image
		decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
			Result:           &sdkImages,
			WeaklyTypedInput: true,
			ErrorUnused:      false,
		})
		if err != nil {
			return packerSDKArtifacts, fmt.Errorf(
				"failed to create decoder for HCP Packer artifact: %w",
				err)
		}

		state := art.State(packerSDKRegistry.ArtifactStateURI)
		if state == nil {
			log.Printf("[WARN] - artifact %q returned a nil value for the HCP state, ignoring", art.BuilderId())
			continue
		}

		err = decoder.Decode(state)
		if err != nil {
			log.Printf("[WARN] - artifact %q failed to be decoded to an HCP artifact, this is probably because it is not compatible: %s", art.BuilderId(), err)
			continue
		}

		err = bucket.UpdateArtifactForBuild(buildName, sdkImages...)
		if err != nil {

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Report as a Packer bug with the full output (github.com/hashicorp/packer/issues)
  2. If using a fork or patch, revert changes to the mapstructure.DecoderConfig in doCompleteBuild
  3. Ensure packer-plugin-sdk and mapstructure dependency versions are consistent when building from source
Defensive patterns

Strategy: type-guard

Validate before calling

// Only relevant when patching Packer: unit-test the decoder config
// dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{Result: &imgs, WeaklyTypedInput: true})
// if err != nil { t.Fatalf("decoder config invalid: %v", err) }

Type guard

// Go: assert the decode target is a valid settable pointer
func validDecoderTarget(p interface{}) bool {
	v := reflect.ValueOf(p)
	return v.Kind() == reflect.Ptr && !v.IsNil() && v.Elem().CanSet()
}

Try / catch

// Go
_, err := bucket.CompleteBuild(ctx, buildName, arts, ui, nil)
if err != nil && strings.Contains(err.Error(), "failed to create decoder") {
	// internal bug path: file an issue at github.com/hashicorp/packer/issues
	return fmt.Errorf("packer internal error, please report: %w", err)
}

Prevention

When it happens

Trigger: Bucket.CompleteBuild -> doCompleteBuild constructs mapstructure.NewDecoder with Result pointing at &sdkImages; the constructor returns an error (misconfigured DecoderConfig), triggering the wrapped return.

Common situations: Code changes to the decoder configuration in a forked/patched Packer or plugin SDK that make the config invalid; virtually never hit by end users running stock Packer.

Related errors


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