Skip to content

A platform for community discussion. Free, open, simple.

DEV: Resolve plugin Gemfiles with bundler (#44891)

Gerhard Schlagerffe94e14449e
diff --git a/.gitignore b/.gitignore
index 57f02d9971d..4c460cd91b4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -86,6 +86,7 @@
/spec/fixtures/plugins/my_plugin/auto_generated
/vendor/bundle
+/vendor/plugin-gems
/vendor/runtime_node_modules
/vendor/data/GeoLite2-City.mmdb
/vendor/data/GeoLite2-ASN.mmdb
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 624cec271c9..b111feb9821 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -382,29 +382,35 @@ module Discourse
def self.activate_plugins!
@plugins = []
@plugins_by_name = {}
- Plugin::Instance
- .find_all("#{Rails.root.join("plugins")}")
- .each do |p|
- v = p.metadata.required_version || Discourse::VERSION::STRING
- if Discourse.has_needed_version?(Discourse::VERSION::STRING, v)
- p.activate!
- @plugins << p
- @plugins_by_name[p.name] = p
-
- # The plugin directory name and metadata name should match, but that
- # is not always the case
- dir_name = p.path.split("/")[-2]
- if p.name != dir_name
- STDERR.puts "Plugin name is '#{p.name}', but plugin directory is named '#{dir_name}'"
- # Plugins are looked up by directory name in SiteSettingExtension
- # because SiteSetting.load_settings uses directory name as plugin
- # name. We alias the two names just to make sure the look up works
- @plugins_by_name[dir_name] = p
- end
- else
+ plugins =
+ Plugin::Instance
+ .find_all("#{Rails.root.join("plugins")}")
+ .select do |p|
+ v = p.metadata.required_version || Discourse::VERSION::STRING
+ next true if Discourse.has_needed_version?(Discourse::VERSION::STRING, v)
+
STDERR.puts "Could not activate #{p.metadata.name}, discourse does not meet required version (#{v})"
+ false
end
+
+ PluginGem.activate_gemfiles(plugins)
+
+ plugins.each do |p|
+ p.activate!
+ @plugins << p
+ @plugins_by_name[p.name] = p
+
+ # The plugin directory name and metadata name should match, but that
+ # is not always the case
+ dir_name = p.path.split("/")[-2]
+ if p.name != dir_name
+ STDERR.puts "Plugin name is '#{p.name}', but plugin directory is named '#{dir_name}'"
+ # Plugins are looked up by directory name in SiteSettingExtension
+ # because SiteSetting.load_settings uses directory name as plugin
+ # name. We alias the two names just to make sure the look up works
+ @plugins_by_name[dir_name] = p
end
+ end
DiscourseEvent.trigger(:after_plugin_activation)
end
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index 73f55e07993..6a15c435bce 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -987,6 +987,10 @@ class Plugin::Instance
#
# This is a very rough initial implementation
def gem(name, version, opts = {})
+ if metadata.gemfile
+ raise "Plugin #{directory_name} declares its gems in a Gemfile, `gem` can't be mixed with that"
+ end
+
PluginGem.load(path, name, version, opts)
end
diff --git a/lib/plugin/metadata.rb b/lib/plugin/metadata.rb
index 6381c88b311..6d1469cc1df 100644
--- a/lib/plugin/metadata.rb
+++ b/lib/plugin/metadata.rb
@@ -8,7 +8,18 @@ class Plugin::Metadata
# from config/official_plugins.json
OFFICIAL_PLUGINS = Set.new(JSON.load_file(Rails.root.join("config/official_plugins.json").to_s))
- FIELDS = %i[name about version authors contact_emails url required_version meta_topic_id label]
+ FIELDS = %i[
+ name
+ about
+ version
+ authors
+ contact_emails
+ url
+ required_version
+ meta_topic_id
+ label
+ gemfile
+ ]
attr_accessor(*FIELDS)
MAX_FIELD_LENGTHS = {
@@ -29,6 +40,28 @@ class Plugin::Metadata
end
end
+ # `# gemfile: true` in plugin.rb makes the plugin declare its gems in a
+ # Gemfile next to plugin.rb instead of calling `gem` in plugin.rb. See
+ # PluginGem.activate_gemfiles for the implementation. The rules:
+ # - The Gemfiles of all opted-in plugins are resolved together with the
+ # application's bundle, before any plugin.rb runs.
+ # - The gems are activated, not required. `require:` options have no
+ # effect; the plugin requires its gems itself.
+ # - A Gemfile.lock with a CHECKSUMS section must be committed next to the
+ # Gemfile (`bundle lock --add-checksums`).
+ # - A gem the application has keeps the application's version. For other
+ # gems the highest version locked by any plugin is used.
+ # - Every resolved gem must match a committed lock in version, platform
+ # and source, and a downloaded gem must match the locked SHA256
+ # checksum. Otherwise the boot fails.
+ # - Gems must come from the application's gem servers or from a path with
+ # a gemspec. Git gems are refused.
+ # - Gemfiles and gemspecs are evaluated on every boot. Installed gems are
+ # reused.
+ def gemfile=(value)
+ @gemfile = value == "true"
+ end
+
def self.parse(text)
metadata = new
text.each_line { |line| break unless metadata.parse_line(line) }
diff --git a/lib/plugin_gem.rb b/lib/plugin_gem.rb
index 62b099074f5..d867a861f3d 100644
--- a/lib/plugin_gem.rb
+++ b/lib/plugin_gem.rb
@@ -1,5 +1,8 @@
# frozen_string_literal: true
+require "fileutils"
+require "json"
+
module PluginGem
def self.load(path, name, version, opts = nil)
opts ||= {}
@@ -32,6 +35,390 @@ module PluginGem
end
end
+ GEMS_DIR = File.expand_path("../vendor/plugin-gems", __dir__)
+ LOCKFILE = File.join(GEMS_DIR, "Gemfile.lock")
+ # The source types a plugin Gemfile may use. A gem from a gem server is
+ # checked against the checksum in the lock. A path gem is code the plugin
+ # ships itself. Git gems are refused. A git gem keeps its version number
+ # from commit to commit, so the lock check in `resolve` can't tell two
+ # commits apart. And there is no checksum for a git checkout. Sources
+ # added by bundler plugins are refused because nothing checks what they
+ # download.
+ GEMFILE_SOURCES = [Bundler::Source::Rubygems, Bundler::Source::Path, Bundler::Source::Gemspec]
+
+ # Resolves the Gemfiles of all plugins with `# gemfile: true` together, as
+ # one bundle. A gem the application already has keeps the application's
+ # version. Every other gem gets the version from a plugin's Gemfile.lock.
+ # The resolved gems are activated, not required; each plugin requires its
+ # own gems.
+ def self.activate_gemfiles(plugins)
+ gemfiles =
+ plugins.filter_map do |plugin|
+ next unless plugin.metadata.gemfile
+ unless File.exist?(File.join(plugin.directory, "Gemfile.lock"))
+ raise "Plugin #{plugin.directory_name} has a Gemfile but no Gemfile.lock; run `bundle lock` in #{plugin.directory} and commit it"
+ end
+
+ File.join(plugin.directory, "Gemfile")
+ end
+ return if gemfiles.empty?
+
+ # A Gemfile or gemspec is Ruby code, and its result can change between
+ # boots. So the bundle is resolved on every boot, and that happens in a
+ # child process, see in_child_process.
+ in_child_process { install(gemfiles) }.each do |gemspec, path, directory|
+ spec = Gem::Specification.from_yaml(gemspec)
+ spec.loaded_from = path
+ spec.full_gem_path = directory
+ spec.activate
+ end
+ end
+
+ # Returns the resolved specs as YAML, each with its gemspec path and
+ # install directory. Dependencies come before the gems that need them,
+ # because activating a gem needs its dependencies activated first.
+ def self.install(gemfiles)
+ # The application's gems: name => [version, directory, full name, install
+ # directory]. The directory is only set for path and git gems. This must
+ # be read before resolving, because resolving overwrites the source of
+ # these spec objects.
+ pins =
+ Bundler.load.specs.to_h do |spec|
+ directory = spec.full_gem_path if spec.source.is_a?(Bundler::Source::Path)
+ [spec.name, [spec.version, directory, spec.full_name, spec.full_gem_path]]
+ end
+ # Ruby loads some of its default gems before bundler runs. They are not
+ # in the application's bundle, but their version can't change anymore.
+ Gem.loaded_specs.each_value do |spec|
+ pins[spec.name] ||= [spec.version, nil, spec.full_name, spec.full_gem_path]
+ end
+ pins.delete("bundler")
+ app_lock = Bundler.definition.locked_gems
+ app_groups = Bundler.definition.requested_dependencies.flat_map(&:groups).uniq
+ app_groups -= Bundler.settings[:without]
+ app_groups &= Bundler.settings[:only] if Bundler.settings[:only].any?
+ dsl = Bundler::Dsl.new
+ dependencies = plugin_dependencies(dsl, gemfiles, app_groups)
+ locks = gemfiles.to_h { [it, ensure_lock_matches(it)] }
+ locks.each { |gemfile, lock| ensure_app_remotes(gemfile, lock, app_lock) }
+ locked = locked_versions(dependencies, pins, locks, app_lock)
+
+ dependencies = dependencies.values.flatten
+ dependencies.each do |dependency|
+ version = locked[dependency.name]
+ # Bundler would report a conflict here as a gem it can't find in the
+ # application's version, without the plugin's requirement.
+ if version && pins.key?(dependency.name) && !dependency.requirement.satisfied_by?(version)
+ raise Bundler::GemfileError,
+ "Plugin #{dependency.gemfile.dirname.basename}'s Gemfile needs #{dependency.name} #{dependency.requirement}, but Discourse has #{version}"
+ end
+ dependency.requirement.concat(["= #{version}"]) if version
+ end
+ # Locked gems that no plugin Gemfile names directly (dependencies of
+ # dependencies) are added as pinned dependencies.
+ locked
+ .except(*dependencies.map(&:name), *pins.keys)
+ .each { |name, version| dsl.dependencies << Bundler::Dependency.new(name, "= #{version}") }
+ pins.each do |name, (version, directory)|
+ next unless locked.key?(name)
+ # A path or git gem of the application has no version on a gem server.
+ # It is pinned to its directory instead.
+ if directory
+ dsl.path(directory) { dsl.gem(name) }
+ else
+ dsl.dependencies << Bundler::Dependency.new(name, "= #{version}")
+ end
+ end
+ # The application's pins go last. Bundler takes a gem's source from the
+ # last dependency with that name, so a plugin can't replace an
+ # application gem with a path gem of the same name.
+ dsl.dependencies.unshift(*dependencies)
+
+ # First try with the installed gems only. That works offline and is enough
+ # unless a lock changed. Only then ask the gem servers.
+ with_install_lock do
+ [{ local: true }, {}].each do |options|
+ specs = resolve(dsl, pins, locked, locks.values, app_lock, options)
+ puts "Resolved #{specs.size} plugin gems from #{gemfiles.size} Gemfiles"
+ return specs
+ rescue Bundler::GemNotFound, Bundler::SolveFailure
+ raise unless options[:local]
+ end
+ end
+ rescue Bundler::BundlerError => e
+ raise "Resolving plugin gems failed for #{gemfiles.join(", ")}: #{e.message}"
+ end
+
+ # Processes that boot at the same time, like the web server and the job
+ # runner, would each install the missing gems: they decide that before
+ # bundler's own install lock. Behind this lock the later ones find the gems
+ # installed. Bundler's lock can't be used for this, it's taken again inside
+ # the installer. flock doesn't need write access, so a lock file created by
+ # another user still works. Without access to the directory there is nothing
+ # to install, so there is no lock either.
+ def self.with_install_lock
+ file =
+ begin
+ FileUtils.mkdir_p(GEMS_DIR)
+ File.open(File.join(GEMS_DIR, "install.lock"), File::RDONLY | File::CREAT)
+ rescue Errno::EACCES, Errno::EROFS
+ nil
+ end
+ return yield if !file
+
+ file.flock(File::LOCK_EX)
+ yield
+ ensure
+ file&.close
+ end
+
+ # Runs bundler's frozen-mode check on the plugin's Gemfile and
+ # Gemfile.lock. The check compares the two files without resolving
+ # anything and raises when they don't match. When the lock has a CHECKSUMS
+ # section, the check also raises for a gem without a checksum. Returns the
+ # parsed lock.
+ #
+ # BUNDLE_GEMFILE and BUNDLE_LOCKFILE are set to the plugin's files while
+ # the check runs. Bundler reads path gems relative to the Gemfile's
+ # directory, and its error messages name the default lockfile.
+ def self.ensure_lock_matches(gemfile)
+ dir = File.dirname(gemfile)
+ bundle_env = ENV.values_at("BUNDLE_GEMFILE", "BUNDLE_LOCKFILE")
+ ENV["BUNDLE_GEMFILE"], ENV["BUNDLE_LOCKFILE"] = gemfile, "#{gemfile}.lock"
+ Bundler.reset_paths!
+ Bundler
+ .settings
+ .temporary(frozen: true) do
+ definition = Bundler::Definition.build(gemfile, "#{gemfile}.lock", nil)
+ definition.ensure_equivalent_gemfile_and_lockfile(true)
+ if !definition.locked_checksums
+ raise "Plugin #{File.basename(dir)}'s Gemfile.lock has no checksums; run `bundle lock --add-checksums` in #{dir} and commit it"
+ end
+ definition.locked_gems
+ end
+ rescue Bundler::ProductionError => e
+ raise "Plugin #{File.basename(dir)}'s Gemfile.lock does not match its Gemfile; run `bundle lock` in #{dir} and commit it\n#{e.message}"
+ ensure
+ ENV["BUNDLE_GEMFILE"], ENV["BUNDLE_LOCKFILE"] = bundle_env
+ Bundler.reset_paths!
+ end
+
+ # A plugin may only use the application's gem servers. The servers are
+ # read from the plugin's lock; ensure_lock_matches already made sure the
+ # lock matches the Gemfile.
+ def self.ensure_app_remotes(gemfile, lock, app_lock)
+ remotes = app_lock.sources.grep(Bundler::Source::Rubygems).flat_map(&:remotes)
+ foreign = lock.sources.grep(Bundler::Source::Rubygems).flat_map(&:remotes) - remotes
+ return if foreign.empty?
+
+ raise "Plugin #{File.basename(File.dirname(gemfile))}'s Gemfile uses the gem source #{foreign.join(", ")}; a plugin's gems must come from the application's gem sources (#{remotes.join(", ")})"
+ end
+
+ def self.plugin_dependencies(dsl, gemfiles, app_groups)
+ # A Gemfile can eval_gemfile other files. Their gems count for the lock
+ # of the plugin's top-level Gemfile.
+ gemfiles.to_h do |gemfile|
+ dsl.eval_gemfile(gemfile)
+ # The dependencies are removed from the DSL before the next Gemfile is
+ # evaluated. Otherwise bundler raises when two plugins name the same
+ # gem with different requirements. The resolver can handle that: it
+ # takes the intersection of the requirements.
+ dependencies = dsl.dependencies.slice!(0..)
+ dependencies.each do |dependency|
+ next if dependency.source.nil? || GEMFILE_SOURCES.include?(dependency.source.class)
+
+ raise "Plugin #{dependency.gemfile.dirname.basename}'s Gemfile declares #{dependency.name} from #{dependency.source.class.name.demodulize.downcase}; a plugin's gems must come from the application's gem sources or a path"
+ end
+ dependencies.select! do |dependency|
+ dependency.should_include? && dependency.groups.intersect?(app_groups)
+ end
+ [gemfile, dependencies]
+ end
+ end
+
+ def self.resolve(dsl, pins, locked, locks, app_lock, options)
+ # This runs in the child process. Bundler still has the application's
+ # paths, gem home and settings. Replace them with the plugin bundle's.
+ Bundler.reset_paths!
+ Bundler
+ .settings
+ .temporary(
+ path: GEMS_DIR,
+ frozen: false,
+ deployment: false,
+ disable_checksum_validation: false,
+ only: nil,
+ with: nil,
+ without: nil,
+ ) do
+ Bundler.configure_gem_home_and_path(Bundler.bundle_path)
+ # After Bundler.setup, Gem::Specification only knows the application's
+ # gems. The resolver must also see the plugin gems that are already
+ # installed in GEMS_DIR.
+ Dir["#{Bundler.bundle_path}/specifications/*.gemspec"].each do |path|
+ Gem::Specification.add_spec(Gem::Specification.load(path))
+ end
+
+ definition = dsl.to_definition(LOCKFILE, {})
+ # Bundler checks every downloaded gem against the checksum in its
+ # lockfile. The checksums for the plugin gems come from the plugin
+ # locks, so they are copied into the sources of this definition.
+ # The application's lock is included too. Its gems are never
+ # downloaded, but this way LOCKFILE gets a checksum entry for them.
+ #
+ # A Gemfile can limit a source to some gems with
+ # `source "..." do ... end`. Bundler then creates a second source
+ # object for the same server. Both objects can install gems, so both
+ # get the checksums.
+ [app_lock, *locks].flat_map(&:sources)
+ .each do |locked_source|
+ definition
+ .sources
+ .rubygems_sources
+ .select { it == locked_source }
+ .each { it.checksum_store.merge!(locked_source.checksum_store) }
+ end
+ definition.setup_domain!(options)
+ # Check every resolved spec before anything is installed.
+ locked_specs =
+ locks.flat_map(&:specs).group_by(&:name).merge(app_lock.specs.group_by(&:name))
+ definition.specs.each do |spec|
+ next if spec.name == "bundler"
+ if pin = pins[spec.name]
+ if spec.full_name != pin[2] || spec.full_gem_path != pin[3]
+ raise "Resolved plugin bundle would replace core gem #{pin[2]} with #{spec.full_name} from #{spec.source}"
+ end
+ next
+ end
+ if spec.source.is_a?(Bundler::Source::Path) && !File.file?(spec.loaded_from)
+ raise "Plugin path gem #{spec.name} needs a .gemspec file in #{spec.source.path.expand_path(spec.source.root_path)}"
+ end
+ locked_spec =
+ locked_specs
+ .fetch(spec.name, [])
+ .find do |candidate|
+ candidate.full_name == spec.full_name && candidate.source == spec.source
+ end
+ if !locked.key?(spec.name) || locked_spec.nil?
+ raise "Resolved plugin gem #{spec.full_name} is not in the committed Gemfile.lock files with the same source; update the locks for #{Bundler.local_platform} and commit them"
+ end
+ if spec.source.is_a?(Bundler::Source::Rubygems) &&
+ !locked_spec.source.checksum_store.to_lock(locked_spec).match?(/(?: |,)sha256=/)
+ raise "Locked gem #{spec.full_name} has no SHA256 checksum; run `bundle lock --add-checksums` and commit it"
+ end
+ end
+ specs = definition.specs.reject { Gem.loaded_specs.key?(it.name) }
+ # When every gem is installed already, only the lockfile is written.
+ # Running the installer would reload all gemspecs of the application
+ # for nothing.
+ if specs.all? { it.is_a?(Bundler::StubSpecification) && !it.installation_missing? }
+ Bundler.create_bundle_path
+ definition.lock
+ else
+ # Building a native extension runs ruby in a subprocess. With
+ # RUBYOPT and BUNDLE_GEMFILE still set, that ruby would load the
+ # application's bundle into GEM_HOME. GEM_HOME now points at the
+ # plugin gem directory.
+ Bundler.unbundle_env!
+ Bundler::Installer.install(Bundler.root, definition, options)
+ end
+
+ # The checked specs go back to the application process as YAML. This
+ # way the application doesn't evaluate the gemspecs again and doesn't
+ # get bundler's source indexes.
+ specs.map do |spec|
+ metadata = spec.for_cache
+ # Gem::Specification.from_yaml only accepts the YAML of a plain
+ # Gem::Specification. A spec that bundler fetched from a gem server
+ # is a subclass, and its YAML names that subclass. The gemspec file
+ # installed with the gem is a plain Gem::Specification.
+ unless metadata.instance_of?(Gem::Specification)
+ metadata = Gem::Specification.load(spec.loaded_from).for_cache
+ end
+ [metadata.to_yaml, spec.loaded_from, spec.full_gem_path]
+ end
+ end
+ end
+
+ # Decides the version of every gem the plugin Gemfiles need and returns
+ # name => version.
+ # - A gem in the application's lock gets the application's version. That
+ # also holds for gems in a group the application doesn't load.
+ # - Every other gem gets its version from the plugin locks, dependencies
+ # of dependencies included. When several plugins lock a gem, the highest
+ # version wins. So a plugin with an old lock doesn't force an old
+ # version on the others.
+ # - Only gems reachable from the plugins' dependencies are included. Other
+ # entries in a lock are development tooling or stale.
+ def self.locked_versions(dependencies, pins, locks, app_lock)
+ app_specs = app_lock.specs.group_by(&:name)
+ dependencies.each_with_object({}) do |(gemfile, gemfile_dependencies), versions|
+ specs = locks.fetch(gemfile).specs.group_by(&:name).merge(app_specs)
+ names = gemfile_dependencies.map(&:name)
+ force_ruby = gemfile_dependencies.select(&:force_ruby_platform).map(&:name)
+ names.each do |name|
+ # An application gem keeps its version. Its dependencies are followed
+ # so they get pinned too.
+ if pin = pins[name]
+ versions[name] = pin[0]
+ variants = specs.fetch(name, []).select { it.full_name == pin[2] }
+ names.concat(variants.flat_map { it.dependencies.map(&:name) } - names)
+ next
+ end
+ raise "Plugin dependency #{name} is missing from #{gemfile}.lock" unless specs.key?(name)
+
+ variants =
+ Bundler::MatchPlatform.select_best_platform_match(
+ specs[name],
+ Bundler.local_platform,
+ force_ruby: force_ruby.include?(name),
+ )
+ if variants.empty?
+ raise "Plugin #{File.basename(File.dirname(gemfile))}'s Gemfile.lock has no compatible platform for #{name}; run `bundle lock --add-platform #{Bundler.local_platform}` and commit it"
+ end
+
+ variants.each { |spec| names.concat(spec.dependencies.map(&:name) - names) }
+ version = variants.first.version
+ versions[name] = version if versions[name].nil? || version > versions[name]
+ end
+ end
+ end
+
+ # Resolving runs in a child process. Bundler stores the remote gem index
+ # (about 200 MB) in the spec objects of the running bundle. In the
+ # application process that memory would never be freed.
+ #
+ # The child ends with exit! so that at_exit hooks and finalizers don't
+ # run. They would close sockets the child inherited from the application.
+ def self.in_child_process
+ reader, writer = IO.pipe
+ pid =
+ fork do
+ reader.close
+ result =
+ begin
+ { "value" => yield }
+ rescue Exception => e
+ { "error" => e.message }
+ end
+ writer.write(JSON.generate(result))
+ $stdout.flush
+ ensure
+ exit!(0)
+ end
+ writer.close
+ output = reader.read
+ Process.wait(pid)
+ raise "Resolving plugin gems failed, the child process died" if output.empty?
+
+ result = JSON.parse(output)
+ raise result["error"] if result["error"]
+
+ result["value"]
+ ensure
+ reader&.close
+ end
+
def self.platform_variants(spec_file)
platform_less = "#{spec_file}.gemspec"
platform_full = "#{spec_file}-#{RUBY_PLATFORM}.gemspec"
diff --git a/spec/lib/plugin/instance_spec.rb b/spec/lib/plugin/instance_spec.rb
index 2dfb7adc91a..841e6e8423b 100644
--- a/spec/lib/plugin/instance_spec.rb
+++ b/spec/lib/plugin/instance_spec.rb
@@ -516,6 +516,19 @@ TEXT
end
end
+ describe "#gem" do
+ let(:plugin_instance) { described_class.new(metadata, "/tmp/sample/plugin.rb") }
+
+ it "refuses to be mixed with a Gemfile" do
+ metadata.gemfile = "true"
+
+ expect { plugin_instance.gem("some_gem", "1.0.0") }.to raise_error(
+ RuntimeError,
+ "Plugin sample declares its gems in a Gemfile, `gem` can't be mixed with that",
+ )
+ end
+ end
+
describe "#add_model_callback" do
let(:metadata) do
metadata = Plugin::Metadata.new
diff --git a/spec/lib/plugin_gem_spec.rb b/spec/lib/plugin_gem_spec.rb
new file mode 100644
index 00000000000..67eaf0aaa06
--- /dev/null
+++ b/spec/lib/plugin_gem_spec.rb
@@ -0,0 +1,777 @@
+# frozen_string_literal: true
+
+require "rubygems/package"
+require "bundler/lockfile_generator"
+
+RSpec.describe PluginGem do
+ let(:tmp_dir) { Dir.mktmpdir("plugin-gem-spec") }
+ let(:repo) { File.join(tmp_dir, "repo") }
+ let(:gems_dir) { File.join(tmp_dir, "plugin-gems") }
+ let(:app_versions) { Bundler.load.specs.to_h { [it.name, it.version.to_s] } }
+ let(:rack) { app_versions.fetch("rack") }
+ let(:app_checksums) do
+ File.read(Rails.root.join("Gemfile.lock")).scan(/^ (\S+) \([^)]+\) (sha256=\h+)$/).to_h
+ end
+ let!(:booted_specs) { Gem.loaded_specs.dup.merge(git_gem.name => git_gem) }
+ let(:loaded_specs) { booted_specs.dup }
+ let(:plugins) { [] }
+ let(:alpha_lock) { { "alpha" => "1.1.0", "beta" => "1.0.0" } }
+ # Plays the role of a gem the application loads from git. Once checked out,
+ # a git gem is a path source for bundler: a directory with a gemspec. Only
+ # that directory matters here.
+ let(:git_gem) do
+ dir = File.join(tmp_dir, "app", "delta")
+ write_gemspec(dir, "delta", "1.0.0")
+ Bundler::Source::Path.new("path" => dir).specs.first
+ end
+ let(:repo_gems) do
+ [
+ ["alpha", "1.0.0", { "beta" => "~> 1.0", "rack" => ">= 1" }],
+ ["alpha", "1.1.0", { "beta" => "~> 1.0", "rack" => ">= 1" }],
+ %w[beta 1.0.0],
+ %w[beta 1.1.0],
+ ["gamma", "1.0.0", { git_gem.name => ">= 0" }],
+ %w[delta 1.0.0],
+ %w[shared 1.0.0],
+ %w[shared 1.1.0],
+ %w[solo 1.0.0],
+ # Newer than anything the application has, so only a pin keeps them out.
+ %w[rack 99.0.0],
+ ]
+ end
+
+ def gem_spec(name, version, dependencies = {}, platform = "ruby")
+ Gem::Specification.new do |spec|
+ spec.name = name
+ spec.version = version
+ spec.platform = platform
+ spec.summary = name
+ spec.authors = ["test"]
+ dependencies.each { |dependency, requirement| spec.add_dependency(dependency, requirement) }
+ end
+ end
+
+ def write_gemspec(dir, name, version)
+ FileUtils.mkdir_p(dir)
+ File.write(File.join(dir, "#{name}.gemspec"), gem_spec(name, version).to_ruby)
+ end
+
+ def build_repo
+ specs = repo_gems.map { gem_spec(*it) }
+ FileUtils.mkdir_p(["#{repo}/gems", "#{repo}/quick/Marshal.4.8"])
+ specs.each do |spec|
+ yield spec if block_given?
+ Gem::DefaultUserInteraction.use_ui(Gem::SilentUI.new) do
+ Gem::Package.build(spec, true, false, "#{repo}/gems/#{spec.file_name}")
+ end
+ File.binwrite(
+ "#{repo}/quick/Marshal.4.8/#{spec.full_name}.gemspec.rz",
+ Gem.deflate(Marshal.dump(spec)),
+ )
+ end
+ index = specs.map { |spec| [spec.name, spec.version, spec.platform.to_s] }
+ File.binwrite("#{repo}/specs.4.8.gz", Gem::Util.gzip(Marshal.dump(index)))
+ end
+
+ # `lock` is name => version. The Gemfile.lock is written as `bundle lock`
+ # would write it, with dependencies and checksums taken from the repo. nil
+ # writes no Gemfile.lock. `path` adds path gems: name => [version,
+ # dependencies]. `checksums` replaces checksums by gem name; nil writes no
+ # CHECKSUMS section.
+ def add_plugin(name, gemfile, lock:, opt_in: true, **options)
+ dir = File.join(tmp_dir, "plugins", name)
+ FileUtils.mkdir_p(dir)
+ File.write(File.join(dir, "plugin.rb"), "# name: #{name}\n# gemfile: #{opt_in}\n")
+ write_plugin_files(dir, gemfile.chomp, lock, **options)
+ plugins << Plugin::Instance.parse_from_source(File.join(dir, "plugin.rb"))
+ dir
+ end
+
+ def write_plugin_files(
+ dir,
+ gemfile,
+ lock,
+ path: {},
+ checksums: {},
+ source: "https://rubygems.org",
+ scoped: false
+ )
+ gemfile =
+ if scoped
+ <<~GEMFILE
+ source "#{source}" do
+ #{gemfile}
+ end
+ GEMFILE
+ else
+ <<~GEMFILE
+ source "#{source}"
+ #{gemfile}
+ GEMFILE
+ end
+ File.write(File.join(dir, "Gemfile"), gemfile)
+ return if lock.nil?
+
+ lockfile = File.join(dir, "Gemfile.lock")
+ definition = Bundler::Definition.build(File.join(dir, "Gemfile"), lockfile, nil)
+ definition.sources.git_sources.clear
+ gems =
+ lock.map do |name, version|
+ [name, version, repo_gems.find { it[0] == name && it[1] == version }&.dig(2)]
+ end
+ # `bundle lock` would add the application's gems the plugin's gems need.
+ app_gems = gems.flat_map { it[2]&.keys || [] }.uniq - lock.keys
+ gems += app_gems.map { [it, app_versions.fetch(it), nil] }
+ rubygems_source =
+ definition.sources.non_global_rubygems_sources.first ||
+ definition.sources.global_rubygems_source
+ specs = gems.map { locked_spec(*it, rubygems_source) }
+ specs +=
+ path.map do |name, (version, dependencies)|
+ source = definition.sources.path_sources.find { it.path.to_s == name }
+ locked_spec(name, version, dependencies, source)
+ end
+ add_checksums(specs, checksums, lockfile) if checksums
+ lock_definition =
+ instance_double(
+ Bundler::Definition,
+ sources: definition.sources,
+ resolve: Bundler::SpecSet.new(specs),
+ platforms: [Gem::Platform::RUBY],
+ dependencies: definition.dependencies,
+ locked_checksums: !checksums.nil?,
+ locked_gems: nil,
+ locked_ruby_version: nil,
+ bundler_version_to_lock: Bundler.gem_version,
+ )
+ # `bundle lock` runs with the Gemfile's directory as Bundler.root and
+ # writes path gems relative to it.
+ allow(Bundler).to receive(:root).and_return(Pathname.new(dir))
+ File.write(lockfile, Bundler::LockfileGenerator.generate(lock_definition))
+ allow(Bundler).to receive(:root).and_call_original
+ end
+
+ def locked_spec(name, version, dependencies, source)
+ spec =
+ Bundler::LazySpecification.new(name, Gem::Version.new(version), Gem::Platform::RUBY, source)
+ spec.dependencies = dependencies&.map { Gem::Dependency.new(*it) } || []
+ spec
+ end
+
+ def add_checksums(specs, checksums, lockfile)
+ specs.each do |spec|
+ value = checksum(spec.name, spec.version) if spec.source.is_a?(Bundler::Source::Rubygems)
+ value = checksums.fetch(spec.name, value)
+ parsed = Bundler::Checksum.from_lock(value, lockfile) if value
+ spec.source.checksum_store.register(spec, parsed)
+ end
+ end
+
+ def checksum(name, version)
+ gem = "#{repo}/gems/#{name}-#{version}.gem"
+ return Bundler::Checksum.from_gem_package(Gem::Package.new(gem)).to_lock if File.exist?(gem)
+
+ # The application's lock has no checksums for its path and git gems, so
+ # those get a made-up one.
+ app_checksums.fetch(name) { "sha256=#{Digest::SHA256.hexdigest(name)}" }
+ end
+
+ # A fresh process: only the application's gems are activated.
+ def boot
+ loaded_specs.replace(booted_specs)
+ PluginGem.activate_gemfiles(plugins)
+ end
+
+ # Bundler's temporary settings can't hold an empty group list: an empty
+ # value removes the key, and the host's bundle config shows through. The
+ # child resets the settings, so every instance is stubbed.
+ #
+ # The application gets an optional `:extras` group, so the spec doesn't
+ # depend on the optional groups of the application's own Gemfile.
+ def boot_with_groups(with: [], without: [], only: [])
+ groups = { with:, without:, only: }
+ allow_any_instance_of(Bundler::Settings).to receive(:[]).and_wrap_original do |original, key|
+ groups.fetch(key.to_sym) { original.call(key) }
+ end
+ extras = Bundler::Dependency.new("extras", ">= 0", "group" => :extras)
+ allow(Bundler.definition).to receive(:requested_dependencies).and_wrap_original do |original|
+ with.include?(:extras) ? original.call + [extras] : original.call
+ end
+ boot
+ end
+
+ def installed
+ Dir["#{gems_dir}/ruby/*/specifications/*.gemspec"].map { File.basename(it, ".gemspec") }.sort
+ end
+
+ def activated
+ (loaded_specs.keys - booted_specs.keys).map { loaded_specs[it].full_name }.sort
+ end
+
+ around do |example|
+ load_path = $LOAD_PATH.dup
+ mirror = "BUNDLE_MIRROR__HTTPS://RUBYGEMS__ORG/"
+ ENV[mirror] = "file://#{repo}/"
+ application_lockfile = Bundler.default_lockfile
+ application_lock = File.read(application_lockfile)
+ stub_const(PluginGem, :GEMS_DIR, gems_dir) do
+ stub_const(PluginGem, :LOCKFILE, File.join(gems_dir, "Gemfile.lock")) { example.run }
+ end
+ expect(File.read(application_lockfile)).to eq(application_lock)
+ ensure
+ ENV.delete(mirror)
+ $LOAD_PATH.replace(load_path)
+ FileUtils.rm_rf(tmp_dir)
+ end
+
+ before do
+ build_repo
+ app_bundle = Bundler.load
+ allow(Bundler).to receive(:load).and_return(
+ instance_double(Bundler::Runtime, specs: Bundler::SpecSet.new([*app_bundle.specs, git_gem])),
+ )
+ allow(Gem).to receive(:loaded_specs).and_return(loaded_specs)
+ end
+
+ it "uses the highest shared lock version and pins its transitive dependencies" do
+ add_plugin(
+ "one",
+ <<~GEMFILE,
+ gem "alpha", ">= 1"
+ gem "rack"
+ GEMFILE
+ lock: {
+ "alpha" => "1.1.0",
+ "beta" => "1.0.0",
+ "rack" => "99.0.0",
+ },
+ )
+ add_plugin("two", %(gem "alpha", "~> 1.0"), lock: { "alpha" => "1.0.0", "beta" => "1.0.0" })
+
+ boot
+ expect(installed).to eq(%w[alpha-1.1.0 beta-1.0.0])
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0])
+ expect(File.read(File.join(gems_dir, "Gemfile.lock"))).to include(
+ " alpha (1.1.0) #{checksum("alpha", "1.1.0")}\n",
+ " rack (#{rack}) #{checksum("rack", rack)}\n",
+ )
+ end
+
+ it "fails when the highest shared lock version does not satisfy another plugin" do
+ add_plugin("one", %(gem "alpha"), lock: alpha_lock)
+ add_plugin("two", %(gem "alpha", "~> 1.0.0"), lock: { "alpha" => "1.0.0", "beta" => "1.0.0" })
+
+ expect { boot }.to raise_error(
+ %r{Resolving plugin gems failed for .*/one/Gemfile, .*/two/Gemfile:.*alpha.*1.1.0}m,
+ )
+ expect(installed).to be_empty
+ end
+
+ it "fails when a plugin needs a version of a gem the application has at another one" do
+ add_plugin("one", %(gem "rack", "> #{rack}"), lock: { "rack" => "99.0.0" })
+
+ expect { boot }.to raise_error(
+ %r{Resolving plugin gems failed for .*/one/Gemfile: Plugin one's Gemfile needs rack > #{rack}, but Discourse has #{rack}\z},
+ )
+ expect(installed).to be_empty
+ end
+
+ it "uses the application's git gem for a direct and a transitive dependency" do
+ add_plugin(
+ "one",
+ <<~GEMFILE,
+ gem "#{git_gem.name}"
+ gem "gamma"
+ GEMFILE
+ lock: {
+ "gamma" => "1.0.0",
+ git_gem.name => git_gem.version.to_s,
+ },
+ )
+
+ boot
+ expect(installed).to eq(%w[gamma-1.0.0])
+ expect(activated).to eq(%w[gamma-1.0.0])
+ end
+
+ it "fails when a plugin needs another version of the application's git gem" do
+ name, version = git_gem.name, git_gem.version.to_s
+ add_plugin("one", %(gem "#{name}", "> #{version}"), lock: { name => "99.0.0" })
+
+ expect { boot }.to raise_error(
+ %r{Resolving plugin gems failed for .*/one/Gemfile:.*#{name}.*#{version}}m,
+ )
+ expect(installed).to be_empty
+ end
+
+ it "keeps the version of a loaded gem that is not in the application's bundle" do
+ # Ruby loads some of its default gems before bundler runs. A plugin gem
+ # installed on an earlier boot plays that role here.
+ dir = add_plugin("one", %(gem "shared"), lock: { "shared" => "1.0.0" })
+ boot
+ booted_specs["shared"] = loaded_specs.fetch("shared")
+
+ write_plugin_files(dir, %(gem "shared"), { "shared" => "1.1.0" })
+ boot
+ expect(installed).to eq(%w[shared-1.0.0])
+ expect(activated).to be_empty
+
+ write_plugin_files(dir, %(gem "shared", ">= 1.1"), { "shared" => "1.1.0" })
+ expect { boot }.to raise_error(
+ /Plugin one's Gemfile needs shared >= 1.1, but Discourse has 1.0.0\z/,
+ )
+ expect(installed).to eq(%w[shared-1.0.0])
+ end
+
+ it "loads dependencies from the groups enabled for the application" do
+ add_plugin(
+ "one",
+ <<~GEMFILE,
+ gem "beta"
+ gem "solo"
+ gem "alpha", group: :development
+ gem "shared", group: :test
+ gem "gamma", group: :extras
+ GEMFILE
+ lock: {
+ "alpha" => "1.1.0",
+ "beta" => "1.0.0",
+ "gamma" => "1.0.0",
+ git_gem.name => git_gem.version.to_s,
+ "shared" => "1.0.0",
+ "solo" => "1.0.0",
+ },
+ )
+
+ boot_with_groups(without: %i[development test])
+ expect(activated).to eq(%w[beta-1.0.0 solo-1.0.0])
+
+ boot_with_groups(without: %i[development])
+ expect(activated).to eq(%w[beta-1.0.0 shared-1.0.0 solo-1.0.0])
+
+ boot_with_groups
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0 shared-1.0.0 solo-1.0.0])
+
+ # Like bundler, `only` doesn't add the default group. beta stays as
+ # alpha's dependency.
+ boot_with_groups(only: %i[development])
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0])
+
+ boot_with_groups(only: %i[default development])
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0 solo-1.0.0])
+
+ boot_with_groups(with: %i[extras], without: %i[development test])
+ expect(activated).to eq(%w[beta-1.0.0 gamma-1.0.0 solo-1.0.0])
+ end
+
+ it "ignores dependencies for other platforms when selecting lock pins" do
+ add_plugin(
+ "one",
+ %(gem "alpha", platforms: :jruby),
+ lock: {
+ "alpha" => "1.1.0",
+ "beta" => "1.1.0",
+ },
+ )
+
+ boot
+ expect(installed).to be_empty
+ end
+
+ it "resolves a path gem's dependencies and activates it from its directory" do
+ dir =
+ add_plugin(
+ "one",
+ <<~GEMFILE,
+ gem "engine", path: "engine"
+ gem "alpha", "~> 1.0"
+ GEMFILE
+ lock: alpha_lock,
+ path: {
+ "engine" => ["0.0.1", { "alpha" => ">= 1.1" }],
+ },
+ )
+ FileUtils.mkdir_p(File.join(dir, "engine"))
+ File.write(File.join(dir, "engine", "VERSION"), "0.0.1")
+ File.write(File.join(dir, "engine", "engine.gemspec"), <<~GEMSPEC)
+ Gem::Specification.new do |s|
+ s.name = "engine"
+ s.version = File.read("VERSION")
+ s.summary = "test"
+ s.authors = ["test"]
+ s.require_paths = ["src"]
+ s.add_dependency "alpha", ">= 1.1"
+ end
+ GEMSPEC
+ engine_file = File.join(dir, "engine", "src", "plugin_gem_test_engine.rb")
+ FileUtils.mkdir_p(File.dirname(engine_file))
+ File.write(engine_file, "true\n")
+
+ boot
+ expect(installed).to eq(%w[alpha-1.1.0 beta-1.0.0])
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0 engine-0.0.1])
+ expect(require "plugin_gem_test_engine").to eq(true)
+
+ File.write(File.join(dir, "engine", "VERSION"), "2.0.0")
+ expect { boot }.to raise_error(/Gemfile.lock does not match its Gemfile/)
+ expect(activated).to be_empty
+ ensure
+ $LOADED_FEATURES.delete(engine_file)
+ end
+
+ it "rejects a path gem without a gemspec before installing any gems" do
+ dir =
+ add_plugin(
+ "one",
+ %(gem "engine", "0.0.1", path: "engine"),
+ lock: {
+ },
+ path: {
+ "engine" => ["0.0.1", {}],
+ },
+ )
+ FileUtils.mkdir_p(File.join(dir, "engine"))
+
+ expect { boot }.to raise_error("Plugin path gem engine needs a .gemspec file in #{dir}/engine")
+ expect(installed).to be_empty
+ expect(activated).to be_empty
+ end
+
+ it "pins core gems and their dependencies when overriding a plugin's path source" do
+ dir =
+ add_plugin(
+ "one",
+ %(gem "rack-test", path: "rack-test"),
+ lock: {
+ },
+ path: {
+ "rack-test" => ["99.0.0", {}],
+ },
+ )
+ write_gemspec(File.join(dir, "rack-test"), "rack-test", "99.0.0")
+
+ boot
+ expect(installed).to be_empty
+ expect(activated).to be_empty
+ expect(File.read(File.join(gems_dir, "Gemfile.lock"))).to include(
+ " rack-test (#{app_versions.fetch("rack-test")})\n",
+ " rack (#{rack})\n",
+ )
+ end
+
+ it "honors core's lock even when the gem is outside core's active groups" do
+ core_dir = add_plugin("core", %(gem "beta"), lock: { "beta" => "1.0.0" })
+ plugins.clear
+ core_lock = Bundler::LockfileParser.new(File.read(File.join(core_dir, "Gemfile.lock")))
+ allow(Bundler.definition).to receive(:locked_gems).and_return(core_lock)
+ dir = add_plugin("one", %(gem "beta"), lock: { "beta" => "1.1.0" })
+
+ boot
+ expect(activated).to eq(%w[beta-1.0.0])
+
+ write_plugin_files(dir, %(gem "beta", ">= 1.1"), { "beta" => "1.1.0" })
+ expect { boot }.to raise_error(/Resolving plugin gems failed.*beta/m)
+ expect(installed).to eq(%w[beta-1.0.0])
+ expect(activated).to be_empty
+ end
+
+ it "reevaluates nested Gemfiles and conditional dependencies on each boot" do
+ previous = ENV["PLUGIN_GEM_TEST_BETA"]
+ dir = add_plugin("one", %(gem "beta"), lock: { "beta" => "1.0.0" })
+ nested = File.join(dir, "dependencies.rb")
+ File.write(nested, %(gem "beta", group: :development\n))
+ File.write(
+ File.join(dir, "Gemfile"),
+ %(source "https://rubygems.org"\neval_gemfile "dependencies.rb"\n),
+ )
+ boot_with_groups(without: %i[development])
+ expect(activated).to be_empty
+
+ File.write(nested, <<~GEMFILE)
+ install_if -> { ENV["PLUGIN_GEM_TEST_BETA"] == "1" } do
+ gem "beta"
+ end
+ GEMFILE
+ ENV["PLUGIN_GEM_TEST_BETA"] = "1"
+ boot
+ expect(activated).to eq(%w[beta-1.0.0])
+
+ ENV["PLUGIN_GEM_TEST_BETA"] = "0"
+ boot
+ expect(activated).to be_empty
+ ensure
+ ENV["PLUGIN_GEM_TEST_BETA"] = previous
+ end
+
+ it "ignores transitive lock pins from other variants when forcing the Ruby gem" do
+ dir =
+ add_plugin(
+ "one",
+ %(gem "alpha", force_ruby_platform: true),
+ lock: {
+ "alpha" => "1.1.0",
+ "beta" => "1.0.0",
+ "shared" => "1.1.0",
+ },
+ )
+ lockfile = File.join(dir, "Gemfile.lock")
+ lock = File.read(lockfile)
+ ["java", Gem::Platform.local.to_s].each do |platform|
+ lock.sub!(
+ " alpha (1.1.0)\n",
+ " alpha (1.1.0-#{platform})\n shared (>= 1)\n alpha (1.1.0)\n",
+ )
+ lock.sub!("PLATFORMS\n", "PLATFORMS\n #{platform}\n")
+ lock.sub!(
+ "CHECKSUMS\n",
+ "CHECKSUMS\n alpha (1.1.0-#{platform}) #{checksum("alpha", "1.1.0")}\n",
+ )
+ end
+ File.write(lockfile, lock)
+ add_plugin("two", %(gem "shared", "1.0.0"), lock: { "shared" => "1.0.0" })
+
+ boot
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0 shared-1.0.0])
+ end
+
+ it "rejects unlocked variants and dependencies deep in the graph before installing any gems" do
+ repo_gems << ["chain", "1.0.0", { "alpha" => ">= 1" }]
+ repo_gems << ["beta", "1.0.0", { "shared" => ">= 1" }, Gem::Platform.local.to_s]
+ build_repo
+ add_plugin(
+ "one",
+ %(gem "chain"),
+ lock: {
+ "chain" => "1.0.0",
+ "alpha" => "1.1.0",
+ "beta" => "1.0.0",
+ },
+ )
+
+ expect { boot }.to raise_error(
+ /Resolved plugin gem .* is not in the committed Gemfile.lock files/,
+ )
+ expect(installed).to be_empty
+ end
+
+ it "rejects a replacement platform variant of an active core gem" do
+ repo_gems << ["rack", rack, {}, Gem::Platform.local.to_s]
+ build_repo
+ add_plugin("one", %(gem "alpha"), lock: alpha_lock)
+
+ expect { boot }.to raise_error(/Resolved plugin bundle would replace core gem rack-/)
+ expect(installed).to be_empty
+ expect(activated).to be_empty
+ end
+
+ it "rejects a lock with no compatible variant instead of dropping its version pin" do
+ dir = add_plugin("one", %(gem "beta"), lock: { "beta" => "1.0.0" })
+ lockfile = File.join(dir, "Gemfile.lock")
+ File.write(lockfile, File.read(lockfile).gsub("beta (1.0.0)", "beta (1.0.0-java)"))
+
+ expect { boot }.to raise_error(/Gemfile.lock has no compatible platform for beta/)
+ expect(installed).to be_empty
+ end
+
+ it "builds extensions without inheriting the application's bundle" do
+ repo_gems << %w[native 1.0.0]
+ Dir.chdir(tmp_dir) do
+ File.write("extconf.rb", 'File.write("Makefile", "all install clean:\n\t@true\n")')
+ build_repo { |spec| spec.files = spec.extensions = ["extconf.rb"] if spec.name == "native" }
+ end
+ add_plugin("one", %(gem "native"), lock: { "native" => "1.0.0" })
+
+ boot
+ expect(activated).to eq(%w[native-1.0.0])
+ expect(loaded_specs.fetch("native").missing_extensions?).to eq(false)
+ end
+
+ it "waits for another process that installs plugin gems, even with a lock file it can't write" do
+ add_plugin("one", %(gem "alpha"), lock: alpha_lock)
+ FileUtils.mkdir_p(gems_dir)
+ FileUtils.touch(File.join(gems_dir, "install.lock"))
+ File.chmod(0444, File.join(gems_dir, "install.lock"))
+
+ File.open(File.join(gems_dir, "install.lock"), File::RDONLY) do |lock|
+ lock.flock(File::LOCK_EX)
+ release =
+ Thread.new do
+ sleep 1
+ lock.flock(File::LOCK_UN)
+ end
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+
+ boot
+
+ expect(Process.clock_gettime(Process::CLOCK_MONOTONIC) - started).to be >= 1
+ release.join
+ end
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0])
+ end
+
+ it "fails a plugin's `gem` that needs another version of a gem from a plugin Gemfile" do
+ add_plugin("one", %(gem "alpha"), lock: alpha_lock)
+ boot
+ plugin_rb = File.join(tmp_dir, "plugins", "two", "plugin.rb")
+ FileUtils.mkdir_p(File.dirname(plugin_rb))
+ File.write(plugin_rb, "# name: two\n")
+ load_alpha = ->(version) do
+ PluginGem.load(plugin_rb, "alpha", version, source: "file://#{repo}/", require: false)
+ end
+
+ expect { load_alpha.call("1.0.0") }.to raise_error(
+ Gem::LoadError,
+ "can't activate alpha-1.0.0, already activated alpha-1.1.0",
+ )
+ expect { load_alpha.call("1.1.0") }.not_to raise_error
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0])
+ end
+
+ it "does nothing without a plugin that opts in" do
+ add_plugin("one", %(gem "alpha"), lock: nil, opt_in: false)
+
+ expect { boot }.not_to output.to_stdout_from_any_process
+ expect(Dir.exist?(gems_dir)).to eq(false)
+ end
+
+ it "resolves from the installed gems after a change, and fails for a missing gem when offline" do
+ add_plugin("one", %(gem "alpha"), lock: alpha_lock)
+ add_plugin("two", %(gem "shared"), lock: { "shared" => "1.1.0" })
+ boot
+
+ FileUtils.rm_rf(repo)
+ expect { boot }.not_to output(/Fetching|Installing/).to_stdout_from_any_process
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0 shared-1.1.0])
+
+ gem_dir = loaded_specs.fetch("alpha").full_gem_path
+ FileUtils.rm_rf(gem_dir)
+ plugins.pop
+ boot
+ expect(Dir.exist?(gem_dir)).to eq(true)
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0])
+
+ add_plugin("three", %(gem "beta", ">= 1.1"), lock: { "beta" => "1.1.0" })
+ expect { boot }.to raise_error(/Resolving plugin gems failed.*Could not fetch specs/)
+ end
+
+ it "refuses a gem source the application doesn't use" do
+ add_plugin("one", %(gem "alpha"), lock: alpha_lock, source: "https://gems.example.com")
+
+ expect { boot }.to raise_error(
+ %r{Plugin one's Gemfile uses the gem source https://gems.example.com/; a plugin's gems must come from the application's gem sources \(https://rubygems.org/[^)]*\)},
+ )
+ expect(Dir.exist?(gems_dir)).to eq(false)
+ end
+
+ it "refuses a git gem" do
+ add_plugin("one", %(gem "alpha", git: "https://example.com/alpha.git"), lock: alpha_lock)
+
+ expect { boot }.to raise_error(
+ /Plugin one's Gemfile declares alpha from git; a plugin's gems must come from the application's gem sources or a path/,
+ )
+ expect(Dir.exist?(gems_dir)).to eq(false)
+ end
+
+ describe "a plugin's Gemfile.lock" do
+ it "is required" do
+ add_plugin("one", %(gem "alpha"), lock: nil)
+
+ expect { boot }.to raise_error(
+ %r{Plugin one has a Gemfile but no Gemfile.lock; run `bundle lock` in .*/one and commit it},
+ )
+ expect(Dir.exist?(gems_dir)).to eq(false)
+ end
+
+ it "must match its Gemfile" do
+ # beta is a dependency of alpha and so in the lock; shared isn't in the
+ # lock at all.
+ dir = add_plugin("one", %(gem "alpha"), lock: alpha_lock)
+ File.write(
+ File.join(dir, "Gemfile"),
+ %(source "https://rubygems.org"\ngem "alpha"\ngem "shared"\n),
+ )
+
+ expect { boot }.to raise_error(
+ %r{Plugin one's Gemfile.lock does not match its Gemfile; run `bundle lock` in .*/one and commit it\n.*added to the Gemfile:\n\* shared}m,
+ )
+ expect(Dir.exist?(gems_dir)).to eq(false)
+ end
+
+ it "needs checksums" do
+ add_plugin("one", %(gem "alpha"), lock: alpha_lock, checksums: nil)
+
+ expect { boot }.to raise_error(
+ %r{Plugin one's Gemfile.lock has no checksums; run `bundle lock --add-checksums` in .*/one and commit it},
+ )
+ expect(Dir.exist?(gems_dir)).to eq(false)
+ end
+
+ it "needs a SHA256 checksum for every downloaded gem" do
+ dir = add_plugin("one", %(gem "alpha"), lock: alpha_lock)
+ lock = File.join(dir, "Gemfile.lock")
+ contents = File.read(lock)
+ File.write(lock, contents.sub(/^ beta .*\n/, ""))
+
+ expect { boot }.to raise_error(
+ /does not match its Gemfile.*missing a CHECKSUMS entry for "beta"/m,
+ )
+ expect(Dir.exist?(gems_dir)).to eq(false)
+
+ File.write(lock, contents.sub(checksum("beta", "1.0.0"), "sha512=#{"0" * 128}"))
+ expect { boot }.to raise_error(/Locked gem beta-1.0.0 has no SHA256 checksum/)
+ expect(installed).to be_empty
+ end
+
+ it "fails for a gem whose checksum differs" do
+ add_plugin(
+ "one",
+ %(gem "alpha"),
+ lock: alpha_lock,
+ checksums: {
+ "beta" => "sha256=#{"0" * 64}",
+ },
+ )
+
+ Bundler
+ .settings
+ .temporary(disable_checksum_validation: true) do
+ expect { boot }.to raise_error(
+ /Bundler found mismatched checksums.*beta \(1\.0\.0\) sha256=0{64}/m,
+ )
+ end
+ expect(installed).not_to include("beta-1.0.0")
+ end
+
+ it "verifies the checksums of every plugin when the first one scopes its source" do
+ add_plugin("one", %( gem "alpha"), lock: alpha_lock, scoped: true)
+ add_plugin(
+ "two",
+ %(gem "shared"),
+ lock: {
+ "shared" => "1.1.0",
+ },
+ checksums: {
+ "shared" => "sha256=#{"0" * 64}",
+ },
+ )
+
+ expect { boot }.to raise_error(
+ /Bundler found mismatched checksums.*shared \(1\.1\.0\) sha256=0{64}/m,
+ )
+ expect(installed).not_to include("shared-1.1.0")
+ end
+
+ it "updates pinned gems when the lock changes and reuses unchanged dependencies" do
+ dir = add_plugin("one", %(gem "alpha"), lock: { "alpha" => "1.0.0", "beta" => "1.0.0" })
+ boot
+ expect(activated).to eq(%w[alpha-1.0.0 beta-1.0.0])
+
+ write_plugin_files(dir, %(gem "alpha"), alpha_lock)
+ expect { boot }.to output(
+ satisfy { it.include?("Installing alpha 1.1.0") && !it.include?("Installing beta") },
+ ).to_stdout_from_any_process
+ expect(installed).to eq(%w[alpha-1.0.0 alpha-1.1.0 beta-1.0.0])
+ expect(activated).to eq(%w[alpha-1.1.0 beta-1.0.0])
+ end
+ end
+end