karatelabs/karate · warning

Failed to write xml for

Error message

Failed to write xml for {}: {}

What it means

JunitXmlWriter.writeSerialized() creates the output directory if needed and writes the pre-serialized XML string to the given file name. Failures (directory creation or Files.writeString) are caught and logged as a warning naming the target file, leaving the run unaffected but the XML file missing.

Solutions

  1. Verify the output directory path is writable and not occupied by a regular file
  2. Free disk space / raise quota on the target volume
  3. Sanitize or shorten the file name if it contains illegal or overly long characters
  4. Check process permissions (chown/chmod) on the output directory
Defensive patterns

Strategy: validation

Validate before calling

Path xml = outputDir.resolve(fileName);
if (Files.exists(xml) && Files.isDirectory(xml))
    throw new IllegalStateException("XML path occupied by a directory: " + xml);
Files.createDirectories(outputDir);

Try / catch

try {
    Files.createDirectories(outputDir);
    Files.writeString(outputDir.resolve(fileName), content);
} catch (Exception e) {
    logger.warn("Failed to write xml for {}: {}", fileName, e.getMessage(), e);
}

Prevention

When it happens

Trigger: Exception creating outputDir or writing fileName — permission denied, path is a file, disk full, or invalid characters in fileName for the target filesystem.

Common situations: Read-only CI workspace; outputDir collides with an existing file; disk quota exceeded during large suites; fileName derived from a feature name containing filesystem-illegal characters.

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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/output/JunitXmlWriter.java:101

     */
    public static String serializeFeature(FeatureResult result) {
        return featureToXml(result);
    }

    /** File name this feature's output is written under. */
    public static String fileNameFor(FeatureResult result) {
        return result.getFeature().getResource().getPackageQualifiedName() + ".xml";
    }

    /** Write pre-serialized content. Safe after the source result has been released. */
    public static void writeSerialized(String fileName, String content, Path outputDir) {
        try {
            if (!Files.exists(outputDir)) {
                Files.createDirectories(outputDir);
            }
            Files.writeString(outputDir.resolve(fileName), content);
        } catch (Exception e) {
            logger.warn("Failed to write xml for {}: {}", fileName, e.getMessage());
        }
    }

    public static void writeFeature(FeatureResult result, Path outputDir) {
        try {
            if (!Files.exists(outputDir)) {
                Files.createDirectories(outputDir);
            }

            String fileName = result.getFeature().getResource().getPackageQualifiedName() + ".xml";
            Path xmlPath = outputDir.resolve(fileName);
            String xml = featureToXml(result);
            Files.writeString(xmlPath, xml);
            logger.debug("JUnit XML written: {}", xmlPath);
        } catch (Exception e) {
            logger.warn("Failed to write JUnit XML for {}: {}", result.getDisplayName(), e.getMessage());
        }
    }

View on GitHub (pinned to a22eb90246)