cli/cli · error · ErrAttestationFileCreation

failed to marshall attestation to JSON while writing to file

Error message

failed to marshall attestation to JSON while writing to file: %v

What it means

Specific cause joined with ErrAttestationFileCreation when encoding/json cannot marshal an attestation bundle to JSON while writing the .jsonl file. json.Marshal only fails on unsupported values (NaN, cycles, channels, functions), so in practice this signals a corrupted or unexpectedly-shaped in-memory bundle rather than ordinary data.

Source

Thrown at pkg/cmd/attestation/download/metadata.go:53

	return path
}

func (s *LiveStore) createMetadataFile(artifactDigest string, attestationsResp []*api.Attestation) (string, error) {
	metadataFilePath := s.createJSONLinesFilePath(artifactDigest)

	f, err := os.Create(metadataFilePath)
	if err != nil {
		return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to create file: %v", err))
	}

	for _, resp := range attestationsResp {
		bundle := resp.Bundle
		attBytes, err := json.Marshal(bundle)
		if err != nil {
			if err = f.Close(); err != nil {
				return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file while marshalling JSON: %v", err))
			}
			return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to marshall attestation to JSON while writing to file: %v", err))
		}

		withNewline := fmt.Sprintf("%s\n", attBytes)
		_, err = f.Write([]byte(withNewline))
		if err != nil {
			if err = f.Close(); err != nil {
				return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file while handling write error: %v", err))
			}

			return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to write attestations: %v", err))
		}
	}

	if err = f.Close(); err != nil {
		return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file after writing attestations: %v", err))
	}

	return metadataFilePath, nil

View on GitHub (pinned to 0eeec0b92e)

Solutions

  1. Re-download the attestations: the fetched data may have been corrupted
  2. Inspect the bundle offline with `gh attestation inspect <file>` to see if it parses
  3. Report upstream if a well-formed attestation reproduces the failure
Defensive patterns

Strategy: retry

Try / catch

b, err := json.Marshal(bundle)
if err != nil {
	_ = f.Close()
	return fmt.Errorf("marshal bundle %s: %w", digest, err)
}

Prevention

When it happens

Trigger: json.Marshal(resp.Bundle) errors inside the per-attestation loop of createMetadataFile, e.g. a bundle containing values the encoder cannot serialize.

Common situations: Very rare; can appear if the fetched attestation payload was truncated or if a code change introduced a custom type without MarshalJSON support.

Related errors


AI-assisted analysis of cli/cli@0eeec0b92e (2026-08-15). Data as JSON: /api/errors/05fe4a49a2eb2b1f. Report an issue: GitHub.