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
- 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.
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
- 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.
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
- Failed to create parent directory '%s' when creating directo
- Unable to create reaper JAR output directory {}
- Cannot create directory '%s' as it already exists, but is no
- Cannot create parent directory '%s' when creating directory
- Failed to patch archive
AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12).
Data as JSON: /api/errors/b8e0e670f45f92d9.
Report an issue: GitHub.