From b3e52f99e643c9acacc12cf767adfc1f14ab0c6d Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 11 Jan 2022 12:30:22 +0000 Subject: [PATCH] FIX: LOAD_PLUGINS=0 in dev/prod, warn in plugin:pull_compatible_all (#15537) The `plugin:pull_compatible_all` task is intended to take incompatible plugins and downgrade them to an earlier version. Problem is, when running the rake task in development/production environments, the plugins have already been activated. If an incompatible plugin raises an error in `plugin.rb` then the rake task will be unable to start. This commit centralises our LOAD_PLUGINS detection, adds support for LOAD_PLUGINS=0 in dev/prod, and adds a warning to `plugin:pull_compatible_all` if it's run with plugins enabled. --- app/models/global_setting.rb | 11 +++++++++++ app/models/site_setting.rb | 2 +- config/application.rb | 10 ++++------ lib/tasks/plugin.rake | 7 +++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb index c39ceaefd92..ff1ac7a5066 100644 --- a/app/models/global_setting.rb +++ b/app/models/global_setting.rb @@ -342,4 +342,15 @@ class GlobalSetting end end + def self.load_plugins? + if ENV["LOAD_PLUGINS"] == "1" + true + elsif ENV["LOAD_PLUGINS"] == "0" + false + elsif Rails.env.test? + false + else + true + end + end end diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index 0bbaf508606..5d496245318 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -15,7 +15,7 @@ class SiteSetting < ActiveRecord::Base load_settings(File.join(Rails.root, 'config', 'site_settings.yml')) - unless Rails.env.test? && ENV['LOAD_PLUGINS'] != "1" + if GlobalSetting.load_plugins? Dir[File.join(Rails.root, "plugins", "*", "config", "settings.yml")].each do |file| load_settings(file, plugin: file.split("/")[-3]) end diff --git a/config/application.rb b/config/application.rb index a6ecb270d0c..59394707820 100644 --- a/config/application.rb +++ b/config/application.rb @@ -30,7 +30,7 @@ require_relative '../lib/plugin_gem' # Global config require_relative '../app/models/global_setting' GlobalSetting.configure! -unless Rails.env.test? && ENV['LOAD_PLUGINS'] != "1" +if GlobalSetting.load_plugins? require_relative '../lib/custom_setting_providers' end GlobalSetting.load_defaults @@ -312,11 +312,9 @@ module Discourse config.relative_url_root = GlobalSetting.relative_url_root end - if Rails.env == "test" - if ENV['LOAD_PLUGINS'] == "1" - Discourse.activate_plugins! - end - else + if Rails.env.test? && GlobalSetting.load_plugins? + Discourse.activate_plugins! + elsif GlobalSetting.load_plugins? plugin_initialization_guard do Discourse.activate_plugins! end diff --git a/lib/tasks/plugin.rake b/lib/tasks/plugin.rake index be293fd1833..5526b5facb8 100644 --- a/lib/tasks/plugin.rake +++ b/lib/tasks/plugin.rake @@ -109,6 +109,13 @@ end desc 'pull compatible plugin versions for all plugins' task 'plugin:pull_compatible_all' do |t| + if GlobalSetting.load_plugins? + STDERR.puts <<~TEXT + WARNING: Plugins were activated before running `rake plugin:pull_compatible_all` + You should prefix this command with LOAD_PLUGINS=0 + TEXT + end + # Loop through each directory plugins = Dir.glob(File.expand_path('plugins/*')).select { |f| File.directory? f } # run plugin:pull_compatible