From 1637b90531e3797aa167e6380e6262fd9c1c9e1e Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> Date: Mon, 20 Sep 2021 11:40:00 -0400 Subject: [PATCH] Add determinsm to extension order resolution (#3076) By sorting alphabetically by extension ID before applying topological sort, we ensure that a given set of extensions will always be booted in the same order. This will make it easier to replicate issues caused by complex extension dependencies. --- src/Extension/ExtensionManager.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Extension/ExtensionManager.php b/src/Extension/ExtensionManager.php index c55a461be..d33e4808d 100644 --- a/src/Extension/ExtensionManager.php +++ b/src/Extension/ExtensionManager.php @@ -421,7 +421,7 @@ class ExtensionManager * Sort a list of extensions so that they are properly resolved in respect to order. * Effectively just topological sorting. * - * @param Extension[] $extensionList: an array of \Flarum\Extension\Extension objects + * @param Extension[] $extensionList * * @return array with 2 keys: 'valid' points to an ordered array of \Flarum\Extension\Extension * 'missingDependencies' points to an associative array of extensions that could not be resolved due @@ -443,6 +443,12 @@ class ExtensionManager $pendingQueue = []; $inDegreeCount = []; // How many extensions are dependent on a given extension? + // Sort alphabetically by ID. This guarantees that any set of extensions will always be sorted the same way. + // This makes boot order deterministic, and independent of enabled order. + $extensionList = Arr::sort($extensionList, function ($ext) { + return $ext->getId(); + }); + foreach ($extensionList as $extension) { $extensionIdMapping[$extension->getId()] = $extension; }