{"record":{"id":"ec20efa178aa556b","repo":"hyperledger/fabric","slug":"error-writing-file-to-package-s","errorCode":null,"errorMessage":"Error writing file to package: %s","messagePattern":"Error writing file to package: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/chaincode/platforms/util/writer.go","lineNumber":88,"sourceCode":"\n\t\t\tfileBytes, err := os.ReadFile(localpath)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\t// Validate metadata file for inclusion in tar\n\t\t\t// Validation is based on the fully qualified path of the file\n\t\t\terr = ccmetadata.ValidateMetadataFile(packagepath, fileBytes)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t} else { // file is not metadata, include in src\n\t\t\tpackagepath = path.Join(\"src\", packagepath)\n\t\t}\n\n\t\terr = WriteFileToPackage(localpath, packagepath, tw)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"Error writing file to package: %s\", err)\n\t\t}\n\n\t\tsuccess = true\n\t\treturn nil\n\t}\n\n\tif err := filepath.Walk(rootDirectory, walkFn); err != nil {\n\t\tlogger.Infof(\"Error walking rootDirectory: %s\", err)\n\t\treturn err\n\t}\n\n\tif !success {\n\t\treturn errors.Errorf(\"no source files found in '%s'\", srcPath)\n\t}\n\treturn nil\n}\n\n// WriteFileToPackage writes a file to a tar stream.","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/chaincode/platforms/util/writer.go#L70-L106","documentation":"Wraps any failure from WriteFileToPackage while walking a chaincode source folder into a tar package (WriteFolderToTarPackage's walkFn). The underlying error - typically from os.Open, Stat, tar header creation, or writing to the tar stream - is wrapped with this message identifying the file that failed.","triggerScenarios":"A source file is deleted between the filepath.Walk listing and the open (race); the file cannot be opened due to permissions; the tar.Writer was closed or its underlying stream errored (e.g. HTTP connection to the peer dropped mid-package); the file info/stat fails on a special file (device, symlink issues).","commonSituations":"Chaincode directory contains files being concurrently modified/removed during packaging; packaging over a network stream that breaks; unreadable files due to restrictive permissions or SELinux; disk full when writing the tar.","solutions":["Check the wrapped underlying error for the specific file and cause (permissions vs missing file vs tar write failure)","Verify read permissions on the chaincode source files (chmod/chown)","Exclude volatile/irrelevant directories (e.g. node_modules churn) via excludeDirs to avoid race windows","Retry packaging if files were being modified concurrently; ensure the tar writer's destination stream is healthy"],"exampleFix":"// before: packaging while node_modules is being written\nutil.WriteFolderToTarPackage(tw, src, nil, nil, nil)\n// after: exclude volatile directories and fix permissions\nerr := util.WriteFolderToTarPackage(tw, src, []string{\"node_modules\", \".git\"}, nil, nil)\nif err != nil {\n    os.Chmod(src, 0o755) // ensure readable\n}","handlingStrategy":"try-catch","validationCode":"// check source files are readable before packaging\nerr := filepath.Walk(srcPath, func(p string, info os.FileInfo, err error) error {\n    if err != nil { return err }\n    if !info.IsDir() {\n        f, err := os.Open(p)\n        if err != nil { return fmt.Errorf(\"unreadable: %s: %w\", p, err) }\n        f.Close()\n    }\n    return nil\n})","typeGuard":null,"tryCatchPattern":"// distinguish packaging failures and handle per-file\nif err != nil && strings.Contains(err.Error(), \"Error writing file to package\") {\n    log.Printf(\"packaging failed, cause: %v\", err) // wrapped underlying error\n    return err\n}","preventionTips":["Do not modify or delete chaincode files while packaging","Ensure read permissions on all source files","Keep the tar writer's destination stream open and healthy until packaging completes","Exclude volatile directories like node_modules from packaging"],"tags":["tar","file-io","packaging"],"backgroundTag":"file-write-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"}