From abd2d865fd4a4968807ec10a8bf3a770313f4eea Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Fri, 6 Nov 2026 13:25:24 +0900 Subject: [PATCH] Process database.yml with ERB in a separate process in Gemfile (#41749). Requiring 'erb' in the Gemfile activates the newest installed erb gem, which may conflict with the version locked in Gemfile.lock. Run ERB in a child Ruby process instead, so that the Gemfile gets the same result as before without loading erb. Co-Authored-By: Claude Opus 5.5 --- Gemfile | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index acb42a470..aebf2327a 100644 --- a/Gemfile +++ b/Gemfile @@ -59,13 +59,21 @@ database_file = File.join(File.dirname(__FILE__), "config/database.yml") if File.exist?(database_file) database_config = File.read(database_file) - # Requiring libraries in a Gemfile may cause Bundler warnings or - # unexpected behavior, especially if multiple gem versions are available. - # So, process database.yml through ERB only if it contains ERB syntax - # in the adapter setting. See https://www.redmine.org/issues/41749. + # Process database.yml with ERB in a separate Ruby process, because + # requiring erb here may activate a version that conflicts with + # Gemfile.lock (#41749). if database_config.match?(/^ *adapter: *<%=/) - require 'erb' - database_config = ERB.new(database_config).result + # Send output from the ERB code to stderr to keep it out of the result + erb_script = <<~RUBY + $stdout = STDERR + STDOUT.write(ERB.new(File.read(ARGV.fetch(0))).result) + RUBY + # Without the unbundled env, the child process would load this Gemfile + # again through RUBYOPT=-rbundler/setup + database_config = Bundler.with_unbundled_env do + IO.popen([RbConfig.ruby, '-rerb', '-e', erb_script, database_file], &:read) + end + raise "Could not evaluate ERB in #{database_file}" unless $?.success? end adapters = database_config.scan(/^ *adapter: *(.*)/).flatten.uniq -- 2.55.0