{"record":{"id":"a2137712da62d399","repo":"hyperledger/fabric","slug":"chaincode-s-exists","errorCode":null,"errorMessage":"chaincode %s exists","messagePattern":"chaincode (.+?) exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/common/ccprovider/cdspackage.go","lineNumber":253,"sourceCode":"\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}\n\n\tif err := os.WriteFile(path, ccpack.buf, 0o644); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":235,"sourceCodeEnd":262,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/common/ccprovider/cdspackage.go#L235-L262","documentation":"PutChaincodeToFS refuses to write a chaincode package to the filesystem when a file for the same chaincode name and version already exists at chaincodeInstallPath. It is an idempotency guard: the installed file '<name>.<version>' is the package's on-disk identity, and Hyperledger Fabric does not overwrite an installed chaincode package. The error is raised before os.WriteFile in core/common/ccprovider/cdspackage.go:253.","triggerScenarios":"Calling PutChaincode (or processCDS) with a ChaincodeDeploymentSpec whose ChaincodeId.Name/ChaincodeId.Version resolve to 'installpath/name.version' that os.Stat finds already on disk; installing the same package twice; a test (TestPutChaincodeToFSErrorPaths) deliberately pre-creating the target file.","commonSituations":"Re-running a deployment script after a previous install already succeeded; attempting to upgrade chaincode without bumping the version; stale files left in /var/hyperledger/production/chaincodes from a prior environment or failed teardown.","solutions":["Bump ChaincodeId.Version (or use the chaincode upgrade flow with a new version) before installing again.","Check existence first with os.Stat on 'installpath/name.version' and skip the install if a valid package is already present.","Compare the existing file's bytes/hash with the new package; if identical, treat the install as already complete.","Remove the stale package file (or clean the chaincode install directory) only if it is known to be corrupt or unwanted."],"exampleFix":"// before\nerr := ccprovider.PutChaincode(ctx, cds)\n// after\npath := filepath.Join(ccprovider.GetChaincodeInstallPath(), name+\".\"+version)\nif _, statErr := os.Stat(path); os.IsNotExist(statErr) {\n    err = ccprovider.PutChaincode(ctx, cds)\n} else {\n    log.Printf(\"chaincode %s already installed, skipping\", path)\n}","handlingStrategy":"validation","validationCode":"path := filepath.Join(chaincodeInstallPath, ccname+\".\"+ccversion)\nif _, err := os.Stat(path); err == nil {\n    return fmt.Errorf(\"chaincode %s already installed: bump version or skip\", path)\n}","typeGuard":null,"tryCatchPattern":"if err := ccprovider.PutChaincode(ctx, cds); err != nil && strings.HasSuffix(err.Error(), \"exists\") {\n    log.Printf(\"already installed, skipping: %v\", err)\n}","preventionTips":["Stat 'name.version' before installing and make installs idempotent.","Bump ChaincodeId.Version on every code change; never reinstall under the same version.","Have deployment scripts treat this error as success/skip rather than aborting.","Clean stale chaincode files when reusing a shared install directory."],"tags":["chaincode","filesystem","fabric","duplicate-install"],"backgroundTag":"chaincode-already-installed","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"}