{"record":{"id":"387763ae07d6a6d4","repo":"laravel/framework","slug":"failed-to-extract-the-request-port-ensure-the-log","errorCode":null,"errorMessage":"Failed to extract the request port. Ensure the log line contains a valid port: {$line}","messagePattern":"Failed to extract the request port\\. Ensure the log line contains a valid port: (.+?)","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"warning","filePath":"src/Illuminate/Foundation/Console/ServeCommand.php","lineNumber":446,"sourceCode":"        preg_match($regex, $line, $matches);\n\n        return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]);\n    }\n\n    /**\n     * Get the request port from the given PHP server output.\n     *\n     * @param  string  $line\n     * @return int\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public static function getRequestPortFromLine($line)\n    {\n        preg_match('/(\\[\\w+\\s\\w+\\s\\d+\\s[\\d:]+\\s\\d{4}\\]\\s)?:(\\d+)\\s(?:(?:\\w+$)|(?:\\[.*))/', $line, $matches);\n\n        if (! isset($matches[2])) {\n            throw new \\InvalidArgumentException(\"Failed to extract the request port. Ensure the log line contains a valid port: {$line}\");\n        }\n\n        return (int) $matches[2];\n    }\n}\n","sourceCodeStart":428,"sourceCodeEnd":452,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Foundation/Console/ServeCommand.php#L428-L452","documentation":"ServeCommand::getRequestPortFromLine() parses the stdout of PHP's built-in dev server (php artisan serve) to extract the listening port. If a given log line does not match the regex capturing a port in group 2, it throws InvalidArgumentException. This is internal to the dev-server watcher and is not part of normal request handling.","triggerScenarios":"The ServeCommand port-revealing loop calls getRequestPortFromLine() on each line of server output; if a line lacks the ':PORT ' token pattern (e.g. a PHP startup banner, an error message, or a non-conforming PHP version's output), the regex misses and the exception fires.","commonSituations":"Non-standard PHP CLI output format (newer/older PHP version, custom php.ini banner). A PHP warning emitted on startup before the '[date] :port' line. Manually feeding getRequestPortFromLine an arbitrary string (e.g. custom integration with artisan serve output).","solutions":["Update Laravel to a version whose regex matches your PHP version's server output format.","If calling getRequestPortFromLine directly, wrap it in try/catch and skip non-port lines.","For custom integrations, pre-filter lines with str_contains($line, ':') before parsing.","Pass the port explicitly via php artisan serve --port=8000 so port detection is not needed."],"exampleFix":"// before\n$port = ServeCommand::getRequestPortFromLine($line);\n\n// after — only parse candidate lines\nif (preg_match('/:\\d+\\s/', $line)) {\n    $port = ServeCommand::getRequestPortFromLine($line);\n}","handlingStrategy":"try-catch","validationCode":"// Only invoke the parser on lines plausibly containing a port\nif (preg_match('/:\\d+\\s/', $line)) {\n    $port = ServeCommand::getRequestPortFromLine($line);\n}","typeGuard":"function lineLooksLikeServerPortEntry(string $line): bool {\n    return (bool) preg_match('/:\\d+\\s\\S+/', $line);\n}","tryCatchPattern":"try {\n    $port = \\Illuminate\\Foundation\\Console\\ServeCommand::getRequestPortFromLine($line);\n} catch (\\InvalidArgumentException $e) {\n    // not a server-port line; skip\n    continue;\n}","preventionTips":["Pass --port explicitly to artisan serve to avoid runtime detection.","Keep Laravel updated so the parser regex matches your PHP version's output.","When integrating with artisan serve output, pre-filter lines before parsing.","Treat this parser as best-effort: wrap it in try/catch for any custom usage."],"tags":["laravel","artisan","dev-server","cli","parsing"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}