SonarSource/sonarqube · error · IllegalStateException

Can not write to file

Error message

Can not write to file 

What it means

When DumpWriterImpl writes ProjectDump.Metadata to the metadata file inside the dump root, an IOException while opening or writing the file is converted into an IllegalStateException with the message "Can not write to file <path>" plus the cause. The dump cannot proceed without metadata, so the export is aborted.

Source

Thrown at server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/DumpWriterImpl.java:67

  public DumpWriterImpl(ProjectDescriptor descriptor, ProjectExportDumpFS projectExportDumpFS, TempFolder tempFolder) {
    this.descriptor = descriptor;
    this.projectExportDumpFS = projectExportDumpFS;
    this.tempFolder = tempFolder;
    this.rootDir = tempFolder.newDir();
  }

  @Override
  public void write(ProjectDump.Metadata metadata) {
    checkNotPublished();
    if (metadataWritten.get()) {
      throw new IllegalStateException("Metadata has already been written");
    }
    File file = new File(rootDir, METADATA.filename());
    try (FileOutputStream output = FILES2.openOutputStream(file, false)) {
      PROTOBUF2.writeTo(metadata, output);
      metadataWritten.set(true);
    } catch (IOException e) {
      throw new IllegalStateException("Can not write to file " + file, e);
    }
  }

  @Override
  public <M extends Message> StreamWriter<M> newStreamWriter(DumpElement<M> elt) {
    checkNotPublished();
    File file = new File(rootDir, elt.filename());
    return StreamWriterImpl.create(file);
  }

  @Override
  public void publish() {
    checkNotPublished();
    if (!metadataWritten.get()) {
      throw new IllegalStateException("Metadata is missing");
    }
    File zip = tempFolder.newFile();
    FILES2.zipDir(rootDir, zip);

View on GitHub (pinned to 184c821202)

Solutions

  1. Check the wrapped IOException cause for the exact filesystem reason
  2. Free disk space on the volume holding the dump directory
  3. Fix permissions/ownership of the dump working directory for the sonarqube/CE user
  4. Ensure rootDir exists and is created before DumpWriterImpl is constructed

Example fix

// before
File file = new File(rootDir, METADATA.filename());
// after: verify writability up-front
Files.createDirectories(rootDir.toPath());
if (!rootDir.canWrite()) { throw new IllegalStateException("Dump dir not writable: " + rootDir); }
Defensive patterns

Strategy: validation

Validate before calling

Files.createDirectories(rootDir.toPath());
if (!Files.isWritable(rootDir.toPath())) {
  throw new IOException("Dump root not writable: " + rootDir);
}

Try / catch

try { writer.write(metadata); } catch (IllegalStateException e) { logger.error("Cannot write metadata file: {}", e.getCause() == null ? "" : e.getCause().getMessage(), e); }

Prevention

When it happens

Trigger: Metadata file cannot be opened for writing (path doesn't exist, permission denied, disk full) or protobuf writeTo fails with an IOException during write(metadata).

Common situations: Insufficient disk space on the CE node; wrong ownership/permissions on the dump working directory; the rootDir was deleted or not created before the writer ran.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/09f67a207ed93cb1. Report an issue: GitHub.