{"record":{"id":"349c5da70cfc46fa","repo":"hyperledger/fabric","slug":"error-writing-package-metadata-to-tar","errorCode":null,"errorMessage":"error writing package metadata to tar","messagePattern":"error writing package metadata to tar","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/peer/lifecycle/chaincode/package.go","lineNumber":171,"sourceCode":"\treturn nil\n}\n\nfunc (p *Packager) getTarGzBytes() ([]byte, error) {\n\tpayload := bytes.NewBuffer(nil)\n\tgw := gzip.NewWriter(payload)\n\ttw := tar.NewWriter(gw)\n\n\tnormalizedPath, err := p.PlatformRegistry.NormalizePath(strings.ToUpper(p.Input.Type), p.Input.Path)\n\tif err != nil {\n\t\treturn nil, errors.WithMessage(err, \"failed to normalize chaincode path\")\n\t}\n\tmetadataBytes, err := toJSON(normalizedPath, p.Input.Type, p.Input.Label)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\terr = writeBytesToPackage(tw, \"metadata.json\", metadataBytes)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"error writing package metadata to tar\")\n\t}\n\n\tcodeBytes, err := p.PlatformRegistry.GetDeploymentPayload(strings.ToUpper(p.Input.Type), p.Input.Path)\n\tif err != nil {\n\t\treturn nil, errors.WithMessage(err, \"error getting chaincode bytes\")\n\t}\n\n\tcodePackageName := \"code.tar.gz\"\n\n\terr = writeBytesToPackage(tw, codePackageName, codeBytes)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"error writing package code bytes to tar\")\n\t}\n\n\terr = tw.Close()\n\tif err == nil {\n\t\terr = gw.Close()\n\t}","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/internal/peer/lifecycle/chaincode/package.go#L153-L189","documentation":"Inside getTarGzBytes, after metadata.json is generated, writeBytesToPackage writes it into the tar stream. If that write fails (tar writer in error state, gzip/buffer failure) the error is wrapped with 'error writing package metadata to tar'. The underlying cause is always available in the wrapped message.","triggerScenarios":"Failure of toJSON producing metadata, or the tar.Writer rejecting the header/bytes for metadata.json — e.g. an I/O error on the underlying bytes.Buffer or an earlier write already poisoning the writer.","commonSituations":"Rare in practice because the tar is written to an in-memory buffer; most often indicates a preceding failure (e.g. metadata encoding problem) or resource exhaustion (OOM) during packaging of very large packages.","solutions":["Read the wrapped inner error in the message to find the root cause.","Retry the package command; transient memory pressure is the most plausible trigger.","Check available memory (free -m) and reduce concurrent peer CLI operations.","Confirm the fabric binary matches your fabric release to rule out mismatched internal writer code."],"exampleFix":"// before: opaque handling\nerr := writeBytesToPackage(tw, \"metadata.json\", metadataBytes)\nif err != nil {\n    return nil, errors.Wrap(err, \"error writing package metadata to tar\")\n}\n// after: fail fast before tar writing if metadata is empty\nif len(metadataBytes) == 0 {\n    return nil, errors.New(\"empty metadata for chaincode package\")\n}\nerr := writeBytesToPackage(tw, \"metadata.json\", metadataBytes)\nif err != nil {\n    return nil, errors.Wrap(err, \"error writing package metadata to tar\")\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Go caller of getTarGzBytes-equivalent\npkgBytes, err := getTarGzBytes()\nif err != nil {\n    if strings.Contains(err.Error(), \"error writing package metadata to tar\") {\n        logger.Errorf(\"metadata tar write failed: %v\", err) // inspect wrapped cause\n    }\n    return fmt.Errorf(\"package failed: %w\", err)\n}","preventionTips":["Read the wrapped inner error; the root cause follows the message colon.","Retry once — these are typically transient memory issues.","Avoid packaging very large payloads on memory-constrained hosts.","Keep peer binaries consistent with your fabric version."],"tags":["hyperledger-fabric","tar","packaging","io-error"],"backgroundTag":"tar-write-failed","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}