{"record":{"id":"65dd93ec6707e204","repo":"charmbracelet/crush","slug":"failed-to-write-file-w-65dd93","errorCode":null,"errorMessage":"failed to write file: %w","messagePattern":"failed to write file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/agent/tools/lsp_replace_symbol.go","lineNumber":161,"sourceCode":"\t\t\t\t\t\tNewContent: newContent,\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn fantasy.ToolResponse{}, fmt.Errorf(\"permission request failed: %w\", err)\n\t\t\t\t}\n\t\t\t\tif !granted {\n\t\t\t\t\treturn NewPermissionDeniedResponse(), nil\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif files != nil && sessionID != \"\" {\n\t\t\t\tif _, err := files.CreateVersion(ctx, sessionID, params.FilePath, string(content)); err != nil {\n\t\t\t\t\tslog.Warn(\"Failed to create file version before replace\", \"path\", params.FilePath, \"error\", err)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif err := os.WriteFile(params.FilePath, []byte(newContent), 0o644); err != nil {\n\t\t\t\treturn fantasy.ToolResponse{}, fmt.Errorf(\"failed to write file: %w\", err)\n\t\t\t}\n\n\t\t\tif filetracker != nil && sessionID != \"\" {\n\t\t\t\tfiletracker.RecordRead(ctx, sessionID, params.FilePath)\n\t\t\t}\n\n\t\t\tnotifyLSPs(ctx, lspManager, params.FilePath)\n\n\t\t\tvar summary string\n\t\t\tswitch action {\n\t\t\tcase \"replace\":\n\t\t\t\tsummary = fmt.Sprintf(\"Replaced symbol '%s' in %s (lines %d-%d)\", params.Symbol, params.FilePath, startLine+1, endLine+1)\n\t\t\tcase \"add_before\":\n\t\t\t\tsummary = fmt.Sprintf(\"Inserted before symbol '%s' in %s (before line %d)\", params.Symbol, params.FilePath, startLine+1)\n\t\t\tcase \"add_after\":\n\t\t\t\tsummary = fmt.Sprintf(\"Inserted after symbol '%s' in %s (after line %d)\", params.Symbol, params.FilePath, endLine+1)\n\t\t\tcase \"delete\":\n\t\t\t\tsummary = fmt.Sprintf(\"Deleted symbol '%s' from %s (lines %d-%d)\", params.Symbol, params.FilePath, startLine+1, endLine+1)","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/agent/tools/lsp_replace_symbol.go#L143-L179","documentation":"This error is returned when os.WriteFile fails to persist the new content to params.FilePath after the symbol replacement was computed and permission was granted. Common wrapped causes are read-only filesystem, permission denied, disk full, or the parent directory no longer existing.","triggerScenarios":"os.WriteFile returns an error during lsp_replace_symbol: file made read-only between read and write, disk quota/full, filesystem mounted read-only (container), or the file was removed by another process.","commonSituations":"Editor or another process locked/changed permissions on the file; running in a read-only container FS; ENOSPC on a full disk; saving to a path outside a writable mount.","solutions":["Check the wrapped OS error (EACCES, ENOSPC, EROFS) and address the specific cause.","Verify the file and directory are writable: ls -l and touch the file as the same user.","Free disk space or raise quota if ENOSPC.","Restore the file if it was deleted between read and write, then re-run the tool."],"exampleFix":"// before\nif err := os.WriteFile(params.FilePath, []byte(newContent), 0o644); err != nil {\n\treturn fantasy.ToolResponse{}, fmt.Errorf(\"failed to write file: %w\", err)\n}\n// after\nif err := os.WriteFile(params.FilePath, []byte(newContent), 0o644); err != nil {\n\tslog.Error(\"Failed to write file after symbol replace\", \"path\", params.FilePath, \"error\", err)\n\treturn fantasy.ToolResponse{}, fmt.Errorf(\"failed to write file: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"if err := unix.Access(filePath, unix.W_OK); err != nil {\n\treturn fmt.Errorf(\"file not writable: %s: %w\", filePath, err)\n}","typeGuard":null,"tryCatchPattern":"if err := os.WriteFile(path, data, 0o644); err != nil {\n\tswitch {\n\tcase errors.Is(err, fs.ErrPermission):\n\t\t// fix permissions or choose another path\n\tcase errors.Is(err, syscall.ENOSPC):\n\t\t// free disk space\n\t}\n\treturn err\n}","preventionTips":["Verify write permission on files before requesting symbol replacement.","Keep target directories writable and monitor disk space.","Avoid running in read-only container filesystems for editing workflows.","Close editors/processes that lock or chmod files mid-session."],"tags":["filesystem","go","file-write"],"backgroundTag":"file-write-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}