hyperledger/fabric · error
failed to write %s as %s: %s
Error message
failed to write %s as %s: %s
What it means
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.
Source
Thrown at core/chaincode/platforms/util/writer.go:145
header.AccessTime = zeroTime
header.ModTime = zeroTime
header.ChangeTime = zeroTime
header.Name = packagepath
header.Mode = 0o100644
header.Uid = 500
header.Gid = 500
header.Uname = ""
header.Gname = ""
err = tw.WriteHeader(header)
if err != nil {
return fmt.Errorf("failed to write header for %s: %s", localpath, err)
}
is := bufio.NewReader(fd)
_, err = io.Copy(tw, is)
if err != nil {
return fmt.Errorf("failed to write %s as %s: %s", localpath, packagepath, err)
}
return nil
}
View on GitHub (pinned to 2736b63f8f)
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
Example fix
// before goutil.WriteFileToPackage(srcOnNfs, pkgPath, tw) // truncated package on NFS hiccup // after localCopy := filepath.Join(os.TempDir(), filepath.Base(srcOnNfs)) copyFile(srcOnNfs, localCopy) // snapshot to stable local disk err := util.WriteFileToPackage(localCopy, pkgPath, tw) // then verify: tar -tf pkgPath
Defensive patterns
Strategy: try-catch
Validate before calling
// pre-check source readability and destination free space
if fi, err := os.Stat(localpath); err != nil || !fi.Mode().IsRegular() {
return fmt.Errorf("bad package input %s", localpath)
}
if err := checkDiskSpace(filepath.Dir(pkgPath), 256<<20); err != nil { return err } Type guard
func isReadableRegular(path string) bool {
f, err := os.Open(path)
if err != nil { return false }
f.Close()
return true
} Try / catch
if err := util.WriteFileToPackage(localpath, pkgPath, tw); err != nil {
if strings.Contains(err.Error(), "failed to write") {
os.Remove(pkgPath) // discard truncated package
return fmt.Errorf("package %s incomplete (source %s): %w", pkgPath, localpath, err)
}
return err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: Disk full on the volume holding the chaincode package; source file being rebuilt while packaging; network file system dropping the connection mid-read.
Related errors
- failed to write header for %s: %s
- failed calculating FileInfoHeader: %s
- Recv() error: %v, closing connection
- chaincode stream terminated
- illegal file name in payload: %s
AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04).
Data as JSON: /api/errors/21155ae44e872e90.
Report an issue: GitHub.