{"record":{"id":"b8e0e670f45f92d9","repo":"elastic/elasticsearch","slug":"failed-to-create-directory-s","errorCode":null,"errorMessage":"Failed to create directory '%s'","messagePattern":"Failed to create directory '(.+?)'","errorType":"exception","errorClass":"UncheckedIOException","httpStatus":null,"severity":"error","filePath":"build-tools/src/main/java/org/elasticsearch/gradle/util/FileUtils.java","lineNumber":66,"sourceCode":"            File parentDirToCreateParent = parentDirToCreate.getParentFile();\n            if (parentDirToCreateParent.isDirectory() == false) {\n                throw new UncheckedIOException(\n                    String.format(\n                        \"Cannot create parent directory '%s' when creating directory '%s' as '%s' is not a directory\",\n                        parentDirToCreate,\n                        dir,\n                        parentDirToCreateParent\n                    )\n                );\n            }\n            if (parentDirToCreate.mkdir() == false && parentDirToCreate.isDirectory() == false) {\n                throw new UncheckedIOException(\n                    String.format(\"Failed to create parent directory '%s' when creating directory '%s'\", parentDirToCreate, dir)\n                );\n            }\n        }\n        if (dir.mkdir() == false && dir.isDirectory() == false) {\n            throw new UncheckedIOException(String.format(\"Failed to create directory '%s'\", dir));\n        }\n    }\n\n    public static String read(File file, String encoding) {\n        try {\n            return org.apache.commons.io.FileUtils.readFileToString(file, encoding);\n        } catch (IOException e) {\n            throw new UncheckedIOException(e);\n        }\n    }\n\n    public static List<String> readLines(File file, String encoding) {\n        try {\n            return org.apache.commons.io.FileUtils.readLines(file, encoding);\n        } catch (IOException e) {\n            throw new UncheckedIOException(e);\n        }\n    }","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/build-tools/src/main/java/org/elasticsearch/gradle/util/FileUtils.java#L48-L84","documentation":"FileUtils.mkdirs() throws when, after all parents are created (or already exist), the FINAL target directory's `mkdir()` returns false and `isDirectory()` is still false. All ancestors are fine; only the leaf directory creation failed. This mirrors #118 but for the target itself rather than an intermediate parent.","triggerScenarios":"Permissions denied specifically on the target's parent for creating the final component; a file already exists at the target path would normally hit #116 first, so this is specifically a create-failure with no pre-existing entry (or a race that resolved between checks).","commonSituations":"Parent directory is writable-checked but the final create fails due to SELinux label, immutable-bit on the parent, a transient lock, a path-length limit, or a filesystem error. Also a race: another process created then removed the dir between the two checks.","solutions":["Check write permission and ownership of the parent directory (`ls -la <parent>`).","Look for filesystem-level denials: SELinux (`ls -Z`, audit log), immutable flag (`lsattr`), or AppArmor.","Retry — if it's a race or transient FS error, a second run may succeed; if deterministic, address the permission/context issue.","Shorten the path if hitting platform path-length limits."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"File parent = dir.getParentFile();\nif (parent != null && parent.isDirectory() && !parent.canWrite()) {\n  throw new IllegalStateException(\"Cannot create \" + dir + \"; parent not writable.\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  FileUtils.mkdirs(dir);\n} catch (UncheckedIOException e) {\n  if (e.getMessage().contains(\"Failed to create directory\")) {\n    // check SELinux/AppArmor, immutable bit, path length; remediate then retry\n  }\n  throw e;\n}","preventionTips":["Confirm write permission and SELinux/AppArmor context on the target parent.","Watch for immutable flags (`lsattr`) on parent directories in hardened environments.","Keep path lengths reasonable to avoid OS limits on directory creation."],"tags":["gradle","filesystem","io","permissions","build-tools"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}