-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: AssertionError occurs when using Validation in CLI #6452
Conversation
at SYSTEMPATH\Validation\FileRules.php:42 Backtrace: 1 [internal function] CodeIgniter\Debug\Exceptions()->errorHandler(2, 'assert(): assert($request instanceof IncomingRequest) failed', 'C:\\laragon\\www\\test\\vendor\\codeigniter4\\framework\\system\\Validation\\FileRules.php', 42, [...])
I aso ran into this issue when executing Backtrace
|
I created a pull request for solve this issue. Thank you! |
Do I need anything for merge this pull un develop branch? |
@mauricevb What version are you using? |
@daycry You need a normal description of the problem and tests, because it is not clear what behavior your PR corrects. |
@daycry Thank you for trying to improve CI4! You need to:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you use validation for CLI, now can use IncomingRequest only.
You must set assert($request instanceof CLIRequest ?? IncomingRequest)
, and now can use for CLI and Web
system/Validation/FileRules.php
Outdated
@@ -39,7 +40,7 @@ public function __construct(?RequestInterface $request = null) | |||
$request = Services::request(); | |||
} | |||
|
|||
assert($request instanceof IncomingRequest); | |||
assert($request instanceof CLIRequest ?? IncomingRequest) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not nullable, how would this work?
You can reproduce the error with this code for example CLI::prompt('Config file already exists, do you want to replace it?', [ 'y', 'n' ]) == 'n') When you try to validate a input with cli commands, you can see the error. |
CLI::prompt('Config file already exists, do you want to replace it?', [ 'y', 'n' ]) == 'n') I got:
|
I was able to reproduce. <?php
namespace App\Controllers;
use CodeIgniter\CLI\CLI;
class Home extends BaseController
{
public function index()
{
$email = CLI::prompt('What is your email?', null, 'required|valid_email');
}
} $ php public/index.php
What is your email? :
[AssertionError]
assert($request instanceof IncomingRequest)
at SYSTEMPATH/Validation/FileRules.php:42 |
CodeIgniter4/system/Validation/Validation.php Lines 551 to 560 in f1862a0
|
I use this code for copy Config file in Config namespace. if (file_exists($appPath . $path) && CLI::prompt('Config file already exists, do you want to replace it?', [ 'y', 'n' ]) == 'n') { [ErrorException] assert(): assert($request instanceof IncomingRequest) failed at SYSTEMPATH\Validation\FileRules.php:42 Backtrace: My first PR solve this error, because $request are CliRequest no IncomingRequest class assert($request instanceof IncomingRequest); -->assert($request instanceof Request); Because extends from Request, but phpstan check return error. Later, I try this change assert($request instanceof IncomingRequest || $request instanceof CLIRequest) but docker postgre return and error, I think that this error isn't from the code, later will tray again. Thank you! |
@kenjis In prompt CLI by default running validate if parameter CodeIgniter4/system/CLI/CLI.php Line 257 in f1862a0
CodeIgniter4/system/CLI/CLI.php Lines 313 to 333 in f1862a0
|
Do you mean the GitHub Action check? |
Yes, this works. $ php public/index.php
What is your email? :
The What is your email? field is required.
What is your email? : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Validation But This PR is workaround. |
This issue is more serious than what I thought first. $ php spark shield:setup
CodeIgniter v4.2.5 Command Line Tool - Server Time: 2022-09-01 11:41:17 UTC+09:00
Created: APPPATH/Config/Auth.php
Created: APPPATH/Config/AuthGroups.php
Updated: APPPATH/Controllers/BaseController.php
Updated: APPPATH/Config/Routes.php
Updated: We have updated file 'APPPATH/Config/Security.php' for security reasons.
Run `spark migrate --all` now? [y, n]: y
[AssertionError]
assert($request instanceof IncomingRequest)
at SYSTEMPATH/Validation/FileRules.php:42
Backtrace:
1 SYSTEMPATH/Validation/FileRules.php:42
assert(false, 'assert($request instanceof IncomingRequest)')
2 SYSTEMPATH/Validation/Validation.php:558
CodeIgniter\Validation\FileRules()->__construct()
3 SYSTEMPATH/Validation/Validation.php:122
CodeIgniter\Validation\Validation()->loadRuleSets()
4 SYSTEMPATH/CLI/CLI.php:324
CodeIgniter\Validation\Validation()->run([...])
5 SYSTEMPATH/CLI/CLI.php:257
CodeIgniter\CLI\CLI::validate('temp', 'y', [...])
6 VENDORPATH/codeigniter4/shield/src/Commands/Setup.php:316
CodeIgniter\CLI\CLI::prompt(' Run `spark migrate --all` now?', [...])
7 VENDORPATH/codeigniter4/shield/src/Commands/Setup.php:302
CodeIgniter\Shield\Commands\Setup()->cliPrompt(' Run `spark migrate --all` now?', [...])
8 VENDORPATH/codeigniter4/shield/src/Commands/Setup.php:92
CodeIgniter\Shield\Commands\Setup()->runMigrations()
9 VENDORPATH/codeigniter4/shield/src/Commands/Setup.php:79
CodeIgniter\Shield\Commands\Setup()->publishConfig()
10 SYSTEMPATH/CLI/Commands.php:63
CodeIgniter\Shield\Commands\Setup()->run([])
11 SYSTEMPATH/CLI/CommandRunner.php:65
CodeIgniter\CLI\Commands()->run('shield:setup', [])
12 SYSTEMPATH/CLI/CommandRunner.php:51
CodeIgniter\CLI\CommandRunner()->index([])
13 SYSTEMPATH/CodeIgniter.php:897
CodeIgniter\CLI\CommandRunner()->_remap('index', [...])
14 SYSTEMPATH/CodeIgniter.php:457
CodeIgniter\CodeIgniter()->runController(Object(CodeIgniter\CLI\CommandRunner))
15 SYSTEMPATH/CodeIgniter.php:336
CodeIgniter\CodeIgniter()->handleRequest(null, Object(Config\Cache), false)
16 SYSTEMPATH/CLI/Console.php:48
CodeIgniter\CodeIgniter()->run()
17 ROOTPATH/spark:98
CodeIgniter\CLI\Console()->run() |
I had the same problem today with fresh v4.2.4 and |
@kenjis and @jozefrebjak I cannot reproduce if: |
@datamweb Yes, this PR fixes the error. |
Should we hotfix this @kenjis ? |
@MGatner If possible, v4.2.6 should be released as soon as possible. Now the commands that use |
Did this happen because we are lacking tests? Or is it because CLI and requests are hard to test under "real" scenarios? Or...? |
Both. |
Description
at SYSTEMPATH\Validation\FileRules.php:42
Backtrace:
1 [internal function]
CodeIgniter\Debug\Exceptions()->errorHandler(2, 'assert(): assert($request instanceof IncomingRequest) failed', 'C:\laragon\www\test\vendor\codeigniter4\framework\system\Validation\FileRules.php', 42, [...])
Checklist: