elastic/elasticsearch · error · UncheckedIOException

Failed to create directory '%s'

Error message

Failed to create directory '%s'

What it means

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.

Source

Thrown at build-tools/src/main/java/org/elasticsearch/gradle/util/FileUtils.java:66

            File parentDirToCreateParent = parentDirToCreate.getParentFile();
            if (parentDirToCreateParent.isDirectory() == false) {
                throw new UncheckedIOException(
                    String.format(
                        "Cannot create parent directory '%s' when creating directory '%s' as '%s' is not a directory",
                        parentDirToCreate,
                        dir,
                        parentDirToCreateParent
                    )
                );
            }
            if (parentDirToCreate.mkdir() == false && parentDirToCreate.isDirectory() == false) {
                throw new UncheckedIOException(
                    String.format("Failed to create parent directory '%s' when creating directory '%s'", parentDirToCreate, dir)
                );
            }
        }
        if (dir.mkdir() == false && dir.isDirectory() == false) {
            throw new UncheckedIOException(String.format("Failed to create directory '%s'", dir));
        }
    }

    public static String read(File file, String encoding) {
        try {
            return org.apache.commons.io.FileUtils.readFileToString(file, encoding);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    public static List<String> readLines(File file, String encoding) {
        try {
            return org.apache.commons.io.FileUtils.readLines(file, encoding);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

View on GitHub (pinned to db6a809a66)

Solutions

  1. Check write permission and ownership of the parent directory (`ls -la <parent>`).
  2. Look for filesystem-level denials: SELinux (`ls -Z`, audit log), immutable flag (`lsattr`), or AppArmor.
  3. Retry — if it's a race or transient FS error, a second run may succeed; if deterministic, address the permission/context issue.
  4. Shorten the path if hitting platform path-length limits.
Defensive patterns

Strategy: try-catch

Validate before calling

File parent = dir.getParentFile();
if (parent != null && parent.isDirectory() && !parent.canWrite()) {
  throw new IllegalStateException("Cannot create " + dir + "; parent not writable.");
}

Try / catch

try {
  FileUtils.mkdirs(dir);
} catch (UncheckedIOException e) {
  if (e.getMessage().contains("Failed to create directory")) {
    // check SELinux/AppArmor, immutable bit, path length; remediate then retry
  }
  throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/b8e0e670f45f92d9. Report an issue: GitHub.