{"record":{"id":"03b09a2294ea19cb","repo":"stanfordnlp/CoreNLP","slug":"cp-cannot-copy-directory-into-regular-file-targ","errorCode":null,"errorMessage":"cp: cannot copy directory into regular file: <target>","messagePattern":"cp: cannot copy directory into regular file: <target>","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/IOUtils.java","lineNumber":1906,"sourceCode":"    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()) {\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","sourceCodeStart":1888,"sourceCodeEnd":1924,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/IOUtils.java#L1888-L1924","documentation":"IOUtils.cp(File,File,boolean) implements a cp -r style recursive copy. When the target path already exists and is a regular file, a directory cannot be copied onto it, so the method throws this IOException to prevent destroying the file.","triggerScenarios":"Calling IOUtils.cp(sourceDir, existingRegularFile, true) (or false) where target.exists() is true and target.isDirectory() is false.","commonSituations":"Script-driven copies that reuse a stale output filename; a previous run wrote the target as a file but the new source is a directory; typos in target paths in config.","solutions":["Delete or rename the existing regular-file target before calling cp, or point the target at a new path","Verify the target path with File.isDirectory() before copying and fail early with a clear message","Use a fresh target directory path per run (timestamped output dir)"],"exampleFix":"// before\nIOUtils.cp(new File(\"data/src\"), new File(\"out\"), true); // out is an old file\n// after\nFile out = new File(\"out\");\nif (out.exists() && !out.isDirectory()) { out.delete(); }\nIOUtils.cp(new File(\"data/src\"), out, true);","handlingStrategy":"validation","validationCode":"if (target.exists() && !target.isDirectory()) { throw new IllegalStateException(\"cp target is a file: \" + target); }","typeGuard":null,"tryCatchPattern":"try { IOUtils.cp(src, target, true); } catch (IOException e) { if (e.getMessage().startsWith(\"cp: cannot copy directory into regular file\")) { /* fix target path or delete file */ } else throw e; }","preventionTips":["Check target.exists()/isDirectory() before cp","Use per-run timestamped target directories","Never reuse a file output path for directory output"],"tags":["io","filesystem","copy"],"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"}