{"record":{"id":"6b0be96451bd7cca","repo":"elastic/elasticsearch","slug":"cannot-create-directory-s-as-it-already-exists","errorCode":null,"errorMessage":"Cannot create directory '%s' as it already exists, but is not a directory","messagePattern":"Cannot create directory '(.+?)' as it already exists, but 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":34,"sourceCode":"import java.util.Collections;\nimport java.util.LinkedList;\nimport java.util.List;\n\npublic final class FileUtils {\n\n    /**\n     * Like {@link java.io.File#mkdirs()}, except throws an informative error if a dir cannot be created.\n     *\n     * @param dir The dir to create, including any non existent parent dirs.\n     */\n    public static void mkdirs(File dir) {\n        dir = dir.getAbsoluteFile();\n        if (dir.isDirectory()) {\n            return;\n        }\n\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\",","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/build-tools/src/main/java/org/elasticsearch/gradle/util/FileUtils.java#L16-L52","documentation":"FileUtils.mkdirs() throws if `dir.exists()` is true BUT `dir.isDirectory()` is false — i.e. a regular file (or special file) already occupies the path where a directory is requested. This is the 'path collision' guard: mkdirs would otherwise fail opaquely or, worse, mask the conflict. It is NOT thrown when the path doesn't exist (that's the create path) or when it's already a directory (early return).","triggerScenarios":"Calling `FileUtils.mkdirs(file)` where `file` points at an existing non-directory: a previously-created file, a symlink to a file, a socket/device, or a broken symlink that `exists()` resolves as present.","commonSituations":"A build step created a file (e.g. a marker or lock) at a path later treated as a directory; a symlink in the build dir points at a file; a previous run left a stray file; cross-platform path handling where Windows considers some paths files.","solutions":["Identify and remove/redirect the conflicting file: `ls -la <path>` to see what it is.","If it's a stale artifact, delete it and rerun (or run `./gradlew clean`).","If a symlink, point it at a directory or remove it.","Restructure so the same path isn't used as both a file and a directory across tasks."],"exampleFix":"# before: a file exists at build/output\nrm build/output   # the conflicting file\n# now mkdirs will succeed\nFileUtils.mkdirs(file('build/output'))","handlingStrategy":"validation","validationCode":"if (dir.exists() && !dir.isDirectory()) {\n  throw new UncheckedIOException(\n    \"Path \" + dir + \" is a non-directory file; remove it before mkdirs.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use distinct base paths for generated files vs generated directories.","Run `./gradlew clean` between incompatible build shapes to clear stray files.","Avoid symlinks-to-files inside the build tree where directories are expected."],"tags":["gradle","filesystem","io","build-tools"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}