{"record":{"id":"1a0c263f094770bc","repo":"SonarSource/sonarqube","slug":"can-not-open-file","errorCode":null,"errorMessage":"Can not open file ","messagePattern":"Can not open file ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Files2.java","lineNumber":177,"sourceCode":"   * Opens a {@link FileOutputStream} for the specified file, checking and\n   * creating the parent directory if it does not exist.\n   * <p>\n   * The parent directory will be created if it does not exist.\n   * The file will be created if it does not exist.\n   *\n   * @param file  the file to open for output, must not be {@code null}\n   * @param append if {@code true}, then bytes will be added to the\n   * end of the file rather than overwriting\n   * @return a new {@link FileOutputStream} for the specified file\n   * @throws IllegalStateException if the specified file is a directory\n   * @throws IllegalStateException if the file can not be written to\n   * @throws IllegalStateException if a parent directory can not be created\n   */\n  public FileOutputStream openOutputStream(File file, boolean append) {\n    try {\n      return openOutputStreamOrThrowIOE(file, append);\n    } catch (IOException e) {\n      throw new IllegalStateException(\"Can not open file \" + file, e);\n    }\n  }\n\n  /**\n   * Opens a {@link FileInputStream} for the specified file, providing better\n   * error messages than simply calling {@code new FileInputStream(file)}.\n   *\n   * @param file  the file to open, must not be {@code null}\n   * @return a new {@link FileInputStream} for the specified file\n   * @throws IOException if the file does not exist\n   * @throws IOException if the specified file is a directory\n   * @throws IOException if the file can not be read\n   */\n  public FileInputStream openInputStreamOrThrowIOE(File file) throws IOException {\n    checkOrThrowIOE(!file.isDirectory(), \"File %s exists but is a directory\", file);\n    checkOrThrowIOE(file.exists(), \"File %s does not exist\", file);\n    checkOrThrowIOE(file.canRead(), \"File %s can not be read\", file);\n    return new FileInputStream(file);","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Files2.java#L159-L195","documentation":"Files2.openOutputStream(File, boolean) opens a FileOutputStream after ensuring parent directories exist and the file is writable, translating any IOException into an IllegalStateException with the message \"Can not open file \" plus the path. It covers both the pre-checks in openOutputStreamOrThrowIOE (including the 'Directory could not be created' case) and the actual FileOutputStream construction.","triggerScenarios":"openOutputStreamOrThrowIOE throws IOException — file exists but is a directory, file not writable, parent directory cannot be created, or new FileOutputStream fails (permission denied, disk full, path invalid).","commonSituations":"Dump/report output path points at a directory instead of a file; CE user lacks write permission; disk quota or full volume; misconfigured work directory path.","solutions":["Verify the target path is a writable file location and not a directory","Check filesystem permissions for the CE process user on the target and its parents","Free disk space / raise quota on the target volume","Fix the configured output or work directory path and retry"],"exampleFix":"// before\nfiles2.openOutputStream(new File(\"/data/dump\"), false); // path is a directory\n// after\nFile out = new File(\"/data/dump/metadata.pb\");\nif (out.isDirectory()) {\n  throw new IllegalArgumentException(\"Expected a file path: \" + out);\n}\nfiles2.openOutputStream(out, false);","handlingStrategy":"validation","validationCode":"if (file.isDirectory()) throw new IllegalArgumentException(\"Expected file, got dir: \" + file);\nif (file.exists() && !file.canWrite()) throw new IllegalArgumentException(\"Not writable: \" + file);\nFile p = file.getParentFile();\nif (p != null && !p.isDirectory() && !p.mkdirs()) throw new IllegalArgumentException(\"Cannot create \" + p);","typeGuard":null,"tryCatchPattern":"try {\n  FileOutputStream out = files2.openOutputStream(file, append);\n} catch (IllegalStateException e) {\n  LOGGER.error(\"Open for write failed on {}: {}\", file, e.getCause(), e);\n}","preventionTips":["Pre-check the target path is a writable file, not a directory","Watch disk quotas and free space on output volumes","Run services with correct file permissions","Validate configured output paths at startup"],"tags":["filesystem","file-write","io","permissions"],"backgroundTag":"file-open-failed","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}