{"record":{"id":"3a0b00e75280ac50","repo":"stanfordnlp/CoreNLP","slug":"cp-cannot-copy-to-directory-recursive-parent","errorCode":null,"errorMessage":"cp: cannot copy to directory: <recursive> (parent doesn't exist)","messagePattern":"cp: cannot copy to directory: <recursive> \\(parent doesn't exist\\)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/IOUtils.java","lineNumber":1880,"sourceCode":"   * <p>An implementation of cp, as close to the Unix command as possible.\n   * Both directories and files are valid for either the source or the target;\n   * if the target exists, the semantics of Unix cp are [intended to be] obeyed.</p>\n   *\n   * @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","sourceCodeStart":1862,"sourceCodeEnd":1898,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/IOUtils.java#L1862-L1898","documentation":"IOUtils.cp(File, File, boolean) throws this IOException when the parent directory of the target path does not exist, so the copy destination cannot be resolved. Note the message incorrectly interpolates the recursive flag where cp(1) would print the target path.","triggerScenarios":"Calling IOUtils.cp(source, new File(\"a/b/c/d\"), recursive) where directory a/b/c does not exist — target.getParentFile().exists() is false.","commonSituations":"Building target paths by concatenation where an intermediate directory was never created; output roots not yet initialized on a fresh environment; typos in directory names in config; deleted output directories between runs.","solutions":["Create the target's parent directory first (target.getParentFile().mkdirs() or IOUtils.ensureDir(target.getParentFile())) before calling cp.","Validate that target.getParentFile() exists and is a directory before copying.","Fix the path construction/config so the destination points under an existing directory."],"exampleFix":"// before\nIOUtils.cp(srcFile, new File(\"/out/2026/sep/file.txt\"), true);\n// after\nFile target = new File(\"/out/2026/sep/file.txt\");\nIOUtils.ensureDir(target.getParentFile());\nIOUtils.cp(srcFile, target, true);","handlingStrategy":"validation","validationCode":"File target = new File(targetPath);\nFile parent = target.getParentFile();\nif (parent == null || !parent.isDirectory())\n  IOUtils.ensureDir(parent == null ? new File(\".\") : parent); // create missing parents first","typeGuard":null,"tryCatchPattern":"try {\n  IOUtils.cp(src, target, recursive);\n} catch (IOException e) {\n  if (e.getMessage().contains(\"(parent doesn't exist)\")) {\n    IOUtils.ensureDir(target.getParentFile());\n    IOUtils.cp(src, target, recursive);\n  } else throw e;\n}","preventionTips":["Always create the destination's parent directory before copying.","Normalize and validate constructed paths against an existing base directory.","Watch out: the message prints the recursive flag, not the path — log the target path separately."],"tags":["io","filesystem","copy","directory","java"],"backgroundTag":"directory-not-found","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"}