laravel/framework · warning · InvalidArgumentException
Failed to extract the request port. Ensure the log line cont
Error message
Failed to extract the request port. Ensure the log line contains a valid port: {$line} What it means
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.
Source
Thrown at src/Illuminate/Foundation/Console/ServeCommand.php:446
preg_match($regex, $line, $matches);
return Carbon::createFromFormat('D M d H:i:s Y', $matches[1]);
}
/**
* Get the request port from the given PHP server output.
*
* @param string $line
* @return int
*
* @throws \InvalidArgumentException
*/
public static function getRequestPortFromLine($line)
{
preg_match('/(\[\w+\s\w+\s\d+\s[\d:]+\s\d{4}\]\s)?:(\d+)\s(?:(?:\w+$)|(?:\[.*))/', $line, $matches);
if (! isset($matches[2])) {
throw new \InvalidArgumentException("Failed to extract the request port. Ensure the log line contains a valid port: {$line}");
}
return (int) $matches[2];
}
}
View on GitHub (pinned to e0f6eb3518)
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.
Example fix
// before
$port = ServeCommand::getRequestPortFromLine($line);
// after — only parse candidate lines
if (preg_match('/:\d+\s/', $line)) {
$port = ServeCommand::getRequestPortFromLine($line);
} Defensive patterns
Strategy: try-catch
Validate before calling
// Only invoke the parser on lines plausibly containing a port
if (preg_match('/:\d+\s/', $line)) {
$port = ServeCommand::getRequestPortFromLine($line);
} Type guard
function lineLooksLikeServerPortEntry(string $line): bool {
return (bool) preg_match('/:\d+\s\S+/', $line);
} Try / catch
try {
$port = \Illuminate\Foundation\Console\ServeCommand::getRequestPortFromLine($line);
} catch (\InvalidArgumentException $e) {
// not a server-port line; skip
continue;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- Invalid database connection [{$db}].
- Database was not created. Aborting migration.
- The --model and --except options cannot be combined.
- Callback must be a callable, callback array, or a 'Class@met
- Auth guard [{$name}] is not defined.
AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11).
Data as JSON: /api/errors/387763ae07d6a6d4.
Report an issue: GitHub.