Skip to content
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

Check if Craft is installed before generating autocomplete classes #14

Merged
merged 1 commit into from
Sep 23, 2024

Conversation

white-lukas
Copy link
Contributor

Description

When installing a new Craft setup an exception is getting thrown when trying to query the global sets

craft\errors\SiteNotFoundException: No primary site exists in /var/www/html/vendor/craftcms/cms/src/services/Sites.php:526
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/services/Sites.php(473): craft\services\Sites->getPrimarySite()
#1 /var/www/html/vendor/craftcms/cms/src/services/Globals.php(127): craft\services\Sites->getCurrentSite()
#2 /var/www/html/vendor/craftcms/cms/src/web/twig/GlobalsExtension.php(28): craft\services\Globals->getAllSets()
#3 /var/www/html/vendor/nystudio107/craft-autocomplete/src/generators/AutocompleteTwigExtensionGenerator.php(166): craft\web\twig\GlobalsExtension->getGlobals()
#4 /var/www/html/vendor/nystudio107/craft-autocomplete/src/generators/AutocompleteTwigExtensionGenerator.php(112): nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator::globalVariables()
#5 /var/www/html/vendor/nystudio107/craft-autocomplete/src/generators/AutocompleteTwigExtensionGenerator.php(54): nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator::generateInternal()
#6 /var/www/html/vendor/nystudio107/craft-autocomplete/src/Autocomplete.php(146): nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator::generate()
#7 [internal function]: nystudio107\autocomplete\Autocomplete->generateAutocompleteClasses(Object(yii\base\Event))
#8 /var/www/html/vendor/yiisoft/yii2/base/Event.php(312): call_user_func(Array, Object(yii\base\Event))
#9 /var/www/html/vendor/yiisoft/yii2/base/Component.php(650): yii\base\Event::trigger('craft\\web\\Appli...', 'init', Object(yii\base\Event))
#10 /var/www/html/vendor/craftcms/cms/src/base/ApplicationTrait.php(546): yii\base\Component->trigger('init', NULL)
#11 /var/www/html/vendor/craftcms/cms/src/base/ApplicationTrait.php(1637): craft\web\Application->trigger('init')
#12 /var/www/html/vendor/craftcms/cms/src/web/Application.php(110): craft\web\Application->_postInit()
#13 /var/www/html/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init()
#14 /var/www/html/vendor/yiisoft/yii2/base/Application.php(204): yii\base\BaseObject->__construct(Array)
#15 [internal function]: yii\base\Application->__construct(Array)
#16 /var/www/html/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs(Array)
#17 /var/www/html/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build('craft\\web\\Appli...', Array, Array)
#18 /var/www/html/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('craft\\web\\Appli...', Array, Array)
#19 /var/www/html/vendor/craftcms/cms/src/Craft.php(70): yii\BaseYii::createObject(Array, Array)
#20 /var/www/html/vendor/craftcms/cms/bootstrap/bootstrap.php(312): Craft::createObject(Array)
#21 /var/www/html/vendor/craftcms/cms/bootstrap/web.php(40): require('/var/www/html/v...')
#22 /var/www/html/web/index.php(11): require('/var/www/html/v...')
#23 {main}

foreach ($autocompleteGenerators as $generatorClass) {
/* @var Generator $generatorClass */
$generatorClass::generate();
if (Craft::$app->getIsInstalled()) {
Copy link
Collaborator

@bencroker bencroker Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like at this point, no primary site exists. Does this necessarily mean that the plugin Craft is not installed, or couldn’t the error also appear in an installed setup with no primary sites?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the thing is, Craft Autocomplete is a self-bootstrapping module, so as long as it's required, it's installed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I meant Craft (not a plugin). What I’m getting at is, wouldn’t it be more correct to check whether a primary site exists rather than whether Craft is installed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the installation process create the default site? Although I guess you're right, someone could potentially delete the one and only site somehow.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna merge it anyway, because I think this is probably a good safety check to have in regardless. If we need to do more for further edge cases down the road, we can.

@khalwat
Copy link
Contributor

khalwat commented Sep 23, 2024

So my only question here would be: when was Craft::$app->getIsInstalled() implemented? Because Craft Autocomplete supports:

    "craftcms/cms": "^3.0.0 || ^4.0.0 || ^5.0.0"

@khalwat
Copy link
Contributor

khalwat commented Sep 23, 2024

Alrighty I looked back, and it appears that :: getIsInstalled() was part of the 3.0 release so we're good to go.

@khalwat khalwat merged commit af3aa89 into nystudio107:develop Sep 23, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants