wp-media / wp-rocket

Performance optimization plugin for WordPress
https://wp-rocket.me
GNU General Public License v2.0
687 stars 215 forks source link

Container V4 service provider lied about providing (options_debug) service #6865

Closed mostafa-hisham closed 2 weeks ago

mostafa-hisham commented 1 month ago

Before submitting an issue please check that you’ve completed the following steps:

Describe the bug After updating the container to V4, we cannot use options_debug in Rucss debug tool.

we are getting this error

Fatal error: Uncaught WP_Rocket\Dependencies\League\Container\Exception\ContainerException: Service provider lied about providing (options_debug) service in /home/mostafa/Local Sites/wp-copra/app/public/wp-content/plugins/wp-rocket/inc/Dependencies/League/Container/Container.php:195 Stack trace: #0 /home/mostafa/Local Sites/wp-copra/app/public/wp-content/plugins/wp-rocket/inc/Dependencies/League/Container/Container.php(118): WP_Rocket\Dependencies\League\Container\Container->resolve('options_debug') #1 /home/mostafa/Local Sites/wp-copra/app/public/wp-content/plugins/wp-rocket/inc/Dependencies/League/Container/Container.php(203): WP_Rocket\Dependencies\League\Container\Container->get('options_debug') #2 /home/mostafa/Local Sites/wp-copra/app/public/wp-content/plugins/wp-rocket/inc/Dependencies/League/Container/Container.php(118): WP_Rocket\Dependencies\League\Container\Container->resolve('options_debug') #3 /home/mostafa/Local Sites/wp-copra/app/public/wp-content/plugins/rucss-debug-tool/configs/binding.php(87): WP_Rocket\Dependencies\League\Container\Container->get('options_debug') #4 /home/mostafa/Local Sites/wp-copra/app/public/wp-content/plugins/rucss-debug-tool/inc/main.php(24): require_once('/home/mostafa/L...') #5 /home/mostafa/Local Sites/wp-copra/app/public/wp-includes/class-wp-hook.php(324): WPRocket\RUCSSDebugTool\init('') #6 /home/mostafa/Local Sites/wp-copra/app/public/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters('', Array) #7 /home/mostafa/Local Sites/wp-copra/app/public/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #8 /home/mostafa/Local Sites/wp-copra/app/public/wp-content/plugins/wp-rocket/inc/main.php(102): do_action('wp_rocket_loade...') #9 /home/mostafa/Local Sites/wp-copra/app/public/wp-includes/class-wp-hook.php(324): rocket_init('') #10 /home/mostafa/Local Sites/wp-copra/app/public/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array) #11 /home/mostafa/Local Sites/wp-copra/app/public/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #12 /home/mostafa/Local Sites/wp-copra/app/public/wp-settings.php(550): do_action('plugins_loaded') #13 /home/mostafa/Local Sites/wp-copra/app/public/wp-config.php(108): require_once('/home/mostafa/L...') #14 /home/mostafa/Local Sites/wp-copra/app/public/wp-load.php(50): require_once('/home/mostafa/L...') #15 /home/mostafa/Local Sites/wp-copra/app/public/wp-blog-header.php(13): require_once('/home/mostafa/L...') #16 /home/mostafa/Local Sites/wp-copra/app/public/index.php(17): require('/home/mostafa/L...') #17 {main} thrown in /home/mostafa/Local Sites/wp-copra/app/public/wp-content/plugins/wp-rocket/inc/Dependencies/League/Container/Container.php on line 195

Notice: Function is_embed was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.) in /home/mostafa/Local Sites/wp-copra/app/public/wp-includes/functions.php on line 6078

Notice: Function is_search was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.) in /home/mostafa/Local Sites/wp-copra/app/public/wp-includes/functions.php on line 6078
There has been a critical error on this website.
Learn more about troubleshooting WordPress.

With @CrochetFeve0251 help we checked and in wp-rocket we are bailing out here and we don't serve it

To Reproduce Steps to reproduce the behavior:

  1. Add rucss-debug-tool and switch to the branch of this PR
  2. open admin page
  3. See error

Expected behavior We should use the options_debug like this $container->get('options_debug' ),

Screenshots image

Tabrisrp commented 3 weeks ago

Reproduce the issue ✅

Yes

Identify the root cause ✅

This happens because options_debug is always added to the provides array of the service provider, but there is an early bail-out in the register() method, so it's creating the error.

Scope a solution ✅

The options_debug value should be added to the provides array only if there is a service registered, so instead of adding it directly to the array, it should be conditionally added in the boot() method, like for the services.

Estimate the effort ✅

Effort XS

Miraeld commented 3 weeks ago

Looks good to me

Khadreal commented 3 weeks ago

@mostafa-hisham I'm getting the error, this is after adding options_debug to boot(). Any idea why this might be happening ?

Fatal error: Uncaught TypeError: WPRocket\RUCSSDebugTool\Admin\Jobs\Subscriber::__construct(): Argument #1 ($query) must be of type WP_Rocket\Engine\Optimization\RUCSS\Database\Queries\UsedCSS, string given in /var/www/html/wp-content/plugins/rucss/inc/Admin/Jobs/Subscriber.php on line 35 TypeError: WPRocket\RUCSSDebugTool\Admin\Jobs\Subscriber::__construct(): Argument #1 ($query) must be of type WP_Rocket\Engine\Optimization\RUCSS\Database\Queries\UsedCSS, string given in /var/www/html/wp-content/plugins/rucss/inc/Admin/Jobs/Subscriber.php on line 35 Call Stack: 0.0009 481856 1. {main}() /var/www/html/wp-admin/plugins.php:0 0.0015 488224 2. require_once('/var/www/html/wp-admin/admin.php') /var/www/html/wp-admin/plugins.php:10 0.0018 488736 3. require_once('/var/www/html/wp-load.php') /var/www/html/wp-admin/admin.php:34 0.0022 489544 4. require_once('/var/www/html/wp-config.php') /var/www/html/wp-load.php:50 0.0041 586456 5. require_once('/var/www/html/wp-settings.php') /var/www/html/wp-config.php:260 0.1486 2049176 6. do_action($hook_name = 'plugins_loaded') /var/www/html/wp-settings.php:555 0.1486 2049392 7. WP_Hook->do_action($args = [0 => '']) /var/www/html/wp-includes/plugin.php:517 0.1486 2049392 8. WP_Hook->apply_filters($value = '', $args = [0 => '']) /var/www/html/wp-includes/class-wp-hook.php:348 0.2084 2273848 9. rocket_init('') /var/www/html/wp-includes/class-wp-hook.php:324 0.4330 3541208 10. do_action($hook_name = 'wp_rocket_loaded') /var/www/html/wp-content/plugins/wp-rocket/inc/main.php:102 0.4330 3541424 11. WP_Hook->do_action($args = [0 => '']) /var/www/html/wp-includes/plugin.php:517 0.4330 3541424 12. WP_Hook->apply_filters($value = '', $args = [0 => '']) /var/www/html/wp-includes/class-wp-hook.php:348 0.4330 3541856 13. WPRocket\RUCSSDebugTool\init('') /var/www/html/wp-includes/class-wp-hook.php:324 0.4406 3585696 14. WP_Rocket\Dependencies\League\Container\Container->get($id = 'WPRocket\\RUCSSDebugTool\\Admin\\Jobs\\Subscriber') /var/www/html/wp-content/plugins/rucss/inc/main.php:27 0.4406 3585696 15. WP_Rocket\Dependencies\League\Container\Container->resolve($id = 'WPRocket\\RUCSSDebugTool\\Admin\\Jobs\\Subscriber', $new = ???) /var/www/html/wp-content/plugins/wp-rocket/inc/Dependencies/League/Container/Container.php:118 0.4412 3585696 16. WP_Rocket\Dependencies\League\Container\ReflectionContainer->get($id = 'WPRocket\\RUCSSDebugTool\\Admin\\Jobs\\Subscriber', $args = ???) /var/www/html/wp-content/plugins/wp-rocket/inc/Dependencies/League/Container/Container.php:203 0.4422 3586312 17. ReflectionClass->newInstanceArgs($args = [0 => 'rucss_used_css_query', 1 => class WPRocket\RUCSSDebugTool\Admin\Common\Renderer { protected $template_basepath = '/var/www/html/wp-content/plugins/rucss/configs/../templates/' }, 2 => class WP_Rocket\Logger\Logger { }]) /var/www/html/wp-content/plugins/rucss/vendor/wp-media/wp-rocket/inc/Dependencies/League/Container/ReflectionContainer.php:58 0.4422 3586440 18. WPRocket\RUCSSDebugTool\Admin\Jobs\Subscriber->__construct($query = 'rucss_used_css_query', $renderer = class WPRocket\RUCSSDebugTool\Admin\Common\Renderer { protected $template_basepath = '/var/www/html/wp-content/plugins/rucss/configs/../templates/' }, $logger = class WP_Rocket\Logger\Logger { }) /var/www/html/wp-content/plugins/rucss/vendor/wp-media/wp-rocket/inc/Dependencies/League/Container/ReflectionContainer.php:58

CrochetFeve0251 commented 3 weeks ago

@Khadreal this means that the class WP_Rocket\Engine\Optimization\RUCSS\Database\Queries\UsedCSS is not defined on the container.

Khadreal commented 3 weeks ago

@CrochetFeve0251 yes, but the class is defined. Could it be because this class WPRocket\RUCSSDebugTool\Admin\Jobs\Subscriber::class is not defined in the configs/binding file

[Update] I added WPRocket\RUCSSDebugTool\Admin\Jobs\Subscriber::class to the container with the arguments and it doesn't throw any error.

CrochetFeve0251 commented 3 weeks ago

@Khadreal I think there is something weird going on with the parameters given.

Some get on the container have two parameters, and we are fetching the parameter before passing it to the definition, which is also necessary.

MathieuLamiot commented 2 weeks ago

Will be tested by Service team: https://github.com/wp-media/rucss-debug-tool/pull/16