{"record":{"id":"f930cf8321f4f6fd","repo":"stanfordnlp/CoreNLP","slug":"could-not-create-directory-tgtdir-getabsolutepath-f930cf","errorCode":null,"errorMessage":"Could not create directory <tgtDir.getAbsolutePath()>","messagePattern":"Could not create directory <tgtDir\\.getAbsolutePath\\(\\)>","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/IOUtils.java","lineNumber":1746,"sourceCode":"  }\n\n  /**\n   * Given a filepath, makes sure a directory exists there.  If not, creates and returns it.\n   * Same as ENSURE-DIRECTORY in CL.\n   *\n   * @param tgtDir The directory that you wish to ensure exists\n   * @throws IOException If directory can't be created, is an existing file, or for other reasons\n   */\n  public static File ensureDir(File tgtDir) throws IOException {\n    if (tgtDir.exists()) {\n      if (tgtDir.isDirectory()) {\n        return tgtDir;\n      } else {\n        throw new IOException(\"Could not create directory \"+tgtDir.getAbsolutePath()+\", as a file already exists at that path.\");\n      }\n    } else {\n      if ( ! tgtDir.mkdirs()) {\n        throw new IOException(\"Could not create directory \"+tgtDir.getAbsolutePath());\n      }\n      return tgtDir;\n    }\n  }\n\n  /**\n   * Given a filepath, delete all files in the directory recursively\n   * @param dir Directory from which to delete files\n   * @return {@code true} if the deletion is successful, {@code false} otherwise\n   */\n  public static boolean deleteDirRecursively(File dir) {\n    if (dir.isDirectory()) {\n      for (File f : dir.listFiles()) {\n        boolean success = deleteDirRecursively(f);\n        if (!success)\n          return false;\n      }\n    }","sourceCodeStart":1728,"sourceCodeEnd":1764,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/IOUtils.java#L1728-L1764","documentation":"IOUtils.ensureDir(File) throws this IOException when the path does not exist and File.mkdirs() fails, meaning the directory (including any needed parent directories) could not be created — typically due to missing write permission on the parent, a read-only filesystem, or a race where another process created a file at the path.","triggerScenarios":"Calling IOUtils.ensureDir(tgtDir) where !tgtDir.exists() and tgtDir.mkdirs() returns false: no write permission on the parent directory, full/read-only disk, parent path component is a file, or a concurrent process created something at the path between the exists() check and mkdirs().","commonSituations":"Writing into system locations like /var or /usr without root; read-only container filesystems; running under a service account lacking permissions on the output root; NFS/permissions issues after deployment; output path colliding with an existing mount point.","solutions":["Check/fix permissions: ensure the process user can write to the parent directory (chmod/chown or run as the right user).","Verify the filesystem is writable (not read-only, not full: df -h, mount flags).","Log tgtDir.getAbsolutePath() and confirm each parent component is a directory, not a file.","If this can occur transiently (race with another process), re-check exists()/isDirectory() after the failure before reporting an error."],"exampleFix":"// before\nIOUtils.ensureDir(new File(\"/var/lib/myapp/cache\"));\n// after\nFile dir = new File(\"/var/lib/myapp/cache\");\nif (!dir.getParentFile().canWrite()) {\n  throw new IllegalStateException(\"No write permission on \" + dir.getParentFile());\n}\nIOUtils.ensureDir(dir);","handlingStrategy":"validation","validationCode":"File dir = new File(path);\nFile parent = dir.getParentFile();\nif (parent != null && !parent.canWrite())\n  throw new IllegalStateException(\"No write permission on \" + parent.getAbsolutePath());\nif (!dir.exists() && Files.isSymbolicLink(dir.toPath()))\n  throw new IllegalStateException(\"Broken symlink at \" + dir);","typeGuard":null,"tryCatchPattern":"try {\n  IOUtils.ensureDir(dir);\n} catch (IOException e) {\n  if (!dir.exists() && !dir.getParentFile().canWrite())\n    throw new IllegalStateException(\"mkdirs failed: check permissions/disk on \" + dir.getParent(), e);\n  throw e;\n}","preventionTips":["Verify the process user has write access to the output root at startup.","Check df/mount for read-only or full filesystems in deployment checks.","Create output directories explicitly during setup rather than lazily at runtime."],"tags":["io","filesystem","mkdir","permissions","java"],"backgroundTag":"mkdir-permission-denied","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"}