{"record":{"id":"98e315f4b4b2a4b6","repo":"yorukot/superfile","slug":"failed-to-stat-source-w","errorCode":null,"errorMessage":"failed to stat source: %w","messagePattern":"failed to stat source: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/internal/file_operations.go","lineNumber":81,"sourceCode":"\t// If on different partitions or rename failed, fall back to copy+delete\n\terr = copyElement(src, dst)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to copy: %w\", err)\n\t}\n\n\terr = os.RemoveAll(src)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to remove source after copy: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// copyElement handles copying of both files and directories\nfunc copyElement(src, dst string) error {\n\tsrcInfo, err := os.Stat(src)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to stat source: %w\", err)\n\t}\n\n\tif srcInfo.IsDir() {\n\t\treturn copyDir(src, dst, srcInfo)\n\t}\n\treturn copyFile(src, dst, srcInfo)\n}\n\n// copyDir recursively copies a directory\nfunc copyDir(src, dst string, srcInfo os.FileInfo) error {\n\terr := os.MkdirAll(dst, srcInfo.Mode())\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create destination directory: %w\", err)\n\t}\n\n\tentries, err := os.ReadDir(src)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to read source directory: %w\", err)","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/yorukot/superfile/blob/b72f550bc6e75913f48eb49fdd258e1a2df2fe88/src/internal/file_operations.go#L63-L99","documentation":"copyElement starts by calling os.Stat(src) to learn whether the source is a file or directory; any Stat failure is wrapped as 'failed to stat source'. It means the source path does not exist or is inaccessible before any copying begins.","triggerScenarios":"moveElement -> copyElement where src was deleted/moved between the partition check and the copy, or src never existed / lacks parent-directory execute permission.","commonSituations":"Race where another process removed the source during the move; symlink to a nonexistent target; path typo; running without traverse permission on a parent directory.","solutions":["Verify the source path exists (ls / os.Stat) before calling the move/copy API.","Fix parent directory permissions so the path is traversable.","If the source was a broken symlink, remove it or point it at a real target.","Retry if this was a transient race; serialize concurrent operations on the same tree."],"exampleFix":"// before\nif _, err := os.Stat(src); err != nil { return } // not checked by caller\n// after\nif _, err := os.Stat(src); err == nil {\n    moveElement(src, dst)\n}","handlingStrategy":"validation","validationCode":"srcInfo, err := os.Stat(src)\nif err != nil {\n    return fmt.Errorf(\"refusing to copy: source %q unavailable: %w\", src, err)\n}\n_ = srcInfo // safe to proceed to copyElement","typeGuard":"func sourceReady(src string) bool {\n    st, err := os.Stat(src)\n    return err == nil && (st.Mode().Perm()&0444 != 0 || st.IsDir())\n}","tryCatchPattern":"if err := copyElement(src, dst); err != nil {\n    var pErr *fs.PathError\n    if errors.As(err, &pErr) && errors.Is(pErr.Err, fs.ErrNotExist) {\n        log.Errorf(\"Source vanished: %s\", pErr.Path)\n        return fs.ErrNotExist // skip gracefully instead of failing the batch\n    }\n    return err\n}","preventionTips":["Stat the source immediately before copy to shrink the race window.","Resolve symlinks (filepath.EvalSymlinks) and check targets exist.","Avoid concurrent writers/deleters on the tree being copied.","Check parent-directory execute (x) permissions, not just the file itself."],"tags":["filesystem","copy","stat","paths"],"backgroundTag":"no-such-file-or-directory","analyzedSha":"b72f550bc6e75913f48eb49fdd258e1a2df2fe88","analyzedAt":"2026-09-01T04:38:08.254Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}