verbb / kint

Adds Kint, an in-app PHP debugger, to Craft CMS for use in Twig and PHP.
MIT License
3 stars 3 forks source link

Error dumping elements #8

Open mikesnoeren opened 1 month ago

mikesnoeren commented 1 month ago

Describe the bug

I can't dump elements anymore using {{ d() }}. For example:

They all result in the error below: Typed property craft\base\FieldLayoutComponent::$elementType must not be accessed before initialization

Notes:

Steps to reproduce

  1. Create any element (e.g. image, entry, product, matrix block)
  2. Dump it in template using {{ d() }}

Craft CMS version

5.1.6

Plugin version

4.0.0

Multi-site?

No

Additional context

No response

engram-design commented 1 month ago

Can't seem to replicate that, sorry! I've tested a few different scenarios:

Can you send through the full stack trace of the error? It might be coming from something unrelated like another plugin for example.

mikesnoeren commented 1 month ago

Thanks for checking it out, see below:

Slack Trace
Error: Typed property craft\base\FieldLayoutComponent::$elementType must not be accessed before initialization in /var/www/html/vendor/yiisoft/yii2/base/Model.php:719
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/base/Model.php(1007): yii\base\Model->getAttributes()
#1 [internal function]: yii\base\Model->getIterator()
#2 /var/www/html/vendor/kint-php/kint/src/Parser/IteratorPlugin.php(84): iterator_to_array(Object(craft\fieldlayoutelements\CustomField))
#3 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(613): Kint\Parser\IteratorPlugin->parse(Object(craft\fieldlayoutelements\CustomField), Object(Kint\Zval\InstanceValue), 2)
#4 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(548): Kint\Parser\Parser->applyPlugins(Object(craft\fieldlayoutelements\CustomField), Object(Kint\Zval\InstanceValue), 2)
#5 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(139): Kint\Parser\Parser->parseObject(Object(craft\fieldlayoutelements\CustomField), Object(Kint\Zval\Value))
#6 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(542): Kint\Parser\Parser->parse(Object(craft\fieldlayoutelements\CustomField), Object(Kint\Zval\Value))
#7 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(139): Kint\Parser\Parser->parseObject(Object(craft\fields\Entries), Object(Kint\Zval\Value))
#8 /var/www/html/vendor/kint-php/kint/src/Parser/ClosurePlugin.php(83): Kint\Parser\Parser->parse(Object(craft\fields\Entries), Object(Kint\Zval\Value))
#9 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(613): Kint\Parser\ClosurePlugin->parse(Object(Closure), Object(Kint\Zval\ClosureValue), 2)
#10 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(548): Kint\Parser\Parser->applyPlugins(Object(Closure), Object(Kint\Zval\ClosureValue), 2)
#11 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(139): Kint\Parser\Parser->parseObject(Object(Closure), Object(Kint\Zval\Value))
#12 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(385): Kint\Parser\Parser->parse(Object(Closure), Object(Kint\Zval\Value))
#13 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(132): Kint\Parser\Parser->parseArray(Array, Object(Kint\Zval\Value))
#14 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(542): Kint\Parser\Parser->parse(Array, Object(Kint\Zval\Value))
#15 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(139): Kint\Parser\Parser->parseObject(Object(craft\behaviors\EventBehavior), Object(Kint\Zval\Value))
#16 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(385): Kint\Parser\Parser->parse(Object(craft\behaviors\EventBehavior), Object(Kint\Zval\Value))
#17 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(132): Kint\Parser\Parser->parseArray(Array, Object(Kint\Zval\Value))
#18 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(542): Kint\Parser\Parser->parse(Array, Object(Kint\Zval\Value))
#19 /var/www/html/vendor/kint-php/kint/src/Parser/Parser.php(139): Kint\Parser\Parser->parseObject(Object(craft\elements\db\EntryQuery), Object(Kint\Zval\Value))
#20 /var/www/html/vendor/kint-php/kint/src/Kint.php(287): Kint\Parser\Parser->parse(Object(craft\elements\db\EntryQuery), Object(Kint\Zval\Value))
#21 /var/www/html/vendor/kint-php/kint/src/Kint.php(268): Kint\Kint->dumpVar(Object(craft\elements\db\EntryQuery), Object(Kint\Zval\Value))
#22 /var/www/html/vendor/kint-php/kint-twig/src/TwigExtension.php(178): Kint\Kint->dumpAll(Array, Array)
#23 /var/www/html/vendor/kint-php/kint-twig/src/TwigExtension.php(128): Kint\Twig\TwigExtension->dump('d', Object(craft\web\twig\Environment), Array, Array)
#24 /var/www/html/storage/runtime/compiled_templates/b2/b2075a381f3e499d54a7e1130c24249a.php(125): Kint\Twig\TwigExtension->Kint\Twig\{closure}(Object(craft\web\twig\Environment), Array, Array)
#25 /var/www/html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_b8b4552897e65a1a60bf7232e080b260->doDisplay(Array, Array)
#26 /var/www/html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#27 /var/www/html/vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#28 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(38): Twig\Template->render(Array)
#29 /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php(1347): Twig\TemplateWrapper->render(Array)
#30 /var/www/html/storage/runtime/compiled_templates/fa/faa3769558dd6c65ee0ccb572eccf0e4.php(118): twig_include(Object(craft\web\twig\Environment), Array, '_views/catalog/...', Array)
#31 /var/www/html/vendor/twig/twig/src/Template.php(171): __TwigTemplate_f121248bcb4d6f663df3a4e0bb80aa83->block_main(Array, Array)
#32 /var/www/html/storage/runtime/compiled_templates/c9/c97981529889acc53235d80fa45ca594.php(270): Twig\Template->displayBlock('main', Array, Array)
#33 /var/www/html/vendor/twig/twig/src/Template.php(171): __TwigTemplate_2e808ff141be9dcd5a3f354aac9ee84c->block_main_override(Array, Array)
#34 /var/www/html/storage/runtime/compiled_templates/c9/c97981529889acc53235d80fa45ca594.php(171): Twig\Template->displayBlock('main_override', Array, Array)
#35 /var/www/html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_2e808ff141be9dcd5a3f354aac9ee84c->doDisplay(Array, Array)
#36 /var/www/html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#37 /var/www/html/storage/runtime/compiled_templates/fa/faa3769558dd6c65ee0ccb572eccf0e4.php(44): Twig\Template->display(Array, Array)
#38 /var/www/html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_f121248bcb4d6f663df3a4e0bb80aa83->doDisplay(Array, Array)
#39 /var/www/html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#40 /var/www/html/vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#41 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(38): Twig\Template->render(Array)
#42 /var/www/html/vendor/twig/twig/src/Environment.php(280): Twig\TemplateWrapper->render(Array)
#43 /var/www/html/vendor/craftcms/cms/src/web/View.php(482): Twig\Environment->render('_views/catalog/...', Array)
#44 /var/www/html/vendor/craftcms/cms/src/web/View.php(535): craft\web\View->renderTemplate('_views/catalog/...', Array)
#45 /var/www/html/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(57): craft\web\View->renderPageTemplate('_views/catalog/...', Array, 'site')
#46 /var/www/html/vendor/yiisoft/yii2/web/Response.php(1100): craft\web\TemplateResponseFormatter->format(Object(craft\web\Response))
#47 /var/www/html/vendor/craftcms/cms/src/web/Response.php(338): yii\web\Response->prepare()
#48 /var/www/html/vendor/yiisoft/yii2/web/Response.php(340): craft\web\Response->prepare()
#49 /var/www/html/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#50 /var/www/html/public_html/index.php(12): yii\base\Application->run()
#51 {main}
Application info
PHP version: 8.2.15
OS version: Linux 6.7.12-orbstack-00201-g2ddb8f197a46
Database driver & version: MySQL 8.0.33
Image driver & version: GD 8.2.15
Craft edition & version: Craft Pro 5.1.6
Yii version: 2.0.48.1
Twig version: v3.8.0
Guzzle version: 7.8.1
Plugins
CKEditor: 4.0.6
Craft Commerce: 5.0.7
DigitalOcean Spaces Filesystem: 3.0.0-beta.1
Hyper: 2.0.0
Kint: 4.0.0
Mollie for Craft Commerce: 4.2.0
Navigation: 3.0.0
SEOmatic: 5.0.3
Vite: 5.0.0
engram-design commented 1 month ago

Right, I see the issue, you can't dump an element query.

{% set homepage = craft.entries.id(123).one() %}

{{ d(homepage.entriesField.one()) }}
{{ d(homepage.entriesField) }}

The second call breaks, which would return a craft\elements\db\EntryQuery object.

I'm not sure what we can do about this, as this is largely controlled by the kint-twig package.

mikesnoeren commented 1 month ago

Correct me if I am wrong, but hasn't this always just worked? I used to never get this error, but since Craft 5 I get it all the time, and I haven't changed my workflow.

engram-design commented 1 month ago

Indeed, in Craft 4 this wasn't an issue, but something to do with the FieldLayoutComponent::$elementType on an elementQuery (which didn't exist in Craft 4) is causing an issue here with the Kint parser, when trying to parse the deeply-nested object for an elementQuery.

It's either going to need some tweaking on our end or Craft's end. The "our end" tweak would be us forking the kint-twig package to add some exceptions to elementQuery objects.

From what I'm diving into, it's something of an issue where a craft\fieldlayoutelements\CustomField (our element field - in my testing an Entries field) doesn't have an elementType associated with it yet.

Looking into options!

mikesnoeren commented 1 month ago

Don't mind me asking, but what is the status on this? Is it something to do with kint-twig?

engram-design commented 1 month ago

We got halfway there, but looks like an infinite recursion bug now. I may just have to fork the kint-twig package and make some changes that are specific to Craft and an ElementQuery to get around this.

But it does beg the question - do you really need to dump the contents of a relational field? Note this is going to be the ElementQuery not a single element or a collection of elements, which would otherwise be using .one() and .all() respectively. There aren't any issues on my end dumping those values?

mikesnoeren commented 1 month ago

I've conducted further investigation into the issue with {{ d(...) }} not working consistently across our application. The problem seems to manifest under certain conditions, particularly when navigating away from entry pages and towards product pages or custom routes like /cart. Below are the observations and examples that might help identify the root cause.

Observations:

Additional Context:

engram-design commented 4 weeks ago

Thanks for all those test cases! So firstly, I've updated to 5.1.8 which helps with this error. But even with this, most variables are so large they crash the page. Still looking into it.

mikesnoeren commented 4 weeks ago

Great! I'll do some more testing today to see if the issue is fixed with release 5.1.8.

Crashing because of the large variables is something we've been running into for as long as I can remember. I always figured that wasn't really a bug, but maybe it's something that can be fixed down the line?

FWIW, we have always changed the depth_limit config setting to 3 or 4 to avoid crashes.

mikesnoeren commented 4 weeks ago

After a full day of testing, everything's running smoothly again!

We've noticed the occasional crashes when dealing with huge data sets, which seem to be a bit more frequent compared to Craft 4 (I'm not entirely sure). It would be nice if these could be looked into, but no pressure if it's not feasible.

engram-design commented 4 weeks ago

Good to know! And yeah, looking into what our options are with large data as I compare between Craft 4...

engram-design commented 3 days ago

I've just done some further testing on this, if you'd like to give my latest changes a go? To get this early, run composer require verbb/kint:"dev-craft-5 as 4.0.0".