Skip to content

Commit

Permalink
Fixes: 4045 - Error when accessing ARGV in Rails 6.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgruner committed Apr 1, 2022
1 parent 6906752 commit 643f2bc
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 13 deletions.
5 changes: 0 additions & 5 deletions bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 2 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# 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
Expand Down Expand Up @@ -47,7 +47,7 @@ class Application < Rails::Application

# 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

Expand Down
3 changes: 3 additions & 0 deletions config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 12 additions & 0 deletions lib/argv_helper.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/tasks/zammad/ci/bundle/orphaned.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.task_handler
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
Expand Down
5 changes: 3 additions & 2 deletions lib/tasks/zammad/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ def self.run_task
# 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
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/zammad/package/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.usage
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
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/zammad/package/uninstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def self.usage
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)
Expand Down

0 comments on commit 643f2bc

Please sign in to comment.