SonarSource/sonarqube · error · IllegalStateException

Failed to create temp file

Error message

Failed to create temp file

What it means

DefaultTempFolder.createTempFile creates a temporary file with Files.createTempFile inside the temp directory. An IOException (permissions, missing base dir, disk full, invalid prefix/suffix) is wrapped in this IllegalStateException. The library throws it because newFile() must return a usable, writable temp file.

Solutions

  1. Ensure the temp base directory exists and is writable (recreate DefaultTempFolder if it was cleaned)
  2. Shorten the prefix/suffix and avoid illegal path characters
  3. Check free disk space and quota on the temp filesystem
  4. Inspect e.getCause() for permission details and fix OS-level access

Example fix

// before
File f = tempFolder.newFile("very-long-prefix-" + uuid + "-", ".someext.tmp");
// after
File f = tempFolder.newFile("sonar-", ".tmp");
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure temp base exists and is writable before newFile
if (!Files.isDirectory(tempBase) || !Files.isWritable(tempBase)) {
  throw new IllegalStateException("temp base missing or not writable: " + tempBase);
}
if ((prefix != null && prefix.length() > 100) || (suffix != null && suffix.length() > 50)) {
  throw new IllegalArgumentException("prefix/suffix too long");
}

Try / catch

try {
  return tempFolder.newFile(prefix, suffix);
} catch (IllegalStateException e) {
  log.error("Temp file creation failed: {}", e.getCause());
  throw new IOException("Unable to create temp file", e);
}

Prevention

When it happens

Trigger: Calling newFile()/newFile(prefix, suffix) when the file cannot be created: temp base deleted at runtime, write permission denied, disk/quota exhausted, or prefix/suffix containing illegal path characters.

Common situations: Long prefix/suffix exceeding filesystem filename limits (e.g. 255 bytes); concurrent cleanup removing the temp dir; restricted service accounts; full disks in CI containers.

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/93e1a2ad44b325ab. Report an issue: GitHub.

Appendix: source

Thrown at sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/utils/DefaultTempFolder.java:91

    }
    return dir;
  }

  @Override
  public File newFile() {
    return newFile(null, null);
  }

  @Override
  public File newFile(@Nullable String prefix, @Nullable String suffix) {
    return createTempFile(tempDir.toPath(), prefix, suffix).toFile();
  }

  private static Path createTempFile(Path baseDir, @Nullable String prefix, @Nullable String suffix) {
    try {
      return Files.createTempFile(baseDir, prefix, suffix);
    } catch (IOException e) {
      throw new IllegalStateException("Failed to create temp file", e);
    }
  }

  public void clean() {
    try {
      if (tempDir.exists()) {
        Files.walkFileTree(tempDir.toPath(), DeleteRecursivelyFileVisitor.INSTANCE);
      }
    } catch (IOException e) {
      LOG.error("Failed to delete temp folder", e);
    }
  }

  @Override
  public void start() {
    // nothing to do
  }

View on GitHub (pinned to 184c821202)