rails: ActiveStorage::Variation (v5.2.6.3)> .validate_arg_string > undefined method .any? for nil:NilClass

Steps to reproduce

ActiveStorage::Variation method .validate_arg_string raises an error : undefined method `any?’ for nil:NilClass

## ActiveStorage::Variation in activestorage (v5.2.6.3, rbenv 2.7.2) > models > active_storage > variation.rb

def validate_arg_string(argument)
    if UNSUPPORTED_IMAGE_PROCESSING_ARGUMENTS.any? { |bad_arg| argument.to_s.downcase.include?(bad_arg) }; raise UnsupportedImageProcessingArgument end
  end

This constant is defined at the top of the class :

## ActiveStorage::Variation in activestorage (v5.2.6.3, rbenv 2.7.2) > models > active_storage > variation.rb

UNSUPPORTED_IMAGE_PROCESSING_ARGUMENTS = ActiveStorage.unsupported_image_processing_arguments

ActiveStorage.unsupported_image_processing_arguments is not null when checking its value in console.

$ ActiveStorage.unsupported_image_processing_arguments
=> ["-debug", "-display", "-distribute-cache", "-help", "-path", "-print", "-set", "-verbose", "-version", "-write", "-write-mask"]

This one is defined in engine within the initializer

## ActiveStorage::Variation in activestorage (v5.2.6.3, rbenv 2.7.2) > models > active_storage > engine.rb
default_unsupported_image_processing_arguments = %w(
      -debug
      -display
      -distribute-cache
      -help
      -path
      -print
      -set
      -verbose
      -version
      -write
      -write-mask
    )

initializer "active_storage.configs" do
      config.after_initialize do |app|
        ActiveStorage.logger     = app.config.active_storage.logger || Rails.logger
        ActiveStorage.queue      = app.config.active_storage.queue
        ActiveStorage.previewers = app.config.active_storage.previewers || []
        ActiveStorage.analyzers  = app.config.active_storage.analyzers || []
        ActiveStorage.paths      = app.config.active_storage.paths || {}

        ActiveStorage.supported_image_processing_methods = app.config.active_storage.supported_image_processing_methods || []
        ActiveStorage.unsupported_image_processing_arguments = app.config.active_storage.unsupported_image_processing_arguments || default_unsupported_image_processing_arguments
        ActiveStorage.variable_content_types = app.config.active_storage.variable_content_types || []
        ActiveStorage.content_types_to_serve_as_binary = app.config.active_storage.content_types_to_serve_as_binary || []
        ActiveStorage.content_types_allowed_inline = app.config.active_storage.content_types_allowed_inline || []
        ActiveStorage.binary_content_type = app.config.active_storage.binary_content_type || "application/octet-stream"
      end
    end

Expected behavior

UNSUPPORTED_IMAGE_PROCESSING_ARGUMENTS should be loaded with the value of ActiveStorage.unsupported_image_processing_arguments, no ?

Actual behavior

UNSUPPORTED_IMAGE_PROCESSING_ARGUMENTS is nil which raises an error

System configuration

rails (5.2.6.3) activestorage (= 5.2.6.3) Ruby version: ruby ‘2.7.2’

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (8 by maintainers)

Commits related to this issue

Most upvoted comments

5.2.7 was released

@mawise thanks for the link to your code - using that I’ve worked out what the problem is now. In Rails 5.2 the UNSUPPORTED_IMAGE_PROCESSING_ARGUMENTS constant is defined in ActiveStorage::Variation which lives in app/models in the Active Storage engine. This gets eager loaded in production which happens before config.after_initialize hooks run so the ActiveStorage.unsupported_image_processing_arguments isn’t set at that point. In Rails 6.0, 6.1 and 7.0 the constant is in a different place that’s marked with autoload and isn’t eager loaded so when it’s eventually loaded the hooks have run and the constant is set correctly.

I’m running into what seems like the same issue. For what it’s worth it doesn’t present locally when using the local disk for storage but does present on deployment when using S3 for storage.

@roms182 are you able to try this branch in your application to see if the error is gone? https://github.com/rails/rails/compare/5-2-stable...rm-fix-5-2

@roms182 thanks for the info - I think must be a load order issue and trying to narrow down where that could be.

@pixeltrix yes I do require “rails/all” in config > application.rb

In the meanwhile, I copied the whole class ActiveStorage::Variation in a app > lib > active_storage > variation.rb file to override the .validate_arg_string() method and it fixed the bug. This is not a solution but I wanted to check if it would fix it or not.

before
def validate_arg_string(argument)
    if UNSUPPORTED_IMAGE_PROCESSING_ARGUMENTS.any? { |bad_arg| argument.to_s.downcase.include?(bad_arg) }; raise UnsupportedImageProcessingArgument end
  end

after
def validate_arg_string(argument)
    if ActiveStorage.unsupported_image_processing_arguments.any? { |bad_arg| argument.to_s.downcase.include?(bad_arg) }; raise UnsupportedImageProcessingArgument end
  end