{"record":{"id":"4021b3c3e6ee2be2","repo":"kovidgoyal/kitty","slug":"failed-to-read-the-directory-s-with-error-w","errorCode":null,"errorMessage":"failed to read the directory %s with error: %w","messagePattern":"failed to read the directory (.+?) with error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"kittens/choose_files/preview.go","lineNumber":173,"sourceCode":"\t\t}\n\t\ttype_map := make(map[string]entry, len(entries))\n\t\tfor _, e := range entries {\n\t\t\ttype_map[e.Name()] = entry{strings.ToLower(e.Name()), e.Type()}\n\t\t}\n\t\tnames := utils.Map(func(e fs.DirEntry) string { return e.Name() }, entries)\n\t\tslices.SortFunc(names, func(a, b string) int { return strings.Compare(type_map[a].lname, type_map[b].lname) })\n\t\tfmt.Fprintln(&buf, \"Contents:\")\n\t\tfor _, n := range names {\n\t\t\ttrailers = append(trailers, icons.IconForFileWithMode(n, type_map[n].ftype, false)+\"  \"+sanitize(n))\n\t\t}\n\t}\n\treturn buf.String(), trailers\n}\n\nfunc NewDirectoryPreview(abspath string, metadata fs.FileInfo) Preview {\n\tentries, err := os.ReadDir(abspath)\n\tif err != nil {\n\t\treturn NewErrorPreview(fmt.Errorf(\"failed to read the directory %s with error: %w\", abspath, err))\n\t}\n\ttitle := icons.IconForFileWithMode(\"dir\", fs.ModeDir, false) + \"  Directory\\n\"\n\theader, extra := write_file_metadata(abspath, metadata, entries)\n\treturn &MessagePreview{title: title, msg: header, trailers: extra}\n}\n\nfunc NewFileMetadataPreview(abspath string, metadata fs.FileInfo) *MessagePreview {\n\text := filepath.Ext(abspath)\n\tif ext == \"\" {\n\t\text = \"File\"\n\t}\n\ttitle := icons.IconForFileWithMode(filepath.Base(abspath), metadata.Mode().Type(), false) + \"  \" + ext\n\th, t := write_file_metadata(abspath, metadata, nil)\n\treturn &MessagePreview{title: title, msg: h, trailers: t}\n}\n\nfunc NewFileMetadataPreviewWithError(abspath string, metadata fs.FileInfo, err error) *MessagePreview {\n\text := filepath.Ext(abspath)","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/kovidgoyal/kitty/blob/6d5d0c440603ad9bdf6dcd599f73f6dde21acb44/kittens/choose_files/preview.go#L155-L191","documentation":"NewDirectoryPreview could not os.ReadDir the directory being previewed, and returns an error preview instead of a listing. The path exists per earlier stat, but reading it failed at this moment.","triggerScenarios":"preview_for -> NewDirectoryPreview where os.ReadDir(abspath) errors: permission denied on execute/search of the directory, directory removed between selection and preview, or special filesystems (e.g. /proc entries, broken FUSE mounts).","commonSituations":"Previewing a directory without +x permission; directory deleted while the chooser is open; NFS/FUSE mount hiccups; dangling symlinks to directories.","solutions":["Check permissions on the directory (need read+execute for listing)","If the dir was deleted, refresh the file list in the chooser","For FUSE/network mounts, verify the mount is alive"],"exampleFix":"// before\nentries, err := os.ReadDir(abspath)\n// after\nentries, err := os.ReadDir(abspath)\nif err != nil {\n    if errors.Is(err, fs.ErrPermission) {\n        return NewErrorPreview(fmt.Errorf(\"no permission to list %s\", abspath))\n    }\n    return NewErrorPreview(fmt.Errorf(\"failed to read the directory %s with error: %w\", abspath, err))\n}","handlingStrategy":"try-catch","validationCode":"if f, err := os.Stat(dir); err != nil || !f.IsDir() { /* skip preview */ }","typeGuard":null,"tryCatchPattern":"if err := os.ReadDir(dir); err != nil { if errors.Is(err, fs.ErrPermission) { show 'no permission' preview } else { show generic error preview } }","preventionTips":["Check r+x permissions before listing","Handle directories that vanish while a chooser session is open"],"tags":["kitty","filesystem","permissions","directory-preview"],"backgroundTag":"permission-denied","analyzedSha":"6d5d0c440603ad9bdf6dcd599f73f6dde21acb44","analyzedAt":"2026-08-27T14:20:20.142Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}