karatelabs/karate · warning

Failed to backup existing dir

Error message

Failed to backup existing dir '{}': {}

What it means

Before a run, Suite can back up an existing output directory by moving it (Files.move) to a suffixed backup path. If the move fails, this warning is logged and the run proceeds anyway — old output may be overwritten or mixed with new output.

Solutions

  1. Close programs holding the output dir open (IDE report viewer, browser, tail -f) and re-run
  2. Check write permissions on the parent directory of the output dir
  3. Delete the old output dir manually before running
  4. On Windows, exclude the reports dir from antivirus/indexing that can lock files mid-move

Example fix

// before: run with reports locked by an open viewer
mvn test -Dkarate.options="target/karate-reports"
// after: clean the dir first so nothing needs backing up
mvn clean test
Defensive patterns

Strategy: validation

Validate before calling

java.nio.file.Path out = java.nio.file.Path.of(outputDir);
if (java.nio.file.Files.exists(out)) {
    // ensure we can move it: parent writable and nothing locked
    if (!java.nio.file.Files.isWritable(out.getParent()))
        throw new IllegalStateException("cannot backup, parent not writable: " + out.getParent());
}

Try / catch

try { new Suite(builder).run(); }
catch (Exception e) {
    // backup failure is only a warning; if stale output matters, verify freshness
    logger.info("run finished; confirm reports are from this run: " + outputDir);
}

Prevention

When it happens

Trigger: Suite construction (dryRun/cleanup option) where the existing outputDir cannot be moved: the directory is open in another process (e.g. an IDE or file watcher on Windows), permission denied on the parent, or the backup target path is locked/non-writable.

Common situations: target/karate-reports open in a browser/IDE on Windows (file locking); CI workspace with leftover read-only artifacts; antivirus scanning the reports directory during the move.

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 karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/b17992dcc0adfd0e. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/Suite.java:1111

            DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");

    private void backupReportDirIfExists() {
        if (!Files.exists(outputDir)) {
            return;
        }
        String timestamp = LocalDateTime.now().format(BACKUP_DATE_FORMAT);
        String baseName = outputDir.getFileName() + "_" + timestamp;
        Path backupPath = outputDir.resolveSibling(baseName);
        int suffix = 1;
        while (Files.exists(backupPath)) {
            backupPath = outputDir.resolveSibling(baseName + "_" + suffix);
            suffix++;
        }
        try {
            Files.move(outputDir, backupPath);
            logger.info("backed up existing output to: {}", backupPath);
        } catch (Exception e) {
            logger.warn("Failed to backup existing dir '{}': {}", outputDir, e.getMessage());
        }
    }

    // ========== Accessors (for private fields) ==========

    public String getOutputDir() {
        return outputDir.toString();
    }

    public Map<String, Object> getCallSingleCache() {
        return CALLSINGLE_CACHE;
    }

    public ReentrantLock getCallSingleLock() {
        return callSingleLock;
    }

    /**

View on GitHub (pinned to a22eb90246)