stanfordnlp/CoreNLP · error · IOException

cp: overwriting a file with a directory

Error message

cp: overwriting a file with a directory: <trueTarget>

What it means

During a recursive cp where the target directory exists, the method computes trueTarget = target/sourceName. If trueTarget already exists as a regular file, copying a directory over it would overwrite a file with a directory, so this IOException is thrown.

Solutions

  1. Remove or rename the conflicting file inside the existing target directory before copying
  2. Pick a different target directory that does not contain a file with the source's name
  3. Check new File(target, source.getName()) type before the copy and handle the conflict explicitly

Example fix

// before
IOUtils.cp(srcDir, destDir, true); // destDir/srcDirName is a file
// after
File trueTarget = new File(destDir, srcDir.getName());
if (trueTarget.exists() && !trueTarget.isDirectory()) { trueTarget.delete(); }
IOUtils.cp(srcDir, destDir, true);
Defensive patterns

Strategy: validation

Validate before calling

File tt = new File(target, source.getName()); if (tt.exists() && !tt.isDirectory()) { tt.delete(); /* or abort */ }

Try / catch

try { IOUtils.cp(src, target, true); } catch (IOException e) { if (e.getMessage().contains("overwriting a file with a directory")) { /* resolve name collision */ } else throw e; }

Prevention

When it happens

Trigger: IOUtils.cp(dirA, existingDirB, true) where existingDirB/dirA.getName() exists and is a regular file.

Common situations: Merging directory trees where a subdirectory name collides with an existing file (e.g. a file named 'src' inside an existing output dir); partial state from an aborted previous copy.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/b62f59a3ddfe59fd. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/io/IOUtils.java:1910

    }
    // Copy
    if (source.isFile()) {
      // Case: copying a file
      copyFile(source, trueTarget);
    } else if (source.isDirectory()) {
      // Case: copying a directory
      File[] children = source.listFiles();
      if (children == null) { throw new IOException("cp: could not list files in source: " + source); }

      if (target.exists()) {
        // Case: cp -r a b -- b exists
        if (!target.isDirectory()) {
          // cp -r a b -- b is a regular file
          throw new IOException("cp: cannot copy directory into regular file: " + target);
        }
        if (trueTarget.exists() && !trueTarget.isDirectory()) {
          // cp -r a b -- b/a is not a directory
          throw new IOException("cp: overwriting a file with a directory: " + trueTarget);
        }
        if (!trueTarget.exists() && !trueTarget.mkdir()) {
          // cp -r a b -- b/a cannot be created
          throw new IOException("cp: could not create directory: " + trueTarget);
        }
      } else {
        // Case: cp -r a b -- b does not exist
        assert trueTarget == target;
        if (!trueTarget.mkdir()) {
          // cp -r a b -- cannot create b as a directory
          throw new IOException("cp: could not create target directory: " + trueTarget);
        }
      }
      // Actually do the copy
      for (File child : children) {
        File childTarget = new File(trueTarget.getPath() + File.separator + child.getName());
        cp(child, childTarget, recursive);
      }

View on GitHub (pinned to 1b7edd19c4)