{"record":{"id":"b2435b60b926d222","repo":"apache/hadoop","slug":"is-a-directory-b2435b","errorCode":null,"errorMessage":"Is a directory","messagePattern":"Is a directory","errorType":"exception","errorClass":"PathIsDirectoryException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/PathData.java","lineNumber":231,"sourceCode":"\n  /**\n   * Ensure that the file exists and if it is or is not a directory\n   * @param typeRequirement Set it to the desired requirement.\n   * @throws PathIOException if file doesn't exist or the type does not match\n   * what was specified in typeRequirement.\n   */\n  private void checkIfExists(FileTypeRequirement typeRequirement) \n  throws PathIOException {\n    if (!exists) {\n      throw new PathNotFoundException(toString());      \n    }\n\n    if ((typeRequirement == FileTypeRequirement.SHOULD_BE_DIRECTORY)\n       && !stat.isDirectory()) {\n      throw new PathIsNotDirectoryException(toString());\n    } else if ((typeRequirement == FileTypeRequirement.SHOULD_NOT_BE_DIRECTORY)\n              && stat.isDirectory()) {\n      throw new PathIsDirectoryException(toString());\n    }\n  }\n  \n  /**\n   * Returns a new PathData with the given extension.\n   * @param extension for the suffix\n   * @return PathData\n   * @throws IOException shouldn't happen\n   */\n  public PathData suffix(String extension) throws IOException {\n    return new PathData(fs, this+extension);\n  }\n\n  /**\n   * Test if the parent directory exists\n   * @return boolean indicating parent exists\n   * @throws IOException upon unexpected error\n   */","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/PathData.java#L213-L249","documentation":"PathData.checkIfExists (PathData.java:231) throws PathIsDirectoryException ('Is a directory') when the path exists, the SHOULD_NOT_BE_DIRECTORY requirement was requested, but stat.isDirectory() is true. Note: in this tree the enum literal is defined and checked, but every current in-class caller (getDirectoryContents, getDirectoryContentsIterator, getPathDataForChild) passes SHOULD_BE_DIRECTORY, so this specific line is a defensive/legacy branch kept for callers that require a plain file.","triggerScenarios":"A (present or future) caller invoking checkIfExists(SHOULD_NOT_BE_DIRECTORY) on a path that turns out to be a directory — the shape used by file-oriented commands (cat, checksum, tail, chmod-on-file) that must refuse directories. The user-visible 'Is a directory' failures from shell commands come from this exception family (PathIsDirectoryException is also thrown directly by FileSystem/FileContext-consuming code).","commonSituations":"'hadoop fs -cat /some/dir' style operations on a directory; passing a directory to an API whose implementation requires a regular file (e.g., opening an InputStream); paths that used to be files and were replaced by directories between runs.","solutions":["Point the command at a file inside the directory, or use the directory-appropriate command (e.g. -ls, or -get for the whole dir)","Verify type before the call: 'hadoop fs -test -d <path>' and branch on it","In code, guard with FileStatus.isDirectory() before file-only operations"],"exampleFix":"# before\nhadoop fs -cat /data/logs       # /data/logs is a directory -> Is a directory\n\n# after\nhadoop fs -ls /data/logs        # list, or fetch a specific file:\nhadoop fs -cat /data/logs/app.log","handlingStrategy":"type-guard","validationCode":"FileStatus st = fs.getFileStatus(p);\nif (st.isDirectory()) {\n  throw new IllegalStateException(\"expected plain file: \" + p);\n}","typeGuard":"boolean isPlainFile(FileSystem fs, Path p) throws IOException {\n  FileStatus st = fs.getFileStatus(p);\n  return st.isFile() && !st.isSymlink();\n}","tryCatchPattern":"catch (PathIsDirectoryException e) { target a specific file inside the directory or switch to a directory-appropriate command }","preventionTips":["Branch on 'hadoop fs -test -d' before file-only commands","Check FileStatus.isDirectory() before opening streams","Watch for paths repurposed from file to directory between runs"],"tags":["is-a-directory","type-mismatch","pathdata","hadoop-shell"],"backgroundTag":"path-is-a-directory","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}