SonarSource/sonarqube · error · IllegalArgumentException

'%s' is not a directory

Error message

'%s' is not a directory

What it means

cleanDirectoryImpl throws IllegalArgumentException when the path passed to cleanDirectory is not a directory. Cleaning only makes sense on directories, so the precondition is enforced before walking the tree.

Source

Thrown at server/sonar-process/src/main/java/org/sonar/process/FileUtils2.java:142

  /**
   * Size of file or directory, in bytes. In case of a directory,
   * the size is the sum of the sizes of all files recursively traversed.
   *
   * This implementation is recommended over commons-io
   * {@code FileUtils#sizeOf(File)} which suffers from slow usage of Java IO.
   *
   * @throws IOException if files can't be traversed or size attribute is not present
   * @see BasicFileAttributes#size()
   */
  public static long sizeOf(Path path) throws IOException {
    SizeVisitor visitor = new SizeVisitor();
    Files.walkFileTree(path, visitor);
    return visitor.size;
  }

  private static void cleanDirectoryImpl(Path path) throws IOException {
    if (!path.toFile().isDirectory()) {
      throw new IllegalArgumentException(format("'%s' is not a directory", path));
    }
    Files.walkFileTree(path, FOLLOW_LINKS, CleanDirectoryFileVisitor.VISIT_MAX_DEPTH, new CleanDirectoryFileVisitor(path));
  }

  private static void deleteDirectoryImpl(Path path) throws IOException {
    Files.walkFileTree(path, DeleteRecursivelyFileVisitor.INSTANCE);
  }

  /**
   * This visitor is intended to be used to visit direct children of directory <strong>or a symLink to a directory</strong>,
   * so, with a max depth of {@link #VISIT_MAX_DEPTH 1}. Each direct children will either be directly deleted (if file)
   * or recursively deleted (if directory).
   */
  private static class CleanDirectoryFileVisitor extends SimpleFileVisitor<Path> {
    public static final int VISIT_MAX_DEPTH = 1;

    private final Path path;
    private final boolean symLink;

View on GitHub (pinned to 184c821202)

Solutions

  1. Ensure the path exists and is a directory before calling cleanDirectory (path.toFile().isDirectory())
  2. Fix the configured path value
  3. Create the directory first if it should exist

Example fix

// before
FileUtils2.cleanDirectory(dir);
// after
File d = new File(dir);
if (!d.isDirectory()) {
  throw new IllegalArgumentException(dir + " is not a directory");
}
FileUtils2.cleanDirectory(dir);
Defensive patterns

Strategy: validation

Validate before calling

File d = new File(path);
if (!d.exists() || !d.isDirectory()) { throw new IllegalArgumentException(path + " must be an existing directory"); }
FileUtils2.cleanDirectory(path);

Try / catch

try { FileUtils2.cleanDirectory(path); } catch (IllegalArgumentException e) { /* create the dir or fix the path */ }

Prevention

When it happens

Trigger: Calling FileUtils2.cleanDirectory(path) where path does not exist or is a regular file.

Common situations: Wrong path in configuration; directory deleted before cleaning; using a file path (e.g. a data file) instead of its parent directory.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/59000f0d12d85747. Report an issue: GitHub.