From 6663dcbfb307026f093b7dd9a37623c9029e5f0e Mon Sep 17 00:00:00 2001 From: SoftFever Date: Sat, 7 Nov 2026 12:42:07 +0800 Subject: [PATCH] Show a reloaded local plugin's new version in the Plugins dialog --- src/slic3r/plugin/PluginManager.cpp | 34 ++++++++++++++++++- .../test_plugin_cloud_metadata.cpp | 3 ++ tests/slic3rutils/test_plugin_lifecycle.cpp | 34 +++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/slic3r/plugin/PluginManager.cpp b/src/slic3r/plugin/PluginManager.cpp index f5954eef5c..b5bd92e8b5 100644 --- a/src/slic3r/plugin/PluginManager.cpp +++ b/src/slic3r/plugin/PluginManager.cpp @@ -73,6 +73,20 @@ std::vector* permission_bucket(PluginPermissions& permissions, Plug return nullptr; } +// Puts what a scan reads from a local plugin's entry file onto `target`. A local plugin's entry file is +// the only record of its version, so the installed version follows it. +void apply_entry_metadata(PluginDescriptor& target, const PluginDescriptor& fresh) +{ + target.name = fresh.name; + target.description = fresh.description; + target.author = fresh.author; + target.version = fresh.version; + target.entry_package = fresh.entry_package; + target.dependencies = fresh.dependencies; + if (!fresh.version.empty()) + target.installed_version = fresh.version; +} + } // namespace void Plugin::release_module() @@ -995,6 +1009,24 @@ void PluginManager::load_plugin_impl(const std::string& plugin_key, bool skip_de fail(GUI::format(_u8L("The plugin name %1% is reserved; rename the plugin file"), plugin_key)); return; } + // A local .py plugin's entry file may have been edited since the last scan: read it again, so the load + // installs the dependencies it declares now and the Plugins dialog shows the version that loads. A wheel + // is left as scanned: a load imports the copy extracted when it first loaded, so new metadata read from + // the wheel could describe code that does not run; a reinstall replaces both. A cloud plugin's version + // comes from the cloud and its install state, and its files change only through a cloud install, which + // rescans. + if (!descriptor.is_cloud_plugin() && boost::filesystem::path(descriptor.entry_path).extension() == ".py") { + PluginDescriptor fresh; + std::string meta_error; + if (!read_python_plugin_metadata(descriptor.entry_path, fresh, meta_error)) { + fail(std::move(meta_error)); + return; + } + apply_entry_metadata(descriptor, fresh); + std::lock_guard lock(m_mutex); + if (Plugin* entry = find_plugin_locked(plugin_key)) + apply_entry_metadata(entry->descriptor, fresh); + } // Reject a duplicate package or a capability-name collision BEFORE on_load() runs, so a // rejected load costs no on_load/on_unload cycle. Also the point at which a cancellation that @@ -1559,7 +1591,7 @@ bool PluginManager::install_plugin(const boost::filesystem::path& filepath, Plug std::string wait_error; if (!wait_for_discovery(std::chrono::milliseconds::max(), wait_error)) { - error = "Plugin discovery is still running"; + error = GUI::format(_u8L("Plugin installation failed: %1%"), wait_error.empty() ? _u8L("Plugin discovery is still running") : wait_error); if (!plugin_descriptor.plugin_key.empty()) set_plugin_error(plugin_descriptor.plugin_key, error); BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": Plugin installation failed while waiting for discovery: " << error; diff --git a/tests/slic3rutils/test_plugin_cloud_metadata.cpp b/tests/slic3rutils/test_plugin_cloud_metadata.cpp index 3e3735a6ca..66e129af17 100644 --- a/tests/slic3rutils/test_plugin_cloud_metadata.cpp +++ b/tests/slic3rutils/test_plugin_cloud_metadata.cpp @@ -189,6 +189,9 @@ TEST_CASE("cloud metadata refresh preserves a plugin's stored config", "[PluginC INFO("load error: " << load_error); CHECK(load_error.empty()); CHECK(manager.is_plugin_loaded(uuid)); + // The load keeps the versions the cloud and the install state give, not the 1.0 its entry file declares. + CHECK(find_by_uuid().version == "1.1"); + CHECK(find_by_uuid().installed_version == "1.0"); CHECK(manager.unload_plugin(uuid)); // Seeing the plugin in a subsequent cloud response clears the orphaned marker. diff --git a/tests/slic3rutils/test_plugin_lifecycle.cpp b/tests/slic3rutils/test_plugin_lifecycle.cpp index 1a2cc7fb9f..3ce5a499df 100644 --- a/tests/slic3rutils/test_plugin_lifecycle.cpp +++ b/tests/slic3rutils/test_plugin_lifecycle.cpp @@ -583,6 +583,40 @@ TEST_CASE("Duplicate discovered plugin keys are reported", "[PluginLifecycle][Py CHECK(descriptor.normalized_error().find("Duplicate plugin key") != std::string::npos); } +TEST_CASE("A script plugin loads with the version and dependencies its entry file declares", "[PluginLifecycle][Python]") +{ + ScopedPluginManager plugin_system; + if (!plugin_system.initialized) + SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error()); + + ScopedDataDir data_dir_guard("lifecycle-edited"); + write_plugin(data_dir_guard, "Echo_Plugin", ECHO_PLUGIN_SOURCE); + + PluginManager& manager = PluginManager::instance(); + manager.discover_plugins(/*async=*/false, /*clear=*/true); + REQUIRE(descriptor_of(manager, "Echo_Plugin").version == "1.0"); + + // The entry file is edited after the scan, to a new version and a new dependency. + std::string edited = ECHO_PLUGIN_SOURCE; + const std::string version = "# version = \"1.0\""; + edited.replace(edited.find(version), version.size(), "# version = \"2.0\""); + edited.insert(edited.find("# [tool.orcaslicer.plugin]"), "# dependencies = [\"echo-dependency\"]\n"); + write_plugin(data_dir_guard, "Echo_Plugin", edited); + + // skip_deps: the dependency is read from the file, not installed. + std::string error; + REQUIRE(load_and_wait(manager, "Echo_Plugin", error)); + INFO("load error: " << error); + CHECK(error.empty()); + + const PluginDescriptor loaded = descriptor_of(manager, "Echo_Plugin"); + CHECK(loaded.version == "2.0"); + CHECK(loaded.installed_version == "2.0"); + CHECK(loaded.dependencies == std::vector{"echo-dependency"}); + + manager.unload_plugin("Echo_Plugin"); +} + TEST_CASE("Unloading a plugin drops the package and its capabilities", "[PluginLifecycle][Python]") { ScopedPluginManager plugin_system;