{"record":{"id":"21155ae44e872e90","repo":"hyperledger/fabric","slug":"failed-to-write-s-as-s-s","errorCode":null,"errorMessage":"failed to write %s as %s: %s","messagePattern":"failed to write (.+?) as (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/chaincode/platforms/util/writer.go","lineNumber":145,"sourceCode":"\theader.AccessTime = zeroTime\n\theader.ModTime = zeroTime\n\theader.ChangeTime = zeroTime\n\theader.Name = packagepath\n\theader.Mode = 0o100644\n\theader.Uid = 500\n\theader.Gid = 500\n\theader.Uname = \"\"\n\theader.Gname = \"\"\n\n\terr = tw.WriteHeader(header)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to write header for %s: %s\", localpath, err)\n\t}\n\n\tis := bufio.NewReader(fd)\n\t_, err = io.Copy(tw, is)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to write %s as %s: %s\", localpath, packagepath, err)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":127,"sourceCodeEnd":150,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/chaincode/platforms/util/writer.go#L127-L150","documentation":"The final step copies file contents into the tar stream with io.Copy(tw, is). This error means reading localpath or writing to the tar stream failed mid-copy, producing a truncated/corrupt package. Message format: local path, package path, underlying error.","triggerScenarios":"io.Copy fails: file shrank or read error while streaming (file modified/truncated concurrently), writer-side failure (disk full, network stream broken), or fd closed early.","commonSituations":"Disk full on the volume holding the chaincode package; source file being rebuilt while packaging; network file system dropping the connection mid-read.","solutions":["Check the wrapped error for ENOSPC/disk-full and free space on the package destination","Re-run packaging after ensuring the source tree is quiescent (no concurrent builds/deletes)","If sources are on NFS, copy to local disk first, then package; validate the resulting tar (tar -tf) before use"],"exampleFix":"// before\ngoutil.WriteFileToPackage(srcOnNfs, pkgPath, tw) // truncated package on NFS hiccup\n// after\nlocalCopy := filepath.Join(os.TempDir(), filepath.Base(srcOnNfs))\ncopyFile(srcOnNfs, localCopy) // snapshot to stable local disk\nerr := util.WriteFileToPackage(localCopy, pkgPath, tw)\n// then verify: tar -tf pkgPath","handlingStrategy":"try-catch","validationCode":"// pre-check source readability and destination free space\nif fi, err := os.Stat(localpath); err != nil || !fi.Mode().IsRegular() {\n    return fmt.Errorf(\"bad package input %s\", localpath)\n}\nif err := checkDiskSpace(filepath.Dir(pkgPath), 256<<20); err != nil { return err }","typeGuard":"func isReadableRegular(path string) bool {\n    f, err := os.Open(path)\n    if err != nil { return false }\n    f.Close()\n    return true\n}","tryCatchPattern":"if err := util.WriteFileToPackage(localpath, pkgPath, tw); err != nil {\n    if strings.Contains(err.Error(), \"failed to write\") {\n        os.Remove(pkgPath) // discard truncated package\n        return fmt.Errorf(\"package %s incomplete (source %s): %w\", pkgPath, localpath, err)\n    }\n    return err\n}","preventionTips":["Freeze the source tree during packaging (no concurrent builds)","Monitor disk space on the package destination volume","Snapshot network-mounted sources to local disk before packaging","Validate the produced tar (tar -tf) before distributing the package"],"tags":["go","io","tar","stream","chaincode"],"backgroundTag":"io-copy-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"}