wintercms / winter

Free, open-source, self-hosted CMS platform based on the Laravel PHP Framework.
https://wintercms.com
MIT License
1.39k stars 195 forks source link

Error when inserting component through twig #557

Closed WebVPF closed 1 year ago

WebVPF commented 2 years ago

Winter CMS Build

Other (please specify below)

PHP Version

8.0

Database engine

MySQL/MariaDB

Plugins installed

No response

Issue description

My local website is down after today's update (Winter CMS 1.2) The error is caused by the code inserted into the file \plugins\webvpf\acme\components\item\default.htm:

{% component 'ShareButtons' %}

2022-05-16_163807

Steps to replicate

updated today

Workaround

No response

WebVPF commented 2 years ago

If I remove string {% component 'ShareButtons' %}, then the site starts working.

bennothommo commented 2 years ago

@WebVPF could you provide the full stack trace for us?

WebVPF commented 2 years ago

@bennothommo like this?

74 | ReflectionClass->__toString() | ~/vendor/twig/twig/src/Node/Expression/CallExpression.php | 303
73 | Twig\Node\Expression\CallExpression->reflectCallable(…) | ~/vendor/twig/twig/src/Node/Expression/CallExpression.php | 30
72 | Twig\Node\Expression\CallExpression->compileCallable(…) | ~/vendor/twig/twig/src/Node/Expression/FilterExpression.php | 38
71 | Twig\Node\Expression\FilterExpression->compile(…) | ~/vendor/twig/twig/src/Compiler.php | 74
70 | Twig\Compiler->subcompile(…) | ~/vendor/twig/twig/src/Node/PrintNode.php | 35
69 | Twig\Node\PrintNode->compile(…) | ~/vendor/twig/twig/src/Node/Node.php | 86
68 | Twig\Node\Node->compile(…) | ~/vendor/twig/twig/src/Node/Node.php | 86
67 | Twig\Node\Node->compile(…) | ~/vendor/twig/twig/src/Compiler.php | 74
66 | Twig\Compiler->subcompile(…) | ~/vendor/twig/twig/src/Node/ForNode.php | 84
65 | Twig\Node\ForNode->compile(…) | ~/vendor/twig/twig/src/Node/Node.php | 86
64 | Twig\Node\Node->compile(…) | ~/vendor/twig/twig/src/Node/Node.php | 86
63 | Twig\Node\Node->compile(…) | ~/vendor/twig/twig/src/Compiler.php | 74
62 | Twig\Compiler->subcompile(…) | ~/vendor/twig/twig/src/Node/ModuleNode.php | 310
61 | Twig\Node\ModuleNode->compileDisplay(…) | ~/vendor/twig/twig/src/Node/ModuleNode.php | 84
60 | Twig\Node\ModuleNode->compileTemplate(…) | ~/vendor/twig/twig/src/Node/ModuleNode.php | 65
59 | Twig\Node\ModuleNode->compile(…) | ~/vendor/twig/twig/src/Compiler.php | 60
58 | Twig\Compiler->compile(…) | ~/vendor/twig/twig/src/Environment.php | 505
57 | Twig\Environment->compile(…) | ~/vendor/twig/twig/src/Environment.php | 516
56 | Twig\Environment->compileSource(…) | ~/vendor/twig/twig/src/Environment.php | 348
55 | Twig\Environment->loadTemplate(…) | ~/vendor/twig/twig/src/Environment.php | 309
54 | Twig\Environment->load(…) | ~/modules/cms/classes/Controller.php | 1060
53 | Cms\Classes\Controller->renderPartial(…) | ~/modules/cms/classes/Controller.php | 1192
52 | Cms\Classes\Controller->renderComponent(…) | ~/modules/cms/twig/Extension.php | 102
51 | Cms\Twig\Extension->componentFunction(…) | ~/storage/cms/twig/8e/8e6a4ebbf5c6874b0c64ef63b2f9ec09404785b46f8c4d505179b996f62cbb0d.php | 75
50 | __TwigTemplate_4e36c5a555ed739cf6de28dd809543095609bace7b3c374e0f28ce952c2c23e9->doDisplay(…) | ~/vendor/twig/twig/src/Template.php | 394
49 | Twig\Template->displayWithErrorHandling(…) | ~/vendor/twig/twig/src/Template.php | 367
48 | Twig\Template->display(…) | ~/vendor/twig/twig/src/Template.php | 379
47 | Twig\Template->render(…) | ~/vendor/twig/twig/src/TemplateWrapper.php | 40
46 | Twig\TemplateWrapper->render(…) | ~/modules/cms/classes/Controller.php | 1061
45 | Cms\Classes\Controller->renderPartial(…) | ~/modules/cms/classes/Controller.php | 1192
44 | Cms\Classes\Controller->renderComponent(…) | ~/modules/cms/twig/Extension.php | 102
43 | Cms\Twig\Extension->componentFunction(…) | ~/storage/cms/twig/ef/ef294c58c9a7c76bb4f374c1a5f60e6453f8c4a9e5eaae84b17ff395ea170246.php | 40
42 | __TwigTemplate_2c60529af6d6cebfb6837bb47e74f2a2b9a673dd02d4a9fe8fce43fb23082d5d->doDisplay(…) | ~/vendor/twig/twig/src/Template.php | 394
41 | Twig\Template->displayWithErrorHandling(…) | ~/vendor/twig/twig/src/Template.php | 367
40 | Twig\Template->display(…) | ~/vendor/twig/twig/src/Template.php | 379
39 | Twig\Template->render(…) | ~/vendor/twig/twig/src/TemplateWrapper.php | 40
38 | Twig\TemplateWrapper->render(…) | ~/modules/cms/classes/Controller.php | 422
37 | Cms\Classes\Controller->runPage(…) | ~/modules/cms/classes/Controller.php | 217
36 | Cms\Classes\Controller->run(…) | ~/modules/cms/classes/CmsController.php | 50
35 | Cms\Classes\CmsController->run(…) | ~/vendor/laravel/framework/src/Illuminate/Routing/Controller.php | 54
34 | Illuminate\Routing\Controller->callAction(…) | ~/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php | 45
33 | Illuminate\Routing\ControllerDispatcher->dispatch(…) | ~/vendor/laravel/framework/src/Illuminate/Routing/Route.php | 261
32 | Illuminate\Routing\Route->runController() | ~/vendor/laravel/framework/src/Illuminate/Routing/Route.php | 204
31 | Illuminate\Routing\Route->run() | ~/vendor/laravel/framework/src/Illuminate/Routing/Router.php | 725
30 | Illuminate\Routing\Router->Illuminate\Routing\{closure}(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 141
29 | Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(…) | ~/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php | 50
28 | Illuminate\Routing\Middleware\SubstituteBindings->handle(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 180
27 | Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(…) | ~/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php | 49
26 | Illuminate\View\Middleware\ShareErrorsFromSession->handle(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 180
25 | Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(…) | ~/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php | 121
24 | Illuminate\Session\Middleware\StartSession->handleStatefulRequest(…) | ~/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php | 64
23 | Illuminate\Session\Middleware\StartSession->handle(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 180
22 | Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(…) | ~/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php | 37
21 | Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 180
20 | Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(…) | ~/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php | 67
19 | Illuminate\Cookie\Middleware\EncryptCookies->handle(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 180
18 | Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 116
17 | Illuminate\Pipeline\Pipeline->then(…) | ~/vendor/laravel/framework/src/Illuminate/Routing/Router.php | 726
16 | Illuminate\Routing\Router->runRouteWithinStack(…) | ~/vendor/laravel/framework/src/Illuminate/Routing/Router.php | 703
15 | Illuminate\Routing\Router->runRoute(…) | ~/vendor/laravel/framework/src/Illuminate/Routing/Router.php | 667
14 | Illuminate\Routing\Router->dispatchToRoute(…) | ~/vendor/winter/storm/src/Router/CoreRouter.php | 20
13 | Winter\Storm\Router\CoreRouter->dispatch(…) | ~/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php | 167
12 | Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 141
11 | Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(…) | ~/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php | 86
10 | Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(…) | ~/vendor/winter/storm/src/Foundation/Http/Middleware/CheckForMaintenanceMode.php | 25
9 | Winter\Storm\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 180
8 | Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(…) | ~/vendor/winter/storm/src/Foundation/Http/Middleware/CheckForTrustedProxies.php | 56
7 | Winter\Storm\Foundation\Http\Middleware\CheckForTrustedProxies->handle(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 180
6 | Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(…) | ~/vendor/winter/storm/src/Http/Middleware/TrustHosts.php | 46
5 | Winter\Storm\Http\Middleware\TrustHosts->handle(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 180
4 | Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(…) | ~/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php | 116
3 | Illuminate\Pipeline\Pipeline->then(…) | ~/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php | 142
2 | Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(…) | ~/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php | 111
1 | Illuminate\Foundation\Http\Kernel->handle(…) | ~/index.php | 43
bennothommo commented 2 years ago

Thanks @WebVPF.

That's strange though - there hasn't been any edits to the Twig functionality in a little while (bar a very minor change to a return type, which should have no effect on your code), so it's puzzling to me that it would only break today.

Could you try clearing the cache with php artisan cache:clear and see if that helps?

WebVPF commented 2 years ago

@bennothommo I already tried clearing the cache. This code worked for several years until this day. The website is down today after an update. Broke on those pages where there is this code. Winter CMS 1.2

WebVPF commented 2 years ago

@bennothommo Here is my component file {% component 'ShareButtons' %} https://github.com/WebVPF/wn-share-plugin/blob/master/components/buttons/default.htm

Does it have any error?

LukeTowers commented 2 years ago

@WebVPF what does your Component class file look like?

WebVPF commented 2 years ago

@LukeTowers there he is https://github.com/WebVPF/wn-share-plugin/blob/master/components/Buttons.php

WebVPF commented 2 years ago

@bennothommo, @LukeTowers This twig is causing the error

{{ ('webvpf.share::lang.buttons.' ~ btnId )|trans }}
bennothommo commented 2 years ago

@WebVPF so just to clarify, the issue is occurring when you include the component within another component? Or is it happening anywhere where you include the ShareButtons component?

bennothommo commented 2 years ago

I've just tested your plugin out on my current Winter 1.2 project, and it seems to be working fine for me. The only difference - as far as I can tell - is that I'm including the component within a layout.

WebVPF commented 2 years ago

@bennothommo When I include in another component

bennothommo commented 2 years ago

Ah right. I just tested that too and I didn't encounter the error.

Just to note, I don't believe we support components within components. While it may be possible to hack together, it's probably likely to break.

WebVPF commented 2 years ago

@bennothommo In Winter 1.2 the system markup filter |trans was removed? I don't see it in the documentation https://wintercms.com/docs/markup/filter-app

{{ "acme.demo::lang.string.example"|trans }}

It is he who is causing the error. I insert this filter into the layout and get an error. Just a filter without a plugin.

arvislacis commented 2 years ago

@WebVPF @bennothommo I think this is not related to Winter 1.2.0, I also got this error today when doing simple composer update on Winter 1.1.8 installation so my guess is that problem is one of the updated dependencies.

arvislacis commented 2 years ago

I am not 100% sure but this may be related to:

So the fix is already deployed and should be fine soon..., I will let you know later.

arvislacis commented 2 years ago

@bennothommo Ok, looks like problem still persists. Same happens when I tried to update (composer update) my Winter 1.1.8 dev installation.

LukeTowers commented 2 years ago

@arvislacis are you using the same components as @WebVPF?

arvislacis commented 2 years ago

@LukeTowers No, I am using mostly my own, custom components and some from Winter CMS plugins, not the ones @WebVPF mentioned.

LukeTowers commented 2 years ago

@arvislacis are you able to isolate the issue to a single specific component or does it affect every single component?

arvislacis commented 2 years ago

Something a bit weird is going on... because I did composer update once again today and it works fine now (so far no error in pages), without any changes in my code although yesterday I did some server management work and cache cleaning etc and reconfiguration, maybe cache was the issue...

@WebVPF could comment on this..., form my side everything looks fine now.

arvislacis commented 2 years ago

Here is additional update on this issue:

My coworker experienced same issue after setting up the same project I used with clean composer update.

After a little investigation we found out that |trans filter was also the main cause for this problem like @WebVPF initally mentioned. We didn't dig deeper into this but we fixed it by doing composer install on stable composer.lock file (from my previous working setup), and it worked fine.

I attached files of stable (working) and unstable (new but problematic) composer.lock files:

stable-composer.txt unstable-composer.txt

If anyone is facing the same problem then I probably would recommend to install previous composer.lock and then try to do composer update again.

LukeTowers commented 2 years ago

Differences between those files:

arvislacis commented 2 years ago

@LukeTowers My guess it's issue with doctrine/cache or twig/twig...

LukeTowers commented 2 years ago

Yeah, looking at https://github.com/twigphp/Twig/compare/v2.14.13...v2.15.1 it would look like those two issues that you linked were indeed the problem.

arvislacis commented 2 years ago

But it's interesting that sometimes also doing composer update on the stable .lock file doesn't causes problems anymore and everything works fine also with latest Twig etc. versions.

LukeTowers commented 2 years ago

@arvislacis could be there was a commit that was tagged as 1.15.1 and your first update got it (the "unstable" composer.lock) and then reverting your lock file to the old version told Composer to download it again.

Anyways, going to close this for now.

damsfx commented 2 years ago

Winter CMS Build

Latest : (dev-wip/1.2)

winter/storm                        dev-wip/1.2 91993e2
winter/wn-backend-module            dev-wip/1.2 0258f85
winter/wn-cms-module                dev-wip/1.2 a998ed7
winter/wn-system-module             dev-wip/1.2 c5295ca

PHP Version

8.0.20

Database engine

MariaDB : 10.3.10

Plugins installed

winter/wn-pages-plugin dev-wip/1.2 62c6003

Issue description

Unable to add a twig tag containing the | trans filter

Steps to replicate

composer create-project wintercms/winter winter12 "dev-wip/1.2 as 1.2"
php artisan winter:install
php artisan winter:env
php artisan winter:up
composer require winter/wn-pages-plugin
php artisan winter:up

=> Site working.

Add twig tag with trans filter in layout : {{ 'winter.pages::lang.plugin.description' | trans }} => Site down with exception:

Object of class Winter\Storm\Foundation\Application could not be converted to string
P:\_Sites\_Labs\winter12\vendor\twig\twig\src\Node\Expression\CallExpression.php line 304

The corresponding line is the same as the issue above : $callableName = $class.'::'.$r->name;

Do a dump of the line variables dd($class, $r->name);:

ReflectionClass {
  +name: "Illuminate\Support\Facades\Facade"
  modifiers: "abstract"
  implements: []
  constants: []
  properties: array:3 [▶]
  methods: array:19 [▶]
  extra: {▶}
}

"get"

Complete stack trace : system.log

Workaround

No response

mjauvin commented 2 years ago

@damsfx @LukeTowers @bennothommo

I was able to reproduce this using the above procedure.

Note: no need to install extra plugin. Just adding {{ 'anything' | trans }} triggers the bug.

mjauvin commented 2 years ago

Interesting: I switched this vhost to PHP 8.1, launched composer update and the problem disappears.

damsfx commented 2 years ago

@mjauvin @LukeTowers @bennothommo Reverting Twig to v3.3.10 (composer require twig/twig:3.3.10) solved my issue.
|trans filter works properly again

mjauvin commented 2 years ago

@damsfx what twig/twig version did you have before downgrading?

when on php 8.1, composer reports this:

composer show twig/twig
name     : twig/twig
descrip. : Twig, the flexible, fast, and secure template language for PHP
keywords : templating
versions : * v3.4.1

When using PHP 8.0 (after updating with composer), I have the exact same version for twig/twig ... so this is caused by a different dependency.

mjauvin commented 2 years ago

But you're right, downgrading to twig/twig:3.3.10 resolves the issue.

Why are you closing this @LukeTowers ?

arvislacis commented 2 years ago

@mjauvin Because initially it seemed like a random issue with specific Twig version but now it seems more global or/and problem still persists with newer Twig versions.

Read above Luke's comment: "@arvislacis could be there was a commit that was tagged as 1.15.1 and your first update got it (the "unstable" composer.lock) and then reverting your lock file to the old version told Composer to download it again.

Anyways, going to close this for now."

damsfx commented 2 years ago

@damsfx what twig/twig version did you have before downgrading?

From v3.4.1 : Downgrading twig/twig (v3.4.1 => v3.3.10)
I try with v3.4.0 with no success.

bennothommo commented 2 years ago

I wonder if this commit to Twig will fix the issue: https://github.com/twigphp/Twig/commit/55ce7242b1f0fcafdef0967d36e2a9d0ef1da6fe

It's pending for the next release.

mjauvin commented 2 years ago

@bennothommo I tried it and it results in the exact same error.

damsfx commented 2 years ago

Same error with last composer update in Winter 1.1.8:

winter/storm                     dev-develop 38db1fc
winter/wn-backend-module         dev-develop 80c7c02
winter/wn-cms-module             dev-develop 2258700
winter/wn-system-module          dev-develop f6cbb49

Need to downgrade twig/twig to 2.14.3 (v2.15.1 => v2.14.13) to get site working.

bennothommo commented 2 years ago

Hi all, I've done some investigation into this.

The issue actually happens with any filter that uses a facade - to test, I created another filter that uses the Cache facade and the same issue occurs.

I believe it's related to this change: https://github.com/twigphp/Twig/pull/3702

It appears that reflection is unable to find the correct method because it's hidden behind __callStatic magic from the facades. I haven't been able to work out where the Application class comes into play though.

We can fix this by either using a callback, or replacing - for example - ['Lang', 'get'] with [$this->app['translator'], 'get'] and use a call to a concrete class, and sidestep the facades.

bennothommo commented 2 years ago

Weirdly, I don't get this issue on my Mac - both on develop and 1.2 with the latest Twig versions (exactly the same version I tested yesterday on my Linux machine).

LukeTowers commented 2 years ago

@mjauvin if you revert only https://github.com/twigphp/Twig/pull/3702 locally, does that fix the error?

mjauvin commented 2 years ago

@LukeTowers @bennothommo I confirm reverting above PR does resolve the issue.

mjauvin commented 2 years ago

@arvislacis @damsfx can you confirm that with latest wip/1.2 & php8.0 the problem is gone?

Not sure what changed but it doesn't trigger anymore for me.

arvislacis commented 2 years ago

@mjauvin I had this issue on v1.1.8-dev when I commented so I can't confirm about wip/1.2... but today I run composer update on my local and test server project which actively uses |trans and I hadn't any problems. As far as I can see in https://github.com/twigphp/Twig then no new commits have been added in the last few days so I think that this issue is still a bit mysterious.

(Just a random side note: Sometimes I wonder that maybe it's somehow related to cache or class loading concurrency because as I commented on https://github.com/wintercms/winter/issues/557#issuecomment-1138608366 then sometimes doing rollback on older composer.lock file and then doing composer update also fixes issue for the newer Twig version.)

WebVPF commented 2 years ago

No. The problem hasn't gone away. Twig |trans, | transchoice and backend mail branding settings page. I asked those who use Oсtober 3, they say that it all works for them. But I myself do not use October, so I cannot confirm the veracity of their words.

LukeTowers commented 2 years ago

@WebVPF I could be wrong but I don't think October 3 uses Twig v3

WebVPF commented 2 years ago

@LukeTowers I recently asked in a telegram in the Ukrainian October group. Tried to check. I didn't tell them that we have problems in Winter. To my questions, people who are testing Oсtober 3 for themselves sent me screenshots:

photo_2022-06-22_16-06-16

Backend mail branding settings page:

photo_2022-06-22_15-55-03

LukeTowers commented 2 years ago

Hmm, odd. It does seem to be fairly environment dependent though, so far I haven't run into this on any of my sites.

WebVPF commented 2 years ago

I have Windows 10 (localhost).

bennothommo commented 2 years ago

The more I looked into it, I'd almost say that it's likely the fault exists within the Reflection library itself within PHP. It's the only way I can explain why the problem doesn't happen on some environments (like my Mac) but does on others, even with the same Twig version.