Browse Source

Fixes: 4045 - Error when accessing ARGV in Rails 6.1.

Martin Gruner 2 years ago
parent
commit
643f2bc4ce

+ 0 - 5
bin/rails

@@ -7,9 +7,4 @@ end
 APP_PATH = File.expand_path('../config/application', __dir__)
 require_relative '../config/boot'
 
-# Unfortunately, Rails removes all Rake commands from ARGV, so remember it now.
-if ARGV.include? 'assets:precompile'
-  ENV['IN_ASSETS_PRECOMPILE'] = 'true'
-end
-
 require 'rails/commands'

+ 2 - 2
config/application.rb

@@ -14,7 +14,7 @@ Bundler.require(*Rails.groups)
 
 # Only load gems for asset compilation if they are needed to avoid
 #   having unneeded runtime dependencies like NodeJS.
-if ENV['IN_ASSETS_PRECOMPILE'] || ARGV.include?('assets:precompile') || Rails.groups.exclude?('production')
+if ArgvHelper.argv.include?('assets:precompile') || Rails.groups.exclude?('production')
   Bundler.load.current_dependencies.select do |dep|
     require dep.name if dep.groups.include?(:assets)
   end
@@ -47,7 +47,7 @@ module Zammad
 
     # zeitwerk:check will only check preloaded paths. To make sure that also lib/ gets validated,
     #   add it to the eager_load_paths only if zeitwerk:check is running.
-    config.eager_load_paths += %W[#{config.root}/lib] if ARGV[0].eql? 'zeitwerk:check'
+    config.eager_load_paths += %W[#{config.root}/lib] if ArgvHelper.argv[0].eql? 'zeitwerk:check'
 
     config.active_job.queue_adapter = :delayed_job
 

+ 3 - 0
config/boot.rb

@@ -2,5 +2,8 @@
 
 ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
 
+# Unfortunately, Rails empties ARGV when executing commands, so remember it now.
+require_relative '../lib/argv_helper'
+
 require 'bundler/setup' # Set up gems listed in the Gemfile.
 require 'bootsnap/setup' # Speed up boot time by caching expensive operations.

+ 12 - 0
lib/argv_helper.rb

@@ -0,0 +1,12 @@
+# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
+
+# Unfortunately, Rails clears ARGV when executing commands, so copy it early at startup
+#   for later usage.
+# See also https://github.com/rails/rails/commit/8ec7a2b7aaa31527686b05e0640d125299933782.
+module ArgvHelper
+  ORIGINAL_ARGV = ARGV.dup.freeze
+
+  def self.argv
+    ORIGINAL_ARGV
+  end
+end

+ 1 - 1
lib/tasks/zammad/ci/bundle/orphaned.rb

@@ -31,7 +31,7 @@ module Tasks
           end
 
           def self.validate_age
-            age = ARGV[1]
+            age = ArgvHelper.argv[1]
             if age.to_i.to_s != age
               abort "Please provide a valid number for 'age_in_years'.\n#{usage}"
             end

+ 3 - 2
lib/tasks/zammad/command.rb

@@ -40,11 +40,12 @@ module Tasks
       #  self-modification in 'zammad:package:install').
       # Enforce the correct number of expected arguments.
       def self.validate_comandline
-        if ARGV.first.to_sym != task_name || ARGV.count != (const_get(:ARGUMENT_COUNT) + 1)
+        args = ArgvHelper.argv
+        if args.first.to_sym != task_name || args.count != (const_get(:ARGUMENT_COUNT) + 1)
           abort "Error: wrong number of arguments given.\n#{usage}"
         end
         # Rake will try to run additional arguments as tasks, so make sure nothing happens for these.
-        ARGV[1..].each { |a| Rake::Task.define_task(a.to_sym => :environment) do; end } # rubocop:disable Style/BlockDelimiters
+        args[1..].each { |a| Rake::Task.define_task(a.to_sym => :environment) do; end } # rubocop:disable Style/BlockDelimiters
       end
     end
   end

+ 1 - 1
lib/tasks/zammad/package/install.rb

@@ -14,7 +14,7 @@ module Tasks
         ARGUMENT_COUNT = 1
 
         def self.task_handler
-          filename = ARGV[1]
+          filename = ArgvHelper.argv[1]
           if filename.blank?
             abort "Error: Please provide a valid filename: #{usage}"
           end

+ 2 - 2
lib/tasks/zammad/package/uninstall.rb

@@ -14,9 +14,9 @@ module Tasks
         ARGUMENT_COUNT = 1
 
         def self.task_handler
-          name = ARGV[1]
+          name = ArgvHelper.argv[1]
           if name.blank?
-            abort "Error: please provide a package name: #{ARGV[0]} MyPackage"
+            abort "Error: please provide a package name: #{ArgvHelper.argv[0]} MyPackage"
           end
           # Find the package so that we don't need to require the version from the command line.
           package = ::Package.find_by(name: name)