{"record":{"id":"40e3496027556937","repo":"wavetermdev/waveterm","slug":"initializing-file-with-empty-write-w","errorCode":null,"errorMessage":"initializing file with empty write: %w","messagePattern":"initializing file with empty write: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/wsh/cmd/wshcmd-file-util.go","lineNumber":57,"sourceCode":"\t\tinfo, err = wshclient.FileInfoCommand(RpcClient, fileData, &wshrpc.RpcOpts{Timeout: fileTimeout})\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"getting file info: %w\", err)\n\t\t}\n\t\treturn info, err\n\t}\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"getting file info: %w\", err)\n\t}\n\treturn info, nil\n}\n\nfunc streamWriteToFile(fileData wshrpc.FileData, reader io.Reader) error {\n\t// First truncate the file with an empty write\n\temptyWrite := fileData\n\temptyWrite.Data64 = \"\"\n\terr := wshclient.FileWriteCommand(RpcClient, emptyWrite, &wshrpc.RpcOpts{Timeout: fileTimeout})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"initializing file with empty write: %w\", err)\n\t}\n\n\tconst chunkSize = wshrpc.FileChunkSize // 32KB chunks\n\tbuf := make([]byte, chunkSize)\n\ttotalWritten := int64(0)\n\n\tfor {\n\t\tn, err := reader.Read(buf)\n\t\tif err == io.EOF {\n\t\t\tbreak\n\t\t}\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"reading input: %w\", err)\n\t\t}\n\n\t\t// Check total size\n\t\ttotalWritten += int64(n)\n\t\tif totalWritten > MaxFileSize {","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/cmd/wsh/cmd/wshcmd-file-util.go#L39-L75","documentation":"streamWriteToFile truncates the destination by issuing a FileWriteCommand with empty data (Data64 = \"\") before streaming chunks. If this initial write RPC fails, the error is wrapped as \"initializing file with empty write\" — nothing was streamed yet.","triggerScenarios":"First FileWriteCommand in `wsh cp -`/append-style streaming fails: destination file not writable, file is actually a directory, remote connection down, or RPC timeout (fileTimeout).","commonSituations":"Piping data to a read-only or protected remote path; destination path points at a directory; SSH connection dropped right as the write started.","solutions":["Check write permission on the destination file and its directory.","Confirm the destination is a file, not a directory, and the path is correct.","Verify the remote connection is alive and retry the command.","Increase fileTimeout for slow links."],"exampleFix":"// before\ncat data.bin | wsh cp - /remote/readonly/data.bin\n// after\ncat data.bin | wsh cp - /remote/home/user/data.bin","handlingStrategy":"validation","validationCode":"if info, err := wshclient.FileInfoCommand(RpcClient, fileData, &wshrpc.RpcOpts{Timeout: fileTimeout}); err == nil && info.IsDir {\n    return fmt.Errorf(\"destination %s is a directory\", fileData.Info.Path)\n}","typeGuard":null,"tryCatchPattern":"if err := streamWriteToFile(fileData, reader); err != nil {\n    if strings.Contains(err.Error(), \"initializing file with empty write\") {\n        // check destination permissions/type before retrying\n    }\n}","preventionTips":["Confirm the destination path is a writable file, not a directory","Check remote disk quota/free space before large writes","Verify the connection is up before starting a stream"],"tags":["filesystem","rpc","permissions"],"backgroundTag":"file-write-failed","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}