{"record":{"id":"591728943e7e861c","repo":"apache/hadoop","slug":"parent-is-not-a-dir","errorCode":null,"errorMessage":"parent is not a dir","messagePattern":"parent is not a dir","errorType":"exception","errorClass":"ParentNotDirectoryException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java","lineNumber":1405,"sourceCode":"   * @param f the path.\n   * @param absolutePermission permission.\n   * @param createParent create parent.\n   * @throws IOException IO failure.\n   */\n  @Deprecated\n  protected void primitiveMkdir(Path f, FsPermission absolutePermission,\n                    boolean createParent)\n    throws IOException {\n\n    if (!createParent) { // parent must exist.\n      // since the this.mkdirs makes parent dirs automatically\n      // we must throw exception if parent does not exist.\n      final FileStatus stat = getFileStatus(f.getParent());\n      if (stat == null) {\n        throw new FileNotFoundException(\"Missing parent:\" + f);\n      }\n      if (!stat.isDirectory()) {\n        throw new ParentNotDirectoryException(\"parent is not a dir\");\n      }\n      // parent does exist - go ahead with mkdir of leaf\n    }\n    // Default impl is to assume that permissions do not matter and hence\n    // calling the regular mkdirs is good enough.\n    // FSs that implement permissions should override this.\n    if (!this.mkdirs(f, absolutePermission)) {\n      throw new IOException(\"mkdir of \"+ f + \" failed\");\n    }\n  }\n\n  /**\n   * Opens an FSDataOutputStream at the indicated Path with write-progress\n   * reporting. Same as create(), except fails if parent directory doesn't\n   * already exist.\n   * @param f the file name to open\n   * @param overwrite if a file with this name already exists, then if true,\n   * the file will be overwritten, and if false an error will be thrown.","sourceCodeStart":1387,"sourceCodeEnd":1423,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java#L1387-L1423","documentation":"The second precondition in primitiveMkdir(createParent=false): the parent path exists but is a regular file, so stat.isDirectory() is false and ParentNotDirectoryException('parent is not a dir') is thrown. A file cannot act as a directory, so creating f under it is impossible.","triggerScenarios":"primitiveMkdir(f, perm, false) where f.getParent() resolves to an existing file - e.g. creating /a/b/c while /a/b is a file.","commonSituations":"Stale output files occupying a directory prefix from earlier runs; layouts that changed between file-at-path and directory-at-path; partially cleaned temp trees.","solutions":["If the file is disposable, delete it (fs.delete(parent, true)) and recreate the directory","Otherwise choose a different destination layout so files and directories never share prefixes","Add a pre-flight check that every prefix of f is either absent or a directory"],"exampleFix":"// before\nfs.primitiveMkdir(new Path(\"/out/part-0/data\"), perm, false); // /out/part-0 is a FILE\n\n// after\nPath parent = new Path(\"/out/part-0/data\").getParent();\nif (fs.exists(parent) && !fs.getFileStatus(parent).isDirectory()) {\n  fs.delete(parent, true);\n}\nfs.mkdirs(parent);","handlingStrategy":"validation","validationCode":"Path parent = f.getParent();\nif (fs.exists(parent) && !fs.getFileStatus(parent).isDirectory()) {\n  throw new IllegalStateException(\"Path prefix occupied by a file: \" + parent);\n}\nfs.primitiveMkdir(f, perm, false);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Design output layouts where directory prefixes are stable","Clean stale outputs before creating directory trees","Assert parent type (directory or absent) in publish preconditions"],"tags":["hadoop","filesystem","mkdir","parent-not-directory"],"backgroundTag":"parent-not-directory","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}