{"record":{"id":"a055ec23ac55bfb8","repo":"elastic/elasticsearch","slug":"cannot-create-parent-directory-s-when-creating","errorCode":null,"errorMessage":"Cannot create parent directory '%s' when creating directory '%s' as '%s' is not a directory","messagePattern":"Cannot create parent directory '(.+?)' when creating directory '(.+?)' as '(.+?)' is not a directory","errorType":"exception","errorClass":"UncheckedIOException","httpStatus":null,"severity":"error","filePath":"build-tools/src/main/java/org/elasticsearch/gradle/util/FileUtils.java","lineNumber":50,"sourceCode":"\n        if (dir.exists() && dir.isDirectory() == false) {\n            throw new UncheckedIOException(String.format(\"Cannot create directory '%s' as it already exists, but is not a directory\", dir));\n        }\n\n        List<File> toCreate = new LinkedList<File>();\n        File parent = dir.getParentFile();\n        while (parent.exists() == false) {\n            toCreate.add(parent);\n            parent = parent.getParentFile();\n        }\n        Collections.reverse(toCreate);\n        for (File parentDirToCreate : toCreate) {\n            if (parentDirToCreate.isDirectory()) {\n                continue;\n            }\n            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    }","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/build-tools/src/main/java/org/elasticsearch/gradle/util/FileUtils.java#L32-L68","documentation":"FileUtils.mkdirs() throws while walking up parent dirs to create: for a parent that needs creating, it checks that the parent-of-that-parent `isDirectory()`. If a path component in the chain is a FILE (not a directory), you can't create a directory beneath it — the OS would refuse — so this fails fast with the three relevant paths named (the parent being created, the target dir, and the offending non-directory ancestor).","triggerScenarios":"Requesting `mkdirs(/a/b/c/d)` where some intermediate like `/a/b` is a regular file. The walk-up collects `/a/b/c` to create, then sees its parent `/a/b` is not a directory, triggering this guard.","commonSituations":"An earlier task wrote a file at `/a/b`, later a task tries to create `/a/b/c/d`; a symlink to a file in the middle of a path; misconfigured output paths that collide across plugins; a generated file (e.g. a `.jar`) whose path overlaps a desired directory tree.","solutions":["Inspect the offending ancestor path (third `%s` in the message) and remove or relocate the file blocking the chain.","Rerun `./gradlew clean` to reset the build tree if the conflict is a stale artifact.","Adjust one of the paths so file and directory trees don't overlap.","On Unix, check for symlinks (`ls -la` / `readlink`) mid-path that resolve to files."],"exampleFix":"# before: /a/b is a file, blocking /a/b/c\nrm /a/b\n# now mkdirs(/a/b/c) can proceed","handlingStrategy":"validation","validationCode":"// Walk the path chain; reject if any existing component is a non-directory\nFile f = dir.getAbsoluteFile();\nwhile (f != null) {\n  if (f.exists() && !f.isDirectory()) {\n    throw new IllegalStateException(\"Non-directory component blocks mkdirs: \" + f);\n  }\n  f = f.getParentFile();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep output-path trees disjoint: never reuse a file path as a directory prefix.","Audit generated-file locations before introducing new directory conventions.","Check for mid-path symlinks with `ls -la` / `readlink -f` when debugging."],"tags":["gradle","filesystem","io","build-tools"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}