diff --git a/src/slic3r/plugin/PluginAuditManager.cpp b/src/slic3r/plugin/PluginAuditManager.cpp index f34d9806f6..ddc1dc38e7 100644 --- a/src/slic3r/plugin/PluginAuditManager.cpp +++ b/src/slic3r/plugin/PluginAuditManager.cpp @@ -2855,8 +2855,12 @@ bool audit_state_belongs_to_loaded_identity(const std::string& plugin_key, const const std::string& installation_revision) { const PluginBrowserProfileIdentityRef identity = current_plugin_browser_profile_identity(plugin_key); - return identity && !identity->installation_revision.empty() && identity->plugin_root == plugin_root && - identity->installation_revision == installation_revision; + if (!identity || identity->plugin_root != plugin_root) + return false; + // A sidecar with no revision is a legacy install, or one that was lost and is being seeded + // again; it has no installation identity to compare against. A load writes the revision before + // plugin code runs, so a real replacement always carries one and still mismatches here. + return installation_revision.empty() || identity->installation_revision == installation_revision; } // Records a grant in the plugin's sidecar so it is not asked again. Each persist helper re-reads @@ -3529,6 +3533,10 @@ int audit_plugin_event(PluginAuditManager& mgr, const std::string& event_name, A if (!have_install_state) { BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << " Refusing install state for a missing or replaced installation: " << mgr.current_plugin(); + // Never decide with a sidecar that does not belong to this load instance: it may describe + // a replacement installed by another process. With no usable state no saved grant applies, + // so the operation is prompted for and persist_grant refuses to record it. + state = PluginInstallState{}; } const std::string plugin_name = state.plugin_name.empty() ? mgr.current_plugin() : state.plugin_name; diff --git a/src/slic3r/plugin/PluginFsUtils.cpp b/src/slic3r/plugin/PluginFsUtils.cpp index 9345e2464d..13df034794 100644 --- a/src/slic3r/plugin/PluginFsUtils.cpp +++ b/src/slic3r/plugin/PluginFsUtils.cpp @@ -592,7 +592,11 @@ void assign_discovered_plugin_key(PluginDescriptor& descriptor, const boost::fil descriptor.plugin_key = make_local_plugin_key(name_source.stem().string()); } -void scan_plugin_directory(const std::string& dir_path, std::vector& out) +// Fills `out` with the packages found directly under `dir_path`. A directory that cannot be +// enumerated or whose packages cannot be inspected (for example the install-state lock is held by +// another instance) sets `error` and leaves `out` partial: the caller must not merge such a list, +// because the packages it is missing are present but unread. +void scan_plugin_directory(const std::string& dir_path, std::vector& out, std::string& error) { namespace fs = boost::filesystem; @@ -663,7 +667,11 @@ void scan_plugin_directory(const std::string& dir_path, std::vector discover_plugin_packages(const std::vector(std::chrono::steady_clock::now() - start_time); - BOOST_LOG_TRIVIAL(info) << "Plugin discovery completed in " << duration.count() << "ms. Found " << discovered.size() - << " plugin manifests"; + if (error.empty()) + BOOST_LOG_TRIVIAL(info) << "Plugin discovery completed in " << duration.count() << "ms. Found " << discovered.size() + << " plugin manifests"; + else + BOOST_LOG_TRIVIAL(warning) << "Plugin discovery stopped after " << duration.count() << "ms with " << discovered.size() + << " plugin manifests; the result is partial: " << error; } catch (const std::exception& ex) { - error = std::string("Plugin discovery failed: ") + ex.what(); + // A scan error already describes the first failure; keep it. + if (error.empty()) + error = std::string("Plugin discovery failed: ") + ex.what(); BOOST_LOG_TRIVIAL(error) << error; } catch (...) { - error = "Plugin discovery failed: unknown error"; + if (error.empty()) + error = "Plugin discovery failed: unknown error"; BOOST_LOG_TRIVIAL(error) << error; } diff --git a/src/slic3r/plugin/PluginFsUtils.hpp b/src/slic3r/plugin/PluginFsUtils.hpp index 396c4c431d..b0406cf8b4 100644 --- a/src/slic3r/plugin/PluginFsUtils.hpp +++ b/src/slic3r/plugin/PluginFsUtils.hpp @@ -116,6 +116,11 @@ std::vector get_plugin_directories(const std::string& cloud_user_id // parsed comes back with metadata_valid == false and its error set (descriptor.is_invalid_package()). // The package-level auto-load flag is read from each package's .install_state.json into // descriptor.enabled. Capabilities are NOT discovered here — a package has none until it is loaded. +// +// `error` is non-empty when a directory could not be scanned (for example its packages could not be +// inspected because the install-state lock is held by another instance); scanning then stops. The +// returned list is partial in that case and must not be merged into an existing catalog, because the +// packages it is missing are installed and still on disk. std::vector discover_plugin_packages(const std::vector& dirs, std::string& error); bool is_ignored_plugin_directory(const boost::filesystem::path& path); diff --git a/tests/slic3rutils/test_plugin_audit.cpp b/tests/slic3rutils/test_plugin_audit.cpp index 6b58939e01..72ecb84f28 100644 --- a/tests/slic3rutils/test_plugin_audit.cpp +++ b/tests/slic3rutils/test_plugin_audit.cpp @@ -8,6 +8,8 @@ #include #include // GCODEVIEWER_APP_KEY, SLIC3R_APP_KEY (via libslic3r_version.h) #include +#include +#include #include #include #include @@ -1602,12 +1604,30 @@ struct AuditHarness std::ofstream((root / (key + ".py")).string(), std::ios::binary) << source; save_permissions(key, {}); PluginManager::instance().discover_plugins(/*async=*/false, /*clear=*/true); + bind_loaded_identity(key); + } + + // A real load binds the plugin key to its package root and sidecar revision before plugin code + // runs; the audit gate trusts saved grants only for that installation. + void bind_loaded_identity(const std::string& key) + { + PluginDescriptor descriptor; + if (!PluginManager::instance().try_get_plugin_descriptor(key, descriptor) || descriptor.plugin_root.empty()) + return; + PluginInstallState state; + read_install_state(boost::filesystem::path(descriptor.plugin_root), state); + begin_plugin_browser_profile(key, descriptor.plugin_root, state.installation_revision); } // Grants as if saved in an earlier session. void save_permissions(const std::string& key, const PluginPermissions& permissions) { PluginInstallState state; + PluginInstallState existing; + if (read_install_state(data.plugins_dir() / key, existing)) { + state.browser_profile_id = existing.browser_profile_id; + state.installation_revision = existing.installation_revision; + } state.installed_from = "local"; state.installed_version = "1.0"; state.plugin_name = key; @@ -3231,6 +3251,10 @@ TEST_CASE("A permission mutation changes only the named entries and preserves th initial.permissions.fs_create = {"/kept/create", "/removed/create"}; initial.permissions.fs_delete = {"/kept/delete", "/removed/delete"}; initial.permissions.network_http = {"https://kept.example"}; + PluginInstallState existing; + REQUIRE(read_install_state(plugin_dir, existing)); + initial.browser_profile_id = existing.browser_profile_id; + initial.installation_revision = existing.installation_revision; REQUIRE(write_install_state(plugin_dir, initial)); PluginPermissionMutation mutation;