elastic/elasticsearch · error · RuntimeException

Error parsing xml file in {}

Error message

Error parsing xml file in {}

What it means

Thrown by the XML class-relocation transformer used during Gradle shadow/jar shading. It walks XML resource files (typically plugin descriptors like META-INF/xml-style descriptors) to rewrite class references to their relocated packages. The broad catch wraps any SAX/IO/parser failure with the offending input stream's toString().

Source

Thrown at build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/shadow/XmlClassRelocationTransformer.java:68

        }
        return false;
    }

    @Override
    public void transform(TransformerContext context) {
        try {
            BufferedInputStream bis = new BufferedInputStream(context.getInputStream());
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(bis);
            doc.getDocumentElement().normalize();
            Node root = doc.getDocumentElement();
            walkThroughNodes(root, context);
            if (hasTransformedResource == false) {
                this.doc = null;
            }
        } catch (Exception e) {
            throw new RuntimeException("Error parsing xml file in " + context.getInputStream(), e);
        }
    }

    private static String getRelocatedClass(String className, TransformerContext context) {
        Set<Relocator> relocators = context.getRelocators();
        if (className != null && className.length() > 0 && relocators != null) {
            for (Relocator relocator : relocators) {
                if (relocator.canRelocateClass(className)) {
                    RelocateClassContext relocateClassContext = new RelocateClassContext(className);
                    return relocator.relocateClass(relocateClassContext);
                }
            }
        }

        return className;
    }

    private void walkThroughNodes(Node node, TransformerContext context) {

View on GitHub (pinned to db6a809a66)

Solutions

  1. Inspect the full cause chain (the wrapped Exception e) to find the SAXParseException line/column.
  2. Identify which jar/resource owns the stream shown in the message and open the original XML to validate it.
  3. If the XML is intentionally non-relocatable, exclude that resource from the transformer's input set.
  4. Reproduce with a standalone DocumentBuilder.parse() on the extracted file to confirm well-formedness.

Example fix

// before
} catch (Exception e) {
    throw new RuntimeException("Error parsing xml file in " + context.getInputStream(), e);
}

// after - keep the resource identity without re-reading a possibly-consumed stream
} catch (Exception e) {
    throw new RuntimeException("Error parsing xml file in " + context.getName(), e);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate the XML is well-formed before passing it to the transformer
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
try (InputStream in = context.getInputStream()) {
    f.newDocumentBuilder().parse(in);
} catch (SAXException | IOException ok) {
    // skip this resource rather than feed invalid XML to the transformer
    return;
}

Try / catch

try {
    transformer.processResource(context);
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Error parsing xml file in")) {
        // log the offending resource and continue shading other resources
        logger.warn("Skipping unparseable XML resource", e);
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: processResource() is invoked on a TransformerContext whose getInputStream() returns malformed, truncated, or non-XML content; dBuilder.parse(bis) throws SAXParseException, or the stream is already consumed/closed.

Common situations: A shaded dependency ships a corrupt XML descriptor; a resource was patched by another transformer leaving invalid XML; classpath contains a renamed XML file that no longer parses; encoding/BOM issues in a hand-edited descriptor.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/80ac920beb246de4. Report an issue: GitHub.