{"record":{"id":"0e3f2ae55b509060","repo":"apache/hadoop","slug":"path-is-not-a-directory-0e3f2a","errorCode":null,"errorMessage":"Path is not a directory: {}","messagePattern":"Path is not a directory: (.+?)","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirMkdirOp.java","lineNumber":57,"sourceCode":"import java.util.Optional;\n\nimport static org.apache.hadoop.util.Time.now;\n\nclass FSDirMkdirOp {\n\n  static FileStatus mkdirs(FSNamesystem fsn, FSPermissionChecker pc, String src,\n      PermissionStatus permissions, boolean createParent) throws IOException {\n    FSDirectory fsd = fsn.getFSDirectory();\n    if(NameNode.stateChangeLog.isDebugEnabled()) {\n      NameNode.stateChangeLog.debug(\"DIR* NameSystem.mkdirs: \" + src);\n    }\n    fsd.writeLock();\n    try {\n      INodesInPath iip = fsd.resolvePath(pc, src, DirOp.CREATE);\n\n      final INode lastINode = iip.getLastINode();\n      if (lastINode != null && lastINode.isFile()) {\n        throw new FileAlreadyExistsException(\"Path is not a directory: \" + src);\n      }\n\n      if (lastINode == null) {\n        if (fsd.isPermissionEnabled()) {\n          fsd.checkAncestorAccess(pc, iip, FsAction.WRITE);\n        }\n\n        if (!createParent) {\n          fsd.verifyParentDir(iip);\n        }\n\n        // validate that we have enough inodes. This is, at best, a\n        // heuristic because the mkdirs() operation might need to\n        // create multiple inodes.\n        fsn.checkFsObjectLimit();\n\n        // Ensure that the user can traversal the path by adding implicit\n        // u+wx permission to all ancestor directories.","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirMkdirOp.java#L39-L75","documentation":"FileAlreadyExistsException from FSDirMkdirOp.mkdirs when the target path's final component already exists as a file - HDFS refuses to mkdir over a file. An existing directory at the same path is fine (mkdir succeeds as a no-op); only a file at the final component collides.","triggerScenarios":"`hdfs dfs -mkdir /tmp/out` where /tmp/out is an existing file (e.g., a former _SUCCESS-style marker or previous output file); `mkdir -p` where the last component is a file; job restart writing to a path now occupied by a file.","commonSituations":"MapReduce/Spark output directories that were previously written as files; a rename leaving a file on the intended directory path; shared scratch/temp paths reused across services.","solutions":["Remove or rename the conflicting file: `hdfs dfs -rm <path>` (or `-mv` it aside), then mkdir.","Or write to a fresh path (append a run id) when the old file must be kept.","Pre-check in code: only call mkdirs when the path is absent or already a directory."],"exampleFix":"// before\nfs.mkdirs(new Path(\"/jobs/run42\")); // FileAlreadyExistsException: /jobs/run42 is a file\n\n// after\nPath p = new Path(\"/jobs/run42\");\nif (fs.exists(p) && !fs.getFileStatus(p).isDirectory()) {\n  fs.delete(p, false);            // or pick /jobs/run42_r2\n}\nfs.mkdirs(p);","handlingStrategy":"validation","validationCode":"if (fs.exists(p) && !fs.getFileStatus(p).isDirectory()) {\n  throw new FileAlreadyExistsException(\"A file exists at \" + p\n      + \"; remove it or choose a different path\");\n}\nfs.mkdirs(p);","typeGuard":null,"tryCatchPattern":"try {\n  fs.mkdirs(p);\n} catch (FileAlreadyExistsException e) {\n  FileStatus st = fs.getFileStatus(p);\n  if (!st.isDirectory()) {\n    fs.delete(p, false); // only if disposable, then:\n    fs.mkdirs(p);\n  }\n}","preventionTips":["Use unique per-run output paths (job id suffix) instead of fixed ones.","Clean or archive previous outputs before reruns.","Check isDirectory (not just exists) before mkdir in shared scratch areas."],"tags":["hdfs","mkdir","file-already-exists","filesystem"],"backgroundTag":"file-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}