{"record":{"id":"9eab92a54270c5c9","repo":"SonarSource/sonarqube","slug":"directory-could-not-be-created","errorCode":null,"errorMessage":"Directory  could not be created","messagePattern":"Directory  could not be created","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Files2.java","lineNumber":152,"sourceCode":"   * The parent directory will be created if it does not exist.\n   * The file will be created if it does not exist.\n   *\n   * @param file  the file to open for output, must not be {@code null}\n   * @param append if {@code true}, then bytes will be added to the\n   * end of the file rather than overwriting\n   * @return a new {@link FileOutputStream} for the specified file\n   * @throws IOException if the specified file is a directory\n   * @throws IOException if the file can not be written to\n   * @throws IOException if a parent directory can not be created\n   */\n  public FileOutputStream openOutputStreamOrThrowIOE(File file, boolean append) throws IOException {\n    if (file.exists()) {\n      checkOrThrowIOE(!file.isDirectory(), \"File %s exists but is a directory\", file);\n      checkOrThrowIOE(file.canWrite(), \"File %s can not be written to\", file);\n    } else {\n      File parent = file.getParentFile();\n      if (parent != null && !parent.mkdirs() && !parent.isDirectory()) {\n        throw new IOException(\"Directory \" + parent + \" could not be created\");\n      }\n    }\n    return new FileOutputStream(file, append);\n  }\n\n  /**\n   * Opens a {@link FileOutputStream} for the specified file, checking and\n   * creating the parent directory if it does not exist.\n   * <p>\n   * The parent directory will be created if it does not exist.\n   * The file will be created if it does not exist.\n   *\n   * @param file  the file to open for output, must not be {@code null}\n   * @param append if {@code true}, then bytes will be added to the\n   * end of the file rather than overwriting\n   * @return a new {@link FileOutputStream} for the specified file\n   * @throws IllegalStateException if the specified file is a directory\n   * @throws IllegalStateException if the file can not be written to","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Files2.java#L134-L170","documentation":"In Files2.openOutputStreamOrThrowIOE, before opening a FileOutputStream for a non-existent file, the code creates the parent directory with mkdirs(). If mkdirs() fails and the parent is still not a directory, it throws an IOException \"Directory <parent> could not be created\", which openOutputStream then wraps in an IllegalStateException. It signals the output file's parent directory could not be created.","triggerScenarios":"The target file does not exist, its parent directory also does not exist, and File.mkdirs() returns false without creating it — e.g. a path component is an existing file, permission denied on an ancestor, or invalid path characters.","commonSituations":"Configured work/temp directory path collides with an existing regular file; CE user lacks write permission on the parent; path too long or containing invalid characters; read-only volume.","solutions":["Check that every path component of the parent is a directory, not a file, and remove any conflicting file","Grant the CE process user write/execute permission on all ancestor directories","Verify the configured paths (sonar.path.data / temp dirs) are valid and on a writable volume","Create the parent directory manually and re-run the task"],"exampleFix":"// before\nfiles2.openOutputStream(new File(\"/opt/sonar/work\")); // 'work' is a regular file -> mkdirs fails\n// after\nFile target = new File(\"/opt/sonar/work/out.bin\");\nFile parent = target.getParentFile();\nif (!parent.isDirectory() && !parent.mkdirs()) {\n  throw new IOException(\"Pre-check failed for \" + parent);\n}\nfiles2.openOutputStream(target);","handlingStrategy":"validation","validationCode":"File parent = file.getParentFile();\nif (parent != null && parent.exists() && !parent.isDirectory()) {\n  throw new IllegalStateException(\"Path component is a file: \" + parent);\n}\nif (parent != null && !parent.canWrite()) {\n  throw new IllegalStateException(\"Parent not writable: \" + parent);\n}","typeGuard":null,"tryCatchPattern":"try {\n  FileOutputStream out = files2.openOutputStream(file, false);\n} catch (IllegalStateException e) {\n  LOGGER.error(\"Cannot create parent dir for {}: {}\", file, e.getCause(), e);\n}","preventionTips":["Make sure configured work/temp paths point to directories, not files","Grant the CE user write access to all parent directories","Pre-create directory trees during installation","Avoid overly long or OS-invalid paths"],"tags":["filesystem","mkdir","io","permissions"],"backgroundTag":"mkdir-permission-denied","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}