{"record":{"id":"7eb5a9533766cb7d","repo":"wavetermdev/waveterm","slug":"failed-to-stat-file-w-7eb5a9","errorCode":null,"errorMessage":"failed to stat file: %w","messagePattern":"failed to stat file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/util/fileutil/fileutil.go","lineNumber":341,"sourceCode":"\t\t\t}\n\t\t\tresults[i].Applied = false\n\t\t\tresults[i].Error = \"previous edit failed\"\n\t\t\tcontinue\n\t\t}\n\n\t\tmodifiedContents, results[i] = applyEdit(modifiedContents, edit, i)\n\t\tif !results[i].Applied {\n\t\t\tfailed = true\n\t\t}\n\t}\n\n\treturn modifiedContents, results\n}\n\nfunc ReplaceInFile(filePath string, edits []EditSpec) error {\n\tfileInfo, err := os.Stat(filePath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to stat file: %w\", err)\n\t}\n\n\tif !fileInfo.Mode().IsRegular() {\n\t\treturn fmt.Errorf(\"not a regular file: %s\", filePath)\n\t}\n\n\tif fileInfo.Size() > MaxEditFileSize {\n\t\treturn fmt.Errorf(\"file too large for editing: %d bytes (max: %d)\", fileInfo.Size(), MaxEditFileSize)\n\t}\n\n\tcontents, err := os.ReadFile(filePath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to read file: %w\", err)\n\t}\n\n\tmodifiedContents, err := ApplyEdits(contents, edits)\n\tif err != nil {\n\t\treturn err","sourceCodeStart":323,"sourceCodeEnd":359,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/util/fileutil/fileutil.go#L323-L359","documentation":"Returned when os.Stat (or an equivalent filesystem stat call) fails for the target file, e.g. because it does not exist, permissions are insufficient, or a path component is not a directory. The underlying error is wrapped with %w.","triggerScenarios":"Calling ReplaceInFile (directly or via editTextFileCallback / ReplaceInAppFile) with a path that does not exist, has a broken symlink, or whose parent directories deny traversal permission.","commonSituations":"Typo in the file path; editing a file that was deleted or moved; dangling symlink; running without permission on a protected path; case-sensitivity mismatches on Linux paths.","solutions":["Verify the path exists (ls / os.Stat) and correct typos.","Resolve or remove dangling symlinks before editing.","Check directory permissions (execute bit on parent dirs) for the current user.","Create the file first if it may not exist, or skip editing when os.Stat returns NotExist."],"exampleFix":"// before\nerr := fileutil.ReplaceInFile(\"/app/config.json\", edits) // file missing\n\n// after\nif _, err := os.Stat(path); os.IsNotExist(err) {\n    return fmt.Errorf(\"cannot replace in missing file %s\", path)\n}\nerr := fileutil.ReplaceInFile(path, edits)","handlingStrategy":"validation","validationCode":"func ensureEditableFile(path string) error {\n    fi, err := os.Stat(path)\n    if err != nil {\n        return fmt.Errorf(\"cannot edit %s: %w\", path, err)\n    }\n    if !fi.Mode().IsRegular() {\n        return fmt.Errorf(\"%s is not a regular file\", path)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// Go: distinguish not-exist from other stat failures\nif err := fileutil.ReplaceInFile(path, edits); err != nil {\n    if strings.Contains(err.Error(), \"failed to stat file\") {\n        if _, serr := os.Stat(path); os.IsNotExist(serr) {\n            // create the file or abort\n        }\n    }\n}","preventionTips":["Stat the path before editing; handle IsNotExist explicitly","Validate user-supplied paths against known file locations","Resolve symlinks and check the final target exists","Watch for case-sensitivity differences on Linux filesystems"],"tags":["go","filesystem","stat"],"backgroundTag":"file-not-found","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}