sebastianbergmann/phpunit · error · CannotLoadBaselineException

Cannot read baseline %s, file does not exist

Error message

Cannot read baseline %s, file does not exist

What it means

PHPUnit was told to load a baseline (--baseline=... via configureBaseline) and Reader::read() found no regular file at that path (is_file failed). A baseline is the XML file generated by --generate-baseline that lets PHPUnit ignore known deprecations/notices/warnings; the run refuses to proceed without a readable one.

Source

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

use PHPUnit\Util\Xml\Loader as XmlLoader;
use PHPUnit\Util\Xml\XmlException;

/**
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
 *
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
 */
final readonly class Reader
{
    /**
     * @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()),
                ),
            );
        }

View on GitHub (pinned to f123cdb2a2)

Solutions

  1. Check the exact path passed to --baseline (relative paths resolve from where you invoke phpunit).
  2. Generate the file first: vendor/bin/phpunit --generate-baseline=.phpunit/baseline.xml.
  3. If it should already exist, verify it is committed and present in the CI image.

Example fix

# before
vendor/bin/phpunit --baseline=.phpunit/baseline.xml  # file missing

# after
vendor/bin/phpunit --generate-baseline=.phpunit/baseline.xml
vendor/bin/phpunit --baseline=.phpunit/baseline.xml
Defensive patterns

Strategy: validation

Validate before calling

// Before running with --baseline
if (!is_file($baselinePath)) {
    fwrite(STDERR, "Baseline {$baselinePath} missing; generating it first\n");
    exit(1); // or shell_exec('vendor/bin/phpunit --generate-baseline=' . escapeshellarg($baselinePath));
}

Try / catch

try {
    $baseline = (new Reader())->read($baselineFile);
} catch (CannotLoadBaselineException $e) {
    // regenerate and retry once, or fail with a clear message
}

Prevention

When it happens

Trigger: vendor/bin/phpunit --baseline=.phpunit/baseline.xml when the file was never generated, lives at another path, or the relative path resolves against a different working directory.

Common situations: First run on a new checkout before generating the baseline; path typos; the baseline file not committed to the repo; CI running from a different cwd so the relative path misses.

Related errors


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