{"record":{"id":"ae6bfe637caf2812","repo":"hyperledger/fabric","slug":"chaincode-s-exists-ae6bfe","errorCode":null,"errorMessage":"chaincode %s exists","messagePattern":"chaincode (.+?) exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/common/ccprovider/sigcdspackage.go","lineNumber":311,"sourceCode":"\tif ccpack.env == nil {\n\t\treturn errors.New(\"env cannot be nil if buf and depspec are 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":293,"sourceCodeEnd":320,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/common/ccprovider/sigcdspackage.go#L293-L320","documentation":"PutChaincodeToFS writes the chaincode package to chaincodeInstallPath/<name>.<version> and refuses to overwrite an existing installation. If os.Stat finds the target file already present, this error is returned so an existing chaincode package is never silently replaced.","triggerScenarios":"Installing the same chaincode name+version twice on the same peer, e.g., running `peer chaincode install` for an already-installed version, or reinstalling after a failed deployment without changing the version.","commonSituations":"Re-running an install script idempotently, retrying a failed upgrade while keeping the same version label, or a peer whose filesystem already contains packages from a prior deployment.","solutions":["Verify the chaincode with `peer chaincode list --installed`; if already present, skip the install step","Bump the chaincode version and package a new version before reinstalling","Delete the stale package file at the reported path (or wipe the peer's chaincode install directory) only if the existing package is no longer needed, then retry"],"exampleFix":"// before\npeer chaincode install cc.out  # version 1.0, already installed -> error\n\n// after\n# bump version and package again, or guard in script:\nif ! peer chaincode list --installed | grep -q 'mycc@1.0'; then\n  peer chaincode install cc.out\nfi","handlingStrategy":"try-catch","validationCode":"path := filepath.Join(chaincodeInstallPath, ccname+\".\"+ccversion)\nif _, err := os.Stat(path); err == nil {\n  return fmt.Errorf(\"chaincode %s already installed; skip or bump version\", path)\n}","typeGuard":"func chaincodeInstalled(installPath, name, version string) bool {\n  _, err := os.Stat(filepath.Join(installPath, name+\".\"+version))\n  return err == nil\n}","tryCatchPattern":"err := ccprovider.PutChaincodeToFS(pack)\nif err != nil {\n  var pathErr *os.PathError\n  if strings.Contains(err.Error(), \"exists\") {\n    log.Printf(\"already installed, treating as success\")\n    return nil // idempotent install\n  }\n  return err\n}","preventionTips":["Check installed chaincode list before each install","Make install scripts idempotent (skip when name@version exists)","Always bump the version for each new deployment"],"tags":["chaincode-install","duplicate","fabric"],"backgroundTag":"resource-already-exists","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"}