{"record":{"id":"b62f59a3ddfe59fd","repo":"stanfordnlp/CoreNLP","slug":"cp-overwriting-a-file-with-a-directory-truetarg","errorCode":null,"errorMessage":"cp: overwriting a file with a directory: <trueTarget>","messagePattern":"cp: overwriting a file with a directory: <trueTarget>","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/IOUtils.java","lineNumber":1910,"sourceCode":"    }\n    // Copy\n    if (source.isFile()) {\n      // Case: copying a file\n      copyFile(source, trueTarget);\n    } else if (source.isDirectory()) {\n      // Case: copying a directory\n      File[] children = source.listFiles();\n      if (children == null) { throw new IOException(\"cp: could not list files in source: \" + source); }\n\n      if (target.exists()) {\n        // Case: cp -r a b -- b exists\n        if (!target.isDirectory()) {\n          // cp -r a b -- b is a regular file\n          throw new IOException(\"cp: cannot copy directory into regular file: \" + target);\n        }\n        if (trueTarget.exists() && !trueTarget.isDirectory()) {\n          // cp -r a b -- b/a is not a directory\n          throw new IOException(\"cp: overwriting a file with a directory: \" + trueTarget);\n        }\n        if (!trueTarget.exists() && !trueTarget.mkdir()) {\n          // cp -r a b -- b/a cannot be created\n          throw new IOException(\"cp: could not create directory: \" + trueTarget);\n        }\n      } else {\n        // Case: cp -r a b -- b does not exist\n        assert trueTarget == target;\n        if (!trueTarget.mkdir()) {\n          // cp -r a b -- cannot create b as a directory\n          throw new IOException(\"cp: could not create target directory: \" + trueTarget);\n        }\n      }\n      // Actually do the copy\n      for (File child : children) {\n        File childTarget = new File(trueTarget.getPath() + File.separator + child.getName());\n        cp(child, childTarget, recursive);\n      }","sourceCodeStart":1892,"sourceCodeEnd":1928,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/IOUtils.java#L1892-L1928","documentation":"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.","triggerScenarios":"IOUtils.cp(dirA, existingDirB, true) where existingDirB/dirA.getName() exists and is a regular file.","commonSituations":"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.","solutions":["Remove or rename the conflicting file inside the existing target directory before copying","Pick a different target directory that does not contain a file with the source's name","Check new File(target, source.getName()) type before the copy and handle the conflict explicitly"],"exampleFix":"// before\nIOUtils.cp(srcDir, destDir, true); // destDir/srcDirName is a file\n// after\nFile trueTarget = new File(destDir, srcDir.getName());\nif (trueTarget.exists() && !trueTarget.isDirectory()) { trueTarget.delete(); }\nIOUtils.cp(srcDir, destDir, true);","handlingStrategy":"validation","validationCode":"File tt = new File(target, source.getName()); if (tt.exists() && !tt.isDirectory()) { tt.delete(); /* or abort */ }","typeGuard":null,"tryCatchPattern":"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; }","preventionTips":["Check for file/subdirectory name collisions before merging trees","Clean partial output from failed runs before re-copying","Keep source directory names unique inside target"],"tags":["io","filesystem","copy","conflict"],"backgroundTag":"incompatible-source-type","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}