{"record":{"id":"bc7429bf799a823c","repo":"stanfordnlp/CoreNLP","slug":"cp-cannot-copy-to-directory-recursive-parent-bc7429","errorCode":null,"errorMessage":"cp: cannot copy to directory: <recursive> (parent isn't a directory)","messagePattern":"cp: cannot copy to directory: <recursive> \\(parent isn't a directory\\)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/IOUtils.java","lineNumber":1884,"sourceCode":"   * @param source The source file or directory.\n   * @param target The target to write this file or directory to.\n   * @param recursive If true, recursively copy directory contents\n   * @throws IOException If either the copy fails (standard IO Exception), or the command is invalid\n   *                     (e.g., copying a directory without the recursive flag)\n   */\n  public static void cp(File source, File target, boolean recursive) throws IOException {\n    // Error checks\n    if (source.isDirectory() && !recursive) {\n      // cp a b -- a is a directory\n      throw new IOException(\"cp: omitting directory: \" + source);\n    }\n    if (!target.getParentFile().exists()) {\n      // cp a b/c/d/e -- b/c/d doesn't exist\n      throw new IOException(\"cp: cannot copy to directory: \" + recursive + \" (parent doesn't exist)\");\n    }\n    if (!target.getParentFile().isDirectory()) {\n      // cp a b/c/d/e -- b/c/d is a regular file\n      throw new IOException(\"cp: cannot copy to directory: \" + recursive + \" (parent isn't a directory)\");\n    }\n    // Get true target\n    File trueTarget;\n    if (target.exists() && target.isDirectory()) {\n      trueTarget = new File(target.getPath() + File.separator + source.getName());\n    } else {\n      trueTarget = target;\n    }\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()) {","sourceCodeStart":1866,"sourceCodeEnd":1902,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/IOUtils.java#L1866-L1902","documentation":"IOUtils.cp(File, File, boolean) throws this IOException when the target's parent path exists but is a regular file, not a directory — e.g. copying to a/b/c/d where a/b/c is a file. As with the sibling error, the message interpolates the recursive flag rather than the offending path.","triggerScenarios":"Calling IOUtils.cp(source, target, recursive) where target.getParentFile() exists but target.getParentFile().isDirectory() is false.","commonSituations":"A path component expected to be a directory is actually a file (leftover file created by a previous bug or bind-mount); output path built from a filename prefix that collides with an existing file; Windows/Unix path handling mistakes producing \"data.txt/sub\" style paths.","solutions":["Inspect target.getParentFile(): remove/rename the file occupying the intended directory path.","Validate before copying: parent must exist AND be a directory; fail early with the real path in the message.","Correct the path construction so no file is used as a path component."],"exampleFix":"// before\nIOUtils.cp(src, new File(\"/out/data.txt/item.bin\"), true);\n// after\nFile target = new File(\"/out/data.txt/item.bin\");\nFile parent = target.getParentFile();\nif (parent.exists() && !parent.isDirectory()) {\n  throw new IllegalStateException(\"Not a directory: \" + parent.getAbsolutePath());\n}\nIOUtils.cp(src, target, true);","handlingStrategy":"validation","validationCode":"File target = new File(targetPath);\nFile parent = target.getParentFile();\nif (parent != null && parent.exists() && !parent.isDirectory())\n  throw new IllegalStateException(\"Parent is a file, not a directory: \" + parent.getAbsolutePath());","typeGuard":null,"tryCatchPattern":"try {\n  IOUtils.cp(src, target, recursive);\n} catch (IOException e) {\n  if (e.getMessage().contains(\"(parent isn't a directory)\")) {\n    throw new IllegalStateException(\"Path component is a file: \" + target.getParentFile().getAbsolutePath(), e);\n  }\n  throw e;\n}","preventionTips":["Never append path components to a filename variable — validate the prefix is a directory.","Detect leftover files at directory paths from previous runs during environment checks."],"tags":["io","filesystem","copy","path","java"],"backgroundTag":"path-is-not-a-directory","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}