languagetool-org/languagetool · error

<resultDir> already exists, but is a file:

Error message

<resultDir> already exists, but is a file: 

What it means

In RuleMatchDiffFinder's two-file mode, the third argument <resultDir> is the output directory. If that path already exists but is a regular file rather than a directory, main() throws this IOException because results cannot be written into it.

Source

Thrown at languagetool-dev/src/main/java/org/languagetool/dev/diff/RuleMatchDiffFinder.java:687

            String outputDir = file3.replace("XX", langCode);
            diffFinder.run(parser, oldFile, newFile, new File(outputDir), langCode, date);
          }
        }
      }
    } else {
      if (args.length != 5) {
        System.out.println("Usage: " + RuleMatchDiffFinder.class.getSimpleName() + " <matches1> <matches2> <resultDir> <langCode> <date>");
        System.out.println(" <matches1> and <matches2> are text outputs of different versions of org.languagetool.dev.dumpcheck.SentenceSourceChecker run on the same input");
        System.out.println("                           or JSON outputs from org.languagetool.dev.httpchecker.HttpApiSentenceChecker");
        System.exit(1);
      }
      File file1 = new File(args[0]);
      File file2 = new File(args[1]);
      File outputDir = new File(args[2]);
      String langCode = args[3];
      String date = args[4];
      if (outputDir.exists() && outputDir.isFile()) {
        throw new IOException("<resultDir> already exists, but is a file: " + outputDir);
      }
      if (!outputDir.exists()) {
        boolean mkdir = outputDir.mkdir();
        if (!mkdir) {
          throw new IOException("Could not create directory " + outputDir);
        }
      }
      diffFinder.run(parser, file1, file2, outputDir, langCode, date);
    }
  }

}

View on GitHub (pinned to 2e990059ce)

Solutions

  1. Remove the file at that path or choose a new directory name for <resultDir>
  2. mkdir the directory beforehand if you want to reuse the name
  3. Check your CLI arguments: args are oldFile newFile resultDir langCode date

Example fix

// before
java RuleMatchDiffFinder old.txt new.txt results.txt de 2024-01-01
// after
mkdir -p results
java RuleMatchDiffFinder old.txt new.txt results de 2024-01-01
Defensive patterns

Strategy: validation

Validate before calling

File out = new File(args[2]);
if (out.exists() && out.isFile()) {
  throw new IllegalArgumentException("resultDir must be a directory: " + out);
}
if (!out.exists()) out.mkdirs();

Type guard

static boolean isValidResultDir(String path) {
  File f = new File(path);
  return !f.exists() || f.isDirectory();
}

Try / catch

try {
  diffFinderMain(args);
} catch (IOException e) {
  if (e.getMessage().startsWith("<resultDir> already exists")) {
    System.err.println("pass a directory, not a file: " + args[2]);
  } else throw e;
}

Prevention

When it happens

Trigger: Passing an existing file path (e.g. results.txt) as args[2] instead of a directory name; typo where the output arg accidentally points at an input file.

Common situations: Script variable reused from a previous run that holds a file path; shell glob expanding to a file; copying an example command but leaving a filename as the output argument.

Understand the failure class

Background: "Unknown argument", "Invalid value", and "must be one of": invalid CLI argument errors explained — this error's family across 35 libraries.

Related errors


AI-assisted analysis of languagetool-org/languagetool@2e990059ce (2026-09-06). Data as JSON: /api/errors/28e9146d51a544f8. Report an issue: GitHub.