putyourlightson / craft-blitz

Intelligent static page caching for creating lightning-fast sites with Craft CMS.
https://putyourlightson.com/plugins/blitz
Other
149 stars 36 forks source link

Undefined constant CRAFT_ENVIRONMENT and code 0 #476

Closed bmilagre closed 1 year ago

bmilagre commented 1 year ago

Description After installing Blitz Cache I wanted to generate the cache via Command. However, when executing the command it aborts already at the beginning with the error message "Undefined constant "CRAFT_ENVIRONMENT"" and code "0".

The named variable exists and is callable within Craft. So its probably something wrong set up.

Generating Blitz cache...
[>                                                                                                                       ] 0% (0/141) 
ETA: Error: Uncaught Error in child process or thread with message "Undefined constant "CRAFT_ENVIRONMENT"" and code "0"; useAmp\Parallel\Sync\ContextPanicError::getOriginalTrace() for the stack trace in the child process or thread

console.log

2023-02-23 13:58:18 [console.ERROR] [Amp\Parallel\Sync\ContextPanicError] Uncaught Error in child process or thread with message "Undefined constant "CRAFT_ENVIRONMENT"" and code "0"; use Amp\Parallel\Sync\ContextPanicError::getOriginalTrace() for the stack trace in the child process or thread {"trace":["#0 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/parallel/lib/Sync/ExitFailure.php(39): Amp\\Parallel\\Sync\\ExitFailure->createException()","#1 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/parallel/lib/Context/Process.php(232): Amp\\Parallel\\Sync\\ExitFailure->getResult()","#2 [internal function]: Amp\\Parallel\\Context\\Process->Amp\\Parallel\\Context\\{closure}()","#3 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/amp/lib/Coroutine.php(118): Generator->send(Object(Amp\\Parallel\\Sync\\ExitFailure))","#4 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/amp/lib/Internal/Placeholder.php(149): Amp\\Coroutine->Amp\\{closure}(NULL, Object(Amp\\Parallel\\Sync\\ExitFailure))","#5 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/amp/lib/Coroutine.php(123): Amp\\Coroutine->resolve(Object(Amp\\Parallel\\Sync\\ExitFailure))","#6 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/amp/lib/Internal/Placeholder.php(149): Amp\\Coroutine->Amp\\{closure}(NULL, '-generator-scri...')","#7 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/amp/lib/Deferred.php(53): Amp\\Promise@anonymous->resolve('-generator-scri...')","#8 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/byte-stream/lib/ResourceInputStream.php(109): Amp\\Deferred->resolve('-generator-scri...')","#9 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/amp/lib/Loop/Driver.php(119): Amp\\ByteStream\\ResourceInputStream::Amp\\ByteStream\\{closure}('ag', '-generator-scri...')","#10 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/amp/lib/Loop/Driver.php(72): Amp\\Loop\\Driver->tick()","#11 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/amp/lib/Loop.php(95): Amp\\Loop\\Driver->run()","#12 /home/wktechni/public_html/wktechnik/beta/vendor/amphp/amp/lib/functions.php(229): Amp\\Loop::run(Object(Closure))","#13 /home/wktechni/public_html/wktechnik/beta/vendor/putyourlightson/craft-blitz/src/drivers/generators/LocalGenerator.php(101): Amp\\Promise\\wait(Object(Amp\\Coroutine))","#14 /home/wktechni/public_html/wktechnik/beta/vendor/putyourlightson/craft-blitz/src/drivers/generators/BaseCacheGenerator.php(103): putyourlightson\\blitz\\drivers\\generators\\LocalGenerator->generateUrisWithProgress(Array, Array)","#15 /home/wktechni/public_html/wktechnik/beta/vendor/putyourlightson/craft-blitz/src/console/controllers/CacheController.php(409): putyourlightson\\blitz\\drivers\\generators\\BaseCacheGenerator->generateUris(Array, Array, false)","#16 /home/wktechni/public_html/wktechnik/beta/vendor/putyourlightson/craft-blitz/src/console/controllers/CacheController.php(156): putyourlightson\\blitz\\console\\controllers\\CacheController->_generateCache(Array)","#17 [internal function]: putyourlightson\\blitz\\console\\controllers\\CacheController->actionGenerate()","#18 /home/wktechni/public_html/wktechnik/beta/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)","#19 /home/wktechni/public_html/wktechnik/beta/vendor/yiisoft/yii2/base/Controller.php(178): yii\\base\\InlineAction->runWithParams(Array)","#20 /home/wktechni/public_html/wktechnik/beta/vendor/yiisoft/yii2/console/Controller.php(180): yii\\base\\Controller->runAction('generate', Array)","#21 /home/wktechni/public_html/wktechnik/beta/vendor/yiisoft/yii2/base/Module.php(552): yii\\console\\Controller->runAction('generate', Array)","#22 /home/wktechni/public_html/wktechnik/beta/vendor/yiisoft/yii2/console/Application.php(180): yii\\base\\Module->runAction('blitz/cache/gen...', Array)","#23 /home/wktechni/public_html/wktechnik/beta/vendor/craftcms/cms/src/console/Application.php(90): yii\\console\\Application->runAction('blitz/cache/gen...', Array)","#24 /home/wktechni/public_html/wktechnik/beta/vendor/yiisoft/yii2/console/Application.php(147): craft\\console\\Application->runAction('blitz/cache/gen...', Array)","#25 /home/wktechni/public_html/wktechnik/beta/vendor/craftcms/cms/src/console/Application.php(121): yii\\console\\Application->handleRequest(Object(craft\\console\\Request))","#26 /home/wktechni/public_html/wktechnik/beta/vendor/yiisoft/yii2/base/Application.php(384): craft\\console\\Application->handleRequest(Object(craft\\console\\Request))","#27 /home/wktechni/public_html/wktechnik/beta/craft(23): yii\\base\\Application->run()","#28 {main}"],"memory":32536536,"exception":"[object] (Amp\\Parallel\\Sync\\ContextPanicError(code: 0): Uncaught Error in child process or thread with message \"Undefined constant \"CRAFT_ENVIRONMENT\"\" and code \"0\"; use Amp\\Parallel\\Sync\\ContextPanicError::getOriginalTrace() for the stack trace in the child process or thread at /home/wktechni/public_html/wktechnik/beta/vendor/amphp/parallel/lib/Sync/ExitFailure.php:46)"} 

Blitz version 4.3.3

Craft CMS version 4.3.9

PHP version 8.0.26

bencroker commented 1 year ago

Have you defined the environment in a CRAFT_ENVIRONMENT environment variable as per the .env example?

# The environment Craft is currently running in (dev, staging, production, etc.)
CRAFT_ENVIRONMENT=dev

https://github.com/craftcms/craft/blob/97f9cfed7301381873046ca3a0b7919bce7ad75e/.env.example.dev#L8

bmilagre commented 1 year ago

Yes its defined and accessible inside craft.

bencroker commented 1 year ago

Can you please try catching the context panic errors as per https://github.com/putyourlightson/craft-blitz/commit/49624dd1a43a8be197a30441df441e2664b1be22, to see if we can get any more helpful info?

bmilagre commented 1 year ago

Our environment is not set with the same name as described in the doc, but is later correctly defined as CRAFT_ENVIRONMENT.

.env

# The environment Craft is currently running in (dev, staging, production, etc.)
ENVIRONMENT=production

web/index.php

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');

I dumped it before the try & catch and it seems to exist.

bmilagre@wk-web:/var/www/html$ ./craft blitz/cache/generate
Generating Blitz cache...
[>                                                                                                                                                                             ] 0% (0/141) ETA: n/a
 CRAFT_ENVIRONMENT: production

Errors from the context panic

[2023-02-27 11:23:49] Uncaught Error in child process or thread with message "Undefined constant "CRAFT_ENVIRONMENT"" and code "0"; use Amp\Parallel\Sync\ContextPanicError::getOriginalTrace() for the stack trace in the child process or thread
[2023-02-27 11:23:49] #0 /var/www/html/vendor/amphp/parallel/lib/Sync/ExitFailure.php(39): Amp\Parallel\Sync\ExitFailure->createException()
[2023-02-27 11:23:49] #0 /var/www/html/vendor/craftcms/cms/src/services/Config.php(289): include()
[2023-02-27 11:23:49] Blitz cache generation complete.
bencroker commented 1 year ago

Console requests don't go through web/index.php and it looks like you may be running an old version of index.php: https://github.com/craftcms/craft/blob/main/web/index.php

The environment is now set as part of the bootstrapping process: https://github.com/craftcms/cms/blob/aad45b7d4c7005a0ac4809f32e135c6eadec7062/bootstrap/bootstrap.php#L92-L93

I suggest you update to use CRAFT_ENVIRONMENT, which should also resolve this issue.

bmilagre commented 1 year ago

It looked like the upgrade to v4 was not done cleanly by us. I copied the following files based on the new version in my project:

bootstrap.php craft web/index.php

What solved the problem for me though was upgrading phpdotenv from 3.4 to 5.4

Thanks for your support!

bencroker commented 1 year ago

Ah yes, that catches lots of people out, documented here for reference: https://craftcms.com/docs/4.x/upgrade.html#entry-script

bencroker commented 1 year ago

The catching of context panic errors has been released in version 4.4.0.