{"record":{"id":"963984dbcf5394f0","repo":"sebastianbergmann/phpunit","slug":"could-not-parse-xml-from-empty-file-s","errorCode":null,"errorMessage":"Could not parse XML from empty file \"%s\"","messagePattern":"Could not parse XML from empty file \"(.+?)\"","errorType":"exception","errorClass":"PHPUnit\\Util\\Xml\\XmlException","httpStatus":null,"severity":"error","filePath":"src/Util/Xml/Loader.php","lineNumber":51,"sourceCode":"     */\n    public function loadFile(string $filename, bool $ignoreComments = false): DOMDocument\n    {\n        $reporting = error_reporting(0);\n        $contents  = file_get_contents($filename);\n\n        error_reporting($reporting);\n\n        if ($contents === false) {\n            throw new XmlException(\n                sprintf(\n                    'Could not read XML from file \"%s\"',\n                    $filename,\n                ),\n            );\n        }\n\n        if (trim($contents) === '') {\n            throw new XmlException(\n                sprintf(\n                    'Could not parse XML from empty file \"%s\"',\n                    $filename,\n                ),\n            );\n        }\n\n        return $this->load($contents, $ignoreComments);\n    }\n\n    /**\n     * @throws XmlException\n     */\n    public function load(string $actual, bool $ignoreComments = false): DOMDocument\n    {\n        if ($actual === '') {\n            throw new XmlException('Could not parse XML from empty string');\n        }","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Util/Xml/Loader.php#L33-L69","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Restore the real content of the file (git checkout the file or re-run the generator that produced it)","If the file is an unwanted leftover, delete it so PHPUnit's config auto-discovery is not misled","Validate configuration files in CI before the test step: `test -s phpunit.xml`","Ensure a minimal valid document exists: `<?xml version=\"1.0\" encoding=\"UTF-8\"?><phpunit/>`"],"exampleFix":"# before\n$ wc -c phpunit.xml   # 0\n$ vendor/bin/phpunit\n# XmlException: Could not parse XML from empty file \"phpunit.xml\"\n\n# after\n$ git checkout -- phpunit.xml\n$ vendor/bin/phpunit","handlingStrategy":"validation","validationCode":"$contents = file_get_contents($filename);\nif ($contents === false) {\n    throw new RuntimeException(\"Cannot read {$filename}\");\n}\nif (trim($contents) === '') {\n    throw new RuntimeException(\"{$filename} is empty\");\n}","typeGuard":null,"tryCatchPattern":"use PHPUnit\\Util\\Xml\\XmlException;\n\ntry {\n    $document = (new Loader)->loadFile($filename);\n} catch (XmlException $e) {\n    if (str_contains($e->getMessage(), 'empty file')) {\n        // regenerate the config/baseline instead of parsing on\n    }\n    throw $e;\n}","preventionTips":["Write configs atomically (tmp file + rename) so failed writes never leave empty targets","Check `test -s file` (size > 0) in CI for generated baseline and config files","Commit a valid minimal phpunit.xml rather than an empty placeholder"],"tags":["phpunit","xml","configuration","empty-file"],"backgroundTag":"xml-parse-error","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}