{"record":{"id":"e349c9a4a3ff440f","repo":"hyperledger/fabric","slug":"depspec-cannot-be-nil-if-buf-is-not-nil","errorCode":null,"errorMessage":"depspec cannot be nil if buf is not nil","messagePattern":"depspec cannot be nil if buf is not nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/common/ccprovider/cdspackage.go","lineNumber":236,"sourceCode":"}\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\n\tpath := fmt.Sprintf(\"%s/%s.%s\", chaincodeInstallPath, ccname, ccversion)\n\tif _, err := os.Stat(path); err == nil {\n\t\treturn fmt.Errorf(\"chaincode %s exists\", path)\n\t}","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/common/ccprovider/cdspackage.go#L218-L254","documentation":"PutChaincodeToFS requires ccpack.depSpec, the deserialized ChaincodeDeploymentSpec, to extract the chaincode name and version when writing the package file. buf being non-nil while depSpec is nil indicates the package bytes were loaded but never unmarshaled, so serialization to the filesystem is aborted.","triggerScenarios":"Setting ccpack.buf without running the deserialization step (InitFromBuffer/InitFromPath/PutChaincodeDepSpec with proto unmarshal); a package struct assembled from a cache or copied across goroutines where only buf was carried over.","commonSituations":"Custom install code that caches raw bytes and reconstructs a CDSPackage with only buf; a corrupted or truncated init path that set buf but errored before unmarshaling the depspec, with the error ignored.","solutions":["Re-initialize the package with InitFromBuffer(buf), which unmarshals the ChaincodeDeploymentSpec into ccpack.depSpec.","Prefer InitFromPath + PutChaincodeToFS (or ccprovider.PutChaincode) over manual field population.","Check and handle the error from any prior init call that may have stopped before depSpec was set."],"exampleFix":"// before\nccpack := &CDSPackage{buf: buf, id: id} // depSpec missing\nccpack.PutChaincodeToFS()\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.depSpec == nil {\n    return errors.New(\"depspec not deserialized: call InitFromBuffer(buf) before writing\")\n}","typeGuard":"func hasDepSpec(p *ccprovider.CDSPackage) bool {\n    return p != nil && p.buf != nil && p.depSpec != nil\n}","tryCatchPattern":"if err := ccpack.PutChaincodeToFS(); err != nil {\n    if strings.Contains(err.Error(), \"depspec cannot be nil\") {\n        if e := ccpack.InitFromBuffer(ccpack.buf); e != nil {\n            return e\n        }\n        return ccpack.PutChaincodeToFS()\n    }\n    return err\n}","preventionTips":["Load packages through the standard init functions so unmarshaling always happens.","Handle (don't swallow) errors from deserialization steps that may leave depSpec nil.","Prefer passing raw bytes to InitFromBuffer over caching partially-built package structs."],"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"}