{"record":{"id":"54c145afa909f5c2","repo":"apache/hadoop","slug":"path-is-a-file","errorCode":null,"errorMessage":"Path is a file: {}","messagePattern":"Path is a file: (.+?)","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java","lineNumber":510,"sourceCode":"        } else {\n          throw new FileAlreadyExistsException(String.format(\n              \"Can't make directory for path '%s', it is a file.\", parent));\n        }\n      } catch (FileNotFoundException e) {\n        LOG.debug(\"The Path: [{}] does not exist.\", path);\n      }\n      parent = parent.getParent();\n    } while (parent != null);\n  }\n\n  @Override\n  public boolean mkdirs(Path f, FsPermission permission) throws IOException {\n    try {\n      FileStatus fileStatus = getFileStatus(f);\n      if (fileStatus.isDirectory()) {\n        return true;\n      } else {\n        throw new FileAlreadyExistsException(\"Path is a file: \" + f);\n      }\n    } catch (FileNotFoundException e) {\n      validatePath(f);\n    }\n\n    return mkDirRecursively(f, permission);\n  }\n\n  /**\n   * Recursively create a directory.\n   *\n   * @param f          Absolute path to the directory.\n   * @param permission Directory permissions. Permission does not work for\n   *                   the CosN filesystem currently.\n   * @return Return true if the creation was successful,  throw a IOException.\n   * @throws IOException The specified path already exists or an error\n   *                     creating the path.\n   */","sourceCodeStart":492,"sourceCodeEnd":528,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java#L492-L528","documentation":"mkdirs(f) first stats f itself: if f already exists as a FILE it throws FileAlreadyExistsException('Path is a file: <f>'). If f already is a directory, mkdirs is idempotent and returns true, so the exception fires only when the exact target path is occupied by a file.","triggerScenarios":"fs.mkdirs(p) where an object with key p already exists; hadoop fs -mkdir on a path that is a file; tools that mkdir an output directory whose name equals an already-written output file.","commonSituations":"Output directory name equals a previously written file name; Hive/Spark warehouse partition creation colliding with staged files; passing a file path where its parent directory was meant.","solutions":["Most likely a caller mistake: pass the intended directory path, not a file path.","If the file is stale, fs.delete(f, false) then fs.mkdirs(f).","Guard reusable utilities with a status check before mkdirs."],"exampleFix":"// before\nfs.mkdirs(new Path('/out/part-00000')); // FileAlreadyExistsException: Path is a file: /out/part-00000\n\n// after\nfs.mkdirs(new Path('/out')); // the directory level you actually want","handlingStrategy":"validation","validationCode":"if (fs.exists(f)) {\n  FileStatus st = fs.getFileStatus(f);\n  if (st.isFile()) { throw new IllegalStateException('Target is already a file: ' + f); }\n  // existing directory: mkdirs is a no-op success\n}","typeGuard":null,"tryCatchPattern":"try {\n  fs.mkdirs(f);\n} catch (FileAlreadyExistsException e) {\n  // 'Path is a file: <f>' -> the exact target is occupied by a file\n  if (allowReplace) { fs.delete(f, false); fs.mkdirs(f); } else { throw e; }\n}","preventionTips":["Keep file and directory names in disjoint namespaces in your layouts","Check status before mkdirs in reusable utilities","Derive directory paths from config, never from a file path string"],"tags":["cosn","hadoop","mkdirs","path-is-file","file-already-exists"],"backgroundTag":"mkdir-target-is-file","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}