{"record":{"id":"b4b52674d7a02217","repo":"plandex-ai/plandex","slug":"failed-to-open-file-s-w","errorCode":null,"errorMessage":"failed to open file %s: %w","messagePattern":"failed to open file (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/cli/lib/context_shared.go","lineNumber":180,"sourceCode":"\t\t\tmapMu.Unlock()\n\t\t\terrCh <- nil\n\t\t}(batch)\n\t}\n\n\tfor i := 0; i < len(mapInputBatches); i++ {\n\t\terr := <-errCh\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\treturn allMapBodies, nil\n}\n\nfunc readImageTokensForDefsOnly(path string, size int64, detail openai.ImageURLDetail, headerBytes int64) (int, error) {\n\tfile, err := os.Open(path)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to open file %s: %w\", path, err)\n\t}\n\tdefer file.Close()\n\n\ttokens, err := shared.GetImageTokensFromHeader(file, detail, headerBytes)\n\tif err != nil {\n\t\ttokens = shared.GetImageTokensEstimateFromBytes(size)\n\t}\n\treturn tokens, nil\n}\n\nfunc printSkippedFilesMsg(\n\tfilesSkippedTooLarge []filePathWithSize,\n\tfilesSkippedAfterSizeLimit []string,\n\tmapFilesTruncatedTooLarge []filePathWithSize,\n\tmapFilesSkippedAfterSizeLimit []string,\n) {\n\tfmt.Println()\n\tfmt.Println(getSkippedFilesMsg(filesSkippedTooLarge, filesSkippedAfterSizeLimit, mapFilesTruncatedTooLarge, mapFilesSkippedAfterSizeLimit))","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/cli/lib/context_shared.go#L162-L198","documentation":"readImageTokensForDefsOnly opens the image file with os.Open to read its header bytes for token estimation; an open failure is wrapped with this message using %w so the underlying error (e.g. *fs.PathError) is preserved for errors.Is/As checks. It means the image token estimator could not even open the file.","triggerScenarios":"os.Open(path) fails inside readImageTokensForDefsOnly — file does not exist, permission denied, path is a directory, or too many open files (EMFILE) during heavy batch reads. Called from getMapFileDetails for image files.","commonSituations":"Broken symlinks to images; images moved/deleted between path resolution and read; hitting the process open-file limit when mapping thousands of files.","solutions":["Verify the path exists and is a regular readable file (not a directory or broken symlink)","Fix permissions on the image file","If mapping many files concurrently and seeing 'too many open files', raise ulimit -n or reduce concurrency"],"exampleFix":"// before\nfile, err := os.Open(path)\nif err != nil {\n    return 0, fmt.Errorf(\"failed to open file %s: %w\", path, err)\n}\n// after — clearer diagnostics for common causes\nfile, err := os.Open(path)\nif err != nil {\n    if os.IsNotExist(err) {\n        return 0, fmt.Errorf(\"image file %s does not exist\", path)\n    }\n    if os.IsPermission(err) {\n        return 0, fmt.Errorf(\"permission denied reading image %s\", path)\n    }\n    return 0, fmt.Errorf(\"failed to open file %s: %w\", path, err)\n}","handlingStrategy":"validation","validationCode":"info, err := os.Stat(path)\nif err != nil {\n    return fmt.Errorf(\"image %s missing: %w\", path, err)\n}\nif info.IsDir() {\n    return fmt.Errorf(\"%s is a directory, not an image\", path)\n}\nprobe, err := os.Open(path)\nif err != nil {\n    return fmt.Errorf(\"image %s not readable: %w\", path, err)\n}\nprobe.Close()","typeGuard":"func canOpenFile(path string) bool {\n    f, err := os.Open(path)\n    if err != nil { return false }\n    f.Close()\n    return true\n}","tryCatchPattern":"file, err := os.Open(path)\nif err != nil {\n    var pathErr *fs.PathError\n    if errors.As(err, &pathErr) && errors.Is(err, syscall.EMFILE) {\n        return 0, fmt.Errorf(\"too many open files; reduce concurrency\")\n    }\n    return 0, fmt.Errorf(\"failed to open file %s: %w\", path, err)\n}","preventionTips":["Raise the open-file ulimit when processing thousands of files","Fix broken symlinks before mapping","Verify image paths exist right before token estimation"],"tags":["filesystem","image","io"],"backgroundTag":"file-open-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}