{"record":{"id":"68c93928aa2a5681","repo":"hyperledger/fabric","slug":"id-cannot-be-nil-if-buf-is-not-nil","errorCode":null,"errorMessage":"id cannot be nil if buf is not nil","messagePattern":"id cannot be nil if buf is not nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/common/ccprovider/cdspackage.go","lineNumber":232,"sourceCode":"\t\treturn nil, nil, err\n\t}\n\n\treturn ccpack.buf, ccpack.depSpec, nil\n}\n\n// InitFromFS returns the chaincode and its package from the file system\nfunc (ccpack *CDSPackage) InitFromFS(ccNameVersion string) ([]byte, *pb.ChaincodeDeploymentSpec, error) {\n\treturn ccpack.InitFromPath(ccNameVersion, chaincodeInstallPath)\n}\n\n// PutChaincodeToFS - serializes chaincode to a package on the file system\nfunc (ccpack *CDSPackage) PutChaincodeToFS() error {\n\tif ccpack.buf == nil {\n\t\treturn errors.New(\"uninitialized package\")\n\t}\n\n\tif ccpack.id == nil {\n\t\treturn errors.New(\"id cannot be nil if buf is not nil\")\n\t}\n\n\tif ccpack.depSpec == nil {\n\t\treturn errors.New(\"depspec cannot be nil if buf is not nil\")\n\t}\n\n\tif ccpack.data == nil {\n\t\treturn errors.New(\"nil data\")\n\t}\n\n\tif ccpack.datab == nil {\n\t\treturn errors.New(\"nil data bytes\")\n\t}\n\n\tccname := ccpack.depSpec.ChaincodeSpec.ChaincodeId.Name\n\tccversion := ccpack.depSpec.ChaincodeSpec.ChaincodeId.Version\n\n\t// return error if chaincode exists","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/common/ccprovider/cdspackage.go#L214-L250","documentation":"After confirming ccpack.buf is non-nil, PutChaincodeToFS checks ccpack.id, the package hash/id computed during initialization. A non-nil buffer with a nil id means the package bytes exist but the identity/hash was never derived, so the library refuses to write a package whose id cannot be verified.","triggerScenarios":"Calling PutChaincodeToFS on a CDSPackage where buf was set directly (or via an init path that skipped id computation) but InitFromBuffer/InitFromPath never populated ccpack.id — e.g. a custom or partially-copied CDSPackage struct.","commonSituations":"Hand-assembling a CDSPackage with only buf set; copying a package struct field-by-field and dropping the id; a forked or patched init routine that deserializes the depspec but skips PutChaincodeDepSpec's id assignment.","solutions":["Populate the package through the normal flow: InitFromBuffer or InitFromPath, which set buf, id, depSpec, data, and datab together.","If constructing manually, use PutChaincodeDepSpec with a computed hash (ccprovider.GetHash for the bytes) so ccpack.id is set before writing.","Regenerate the package from the original ChaincodeDeploymentSpec instead of patching fields individually."],"exampleFix":"// before\nccpack := &CDSPackage{buf: buf} // id never computed\nccpack.PutChaincodeToFS() // -> \"id cannot be nil if buf is not nil\"\n\n// after\nccpack := &CDSPackage{}\nif err := ccpack.InitFromBuffer(buf); err != nil {\n    return err\n}\nccpack.PutChaincodeToFS()","handlingStrategy":"validation","validationCode":"if ccpack.buf != nil && ccpack.id == nil {\n    return errors.New(\"package id missing: re-run InitFromBuffer to compute hash\")\n}","typeGuard":"func hasID(p *ccprovider.CDSPackage) bool {\n    return p != nil && p.buf != nil && p.id != nil\n}","tryCatchPattern":"if err := ccpack.PutChaincodeToFS(); err != nil {\n    if strings.Contains(err.Error(), \"id cannot be nil\") {\n        return fmt.Errorf(\"package not fully initialized: %w\", err)\n    }\n    return err\n}","preventionTips":["Never construct CDSPackage field-by-field; use InitFromBuffer/InitFromPath which set id alongside buf.","Copy packages by re-initializing from bytes rather than copying struct fields.","Treat any manual id computation as suspect — use the provider's hash utilities."],"tags":["hyperledger-fabric","chaincode","go","nil-field"],"backgroundTag":"nil-field-invariant-violation","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"}