sebastianbergmann/phpunit · error · CannotLoadBaselineException

Cannot read baseline %s: %s

Error message

Cannot read baseline %s: %s

What it means

The baseline file exists but PHPUnit could not parse it as XML: XmlLoader::loadFile() raised an XmlException whose message (libxml error detail) is embedded in this CannotLoadBaselineException. A baseline must be the well-formed XML document that --generate-baseline writes (a <files> root with a version attribute).

Source

Thrown at src/Runner/Baseline/Reader.php:51

     * @param non-empty-string $baselineFile
     *
     * @throws CannotLoadBaselineException
     */
    public function read(string $baselineFile): Baseline
    {
        if (!is_file($baselineFile)) {
            throw new CannotLoadBaselineException(
                sprintf(
                    'Cannot read baseline %s, file does not exist',
                    $baselineFile,
                ),
            );
        }

        try {
            $document = (new XmlLoader)->loadFile($baselineFile);
        } catch (XmlException $e) {
            throw new CannotLoadBaselineException(
                sprintf(
                    'Cannot read baseline %s: %s',
                    $baselineFile,
                    trim($e->getMessage()),
                ),
            );
        }

        $documentElement = $document->documentElement;

        assert($documentElement !== null);

        $version = (int) $documentElement->getAttribute('version');

        if ($version !== Baseline::VERSION) {
            throw new CannotLoadBaselineException(
                sprintf(
                    'Cannot read baseline %s, version %d is not supported',

View on GitHub (pinned to f123cdb2a2)

Solutions

  1. Validate the file: xmllint --noout .phpunit/baseline.xml and fix what it reports (often conflict markers or a truncated tail).
  2. If unsure, regenerate from scratch: vendor/bin/phpunit --generate-baseline=<path> (overwrites with valid XML).
  3. Confirm --baseline really points at a baseline document, not another XML file.

Example fix

# before
vendor/bin/phpunit --baseline=.phpunit/baseline.xml  # malformed XML

# after
xmllint --noout .phpunit/baseline.xml   # fix reported errors, or:
vendor/bin/phpunit --generate-baseline=.phpunit/baseline.xml
Defensive patterns

Strategy: validation

Validate before calling

// Validate baseline XML before the run
$doc = new DOMDocument();
if (!$doc->load($baselinePath)) {
    fwrite(STDERR, "Baseline {$baselinePath} is not well-formed XML\n");
    exit(1);
}

Prevention

When it happens

Trigger: Hand-edited baseline with broken tags; file truncated by an interrupted write; merge-conflict markers (<<<<<<<) left in; --baseline pointing at a different XML artifact (e.g. a coverage XML with another root element).

Common situations: Git merge conflicts on a shared baseline; scripts appending to the file; encoding corruption; a tool stripping the root element.

Related errors


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