From 39bdf7ad06b3efa9faadc25a0c8b7a3e6091e5e6 Mon Sep 17 00:00:00 2001 From: Andrew Fullard <20341051+andrewfullard@users.noreply.github.com> Date: Sat, 17 Oct 2026 15:35:45 -0400 Subject: [PATCH] Load plugins and dependencies in declared order PluginLoader iterated plugin and dependency lists with pairs, which has no defined order. Use ipairs so plugins load in the order listed and each dependency fills the init argument matching its position. Co-Authored-By: Claude Opus 5.5 --- spec/std/pluginloader_spec.lua | 44 +++++++++++++++++++++++++++++++++- std/PluginLoader.lua | 6 +++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/spec/std/pluginloader_spec.lua b/spec/std/pluginloader_spec.lua index 21f42de..f3caef9 100644 --- a/spec/std/pluginloader_spec.lua +++ b/spec/std/pluginloader_spec.lua @@ -17,6 +17,7 @@ describe("PluginLoader", function() after_each(function() eaw_env.teardown_environment() require_utilities.reset_require() + package.loaded["spec/test-plugins/ordered-dependencies/init"] = nil end) local function assert_loaded_plugin_stub(plugin) @@ -48,6 +49,47 @@ describe("PluginLoader", function() end) end) + describe("When loading plugin arrays", function() + it("should load only array entries in order", function() + local loaded = {} + plugin_loader.load_plugin = function(self, plugin_name) + table.insert(loaded, plugin_name) + end + + plugin_loader:load { + "first", + "second", + metadata = "not-a-plugin" + } + + assert.are.same({"first", "second"}, loaded) + end) + + it("should pass only dependency array entries in order", function() + local first = {name = "first"} + local second = {name = "second"} + plugin_loader.loaded_plugins.first = first + plugin_loader.loaded_plugins.second = second + plugin_loader.loaded_plugins["not-a-dependency"] = {name = "ignored"} + + package.loaded["spec/test-plugins/ordered-dependencies/init"] = { + dependencies = { + "first", + "second", + metadata = "not-a-dependency" + }, + init = function(self, ctx, ...) + return {dependencies = arg} + end + } + + plugin_loader:load_plugin("ordered-dependencies", {}) + + assert.are.same({first, second, n = 2}, + plugin_loader.loaded_plugins["ordered-dependencies"].dependencies) + end) + end) + describe("When loading plugin with dependency", function() it("should pass dependency to dependent plugin", function() plugin_loader:load {"plugin-with-dependency"} @@ -81,4 +123,4 @@ describe("PluginLoader", function() assert.has_error(cyclic_load) end) end) -end) \ No newline at end of file +end) diff --git a/std/PluginLoader.lua b/std/PluginLoader.lua index 9d9b042..30eb569 100644 --- a/std/PluginLoader.lua +++ b/std/PluginLoader.lua @@ -36,7 +36,8 @@ function PluginLoader:load(plugin_list) end local unresolved_plugins = {} - for _, plugin_name in pairs(plugin_list) do + -- Plugin lists are ordered, so keep the order chosen by the author. + for _, plugin_name in ipairs(plugin_list) do self:load_plugin(plugin_name, unresolved_plugins) end @@ -67,7 +68,8 @@ function PluginLoader:load_plugin(plugin_name, unresolved_plugins) local loaded_dependencies = {} if plugin_def.dependencies and table.getn(plugin_def.dependencies) > 0 then - for _, dependency in pairs(plugin_def.dependencies) do + -- This order also decides which dependency fills each argument. + for _, dependency in ipairs(plugin_def.dependencies) do if not self.loaded_plugins[dependency] then self:load_plugin(dependency, unresolved_plugins) end