sebastianbergmann/phpunit · error · PHPUnit\Util\Xml\XmlException

Could not parse XML from empty file "%s"

Error message

Could not parse XML from empty file "%s"

What it means

After successfully reading a file, Xml\Loader::loadFile() checks that its content is non-empty; a file consisting of nothing or only whitespace throws XmlException('Could not parse XML from empty file "<path>"'). PHPUnit distinguishes this from unreadable files so you know the file exists but has no parseable content.

Source

Thrown at src/Util/Xml/Loader.php:51

     */
    public function loadFile(string $filename, bool $ignoreComments = false): DOMDocument
    {
        $reporting = error_reporting(0);
        $contents  = file_get_contents($filename);

        error_reporting($reporting);

        if ($contents === false) {
            throw new XmlException(
                sprintf(
                    'Could not read XML from file "%s"',
                    $filename,
                ),
            );
        }

        if (trim($contents) === '') {
            throw new XmlException(
                sprintf(
                    'Could not parse XML from empty file "%s"',
                    $filename,
                ),
            );
        }

        return $this->load($contents, $ignoreComments);
    }

    /**
     * @throws XmlException
     */
    public function load(string $actual, bool $ignoreComments = false): DOMDocument
    {
        if ($actual === '') {
            throw new XmlException('Could not parse XML from empty string');
        }

View on GitHub (pinned to f123cdb2a2)

Solutions

  1. Restore the real content of the file (git checkout the file or re-run the generator that produced it)
  2. If the file is an unwanted leftover, delete it so PHPUnit's config auto-discovery is not misled
  3. Validate configuration files in CI before the test step: `test -s phpunit.xml`
  4. Ensure a minimal valid document exists: `<?xml version="1.0" encoding="UTF-8"?><phpunit/>`

Example fix

# before
$ wc -c phpunit.xml   # 0
$ vendor/bin/phpunit
# XmlException: Could not parse XML from empty file "phpunit.xml"

# after
$ git checkout -- phpunit.xml
$ vendor/bin/phpunit
Defensive patterns

Strategy: validation

Validate before calling

$contents = file_get_contents($filename);
if ($contents === false) {
    throw new RuntimeException("Cannot read {$filename}");
}
if (trim($contents) === '') {
    throw new RuntimeException("{$filename} is empty");
}

Try / catch

use PHPUnit\Util\Xml\XmlException;

try {
    $document = (new Loader)->loadFile($filename);
} catch (XmlException $e) {
    if (str_contains($e->getMessage(), 'empty file')) {
        // regenerate the config/baseline instead of parsing on
    }
    throw $e;
}

Prevention

When it happens

Trigger: Pointing PHPUnit's configuration loading (phpunit.xml, --configuration, baseline or migration input) at a file that is zero bytes or contains only blank lines/spaces — for example a config created by `touch`, truncated by a failed write, or emptied by a botched CI cache restore.

Common situations: Config templates generated by scripts that failed mid-write; CI caches restored empty; files accidentially truncated during merge conflicts; committing an empty placeholder phpunit.xml.

Related errors


AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23). Data as JSON: /api/errors/963984dbcf5394f0. Report an issue: GitHub.