Shopify_app: API only compatibility

Created on 21 Jun 2018  ·  5Comments  ·  Source: Shopify/shopify_app

We have a rails application on API only mode and this GEM crashes the server on start (see trace bellow).

The problem comes from engine.rb. It tries to set some assets precompile path and therefore assumes the application has an asset pipeline. In API only rails does not enable the assets pipeline and we wish not to as well.

I'm not sure what to propose to solve this as this is loaded from Bundler.require(*Rails.groups) in my application.rb and the instruction config.api_only = true is loaded within my Rails::Application class.

# config/application.rb
require_relative 'boot'

require "rails"
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
# require "sprockets/railtie"
require "rails/test_unit/railtie"


Bundler.require(*Rails.groups)

module SayduckApiRails
  class Application < Rails::Application
    config.load_defaults 5.2
    config.api_only = true
    config.middleware.use Rack::Attack
    config.middleware.insert_after(ActiveRecord::Migration::CheckPending, ActionDispatch::Cookies)
    config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore)

    config.paperclip_defaults = {
      storage: :s3,
      s3_credentials: {
        bucket: ENV.fetch('AWS_S3_BUCKET_NAME'),
        access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
        secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
        s3_region: ENV.fetch('AWS_REGION'),
      },
      s3_host_name: "s3.#{ENV.fetch('AWS_REGION')}.amazonaws.com",
    }
  end
end
# shopify_app/lib/shopify_app/engine.rb

module ShopifyApp
  class Engine < Rails::Engine
    engine_name 'shopify_app'
    isolate_namespace ShopifyApp

      initializer "shopify_app.assets.precompile" do |app|
        app.config.assets.precompile += %w( shopify_app/redirect.js )
      end
  end
end
=> Booting Puma
=> Rails 5.2.0 application starting in development 
=> Run `rails server -h` for more startup options
Exiting
Traceback (most recent call last):
    57: from bin/rails:4:in `<main>'
    56: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `require'
    55: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency'
    54: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require'
    53: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
    52: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
    51: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
    50: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
    49: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
    48: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/commands.rb:18:in `<main>'
    47: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/command.rb:46:in `invoke'
    46: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/command/base.rb:65:in `perform'
    45: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
    44: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
    43: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
    42: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:142:in `perform'
    41: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:142:in `tap'
    40: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:147:in `block in perform'
    39: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:51:in `start'
    38: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:89:in `log_to_stdout'
    37: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/rack-2.0.5/lib/rack/server.rb:354:in `wrapped_app'
    36: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:27:in `app'
    35: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/rack-2.0.5/lib/rack/server.rb:219:in `app'
    34: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/rack-2.0.5/lib/rack/server.rb:319:in `build_app_and_options_from_config'
    33: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/rack-2.0.5/lib/rack/builder.rb:40:in `parse_file'
    32: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/rack-2.0.5/lib/rack/builder.rb:49:in `new_from_string'
    31: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/rack-2.0.5/lib/rack/builder.rb:49:in `eval'
    30: from config.ru:in `<main>'
    29: from config.ru:in `new'
    28: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/rack-2.0.5/lib/rack/builder.rb:55:in `initialize'
    27: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/rack-2.0.5/lib/rack/builder.rb:55:in `instance_eval'
    26: from config.ru:3:in `block in <main>'
    25: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:44:in `require_relative'
    24: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `require'
    23: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency'
    22: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require'
    21: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
    20: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
    19: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
    18: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
    17: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
    16: from /Users/daniel/GitHub/sayduck-api-rails/config/environment.rb:5:in `<main>'
    15: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/application.rb:361:in `initialize!'
    14: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/initializable.rb:60:in `run_initializers'
    13: from /Users/daniel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:205:in `tsort_each'
    12: from /Users/daniel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:226:in `tsort_each'
    11: from /Users/daniel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `each_strongly_connected_component'
    10: from /Users/daniel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `call'
     9: from /Users/daniel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `each'
     8: from /Users/daniel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:349:in `block in each_strongly_connected_component'
     7: from /Users/daniel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:431:in `each_strongly_connected_component_from'
     6: from /Users/daniel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
     5: from /Users/daniel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:228:in `block in tsort_each'
     4: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/initializable.rb:61:in `block in run_initializers'
     3: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/initializable.rb:32:in `run'
     2: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/initializable.rb:32:in `instance_exec'
     1: from /Users/daniel/.rvm/gems/ruby-2.5.1/gems/shopify_app-8.2.6/lib/shopify_app/engine.rb:7:in `block in <class:Engine>'
/Users/daniel/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/railtie/configuration.rb:97:in `method_missing': undefined method `assets' for #<Rails::Application::Configuration:0x00007f93633678d0> (NoMethodError)
Did you mean?  asset_host

Most helpful comment

This is not a Rails API issue. Rails has a --skip-sprockets option. I imagine with webpacker more and more folks are using this. I know I am.

Instead of letting this bug remain and confuse folks with the NoMethodError (see #591 too) why not just do this:

require "sprockets/railtie" unless defined? Sprockets

Or this:

unless app.config.respond_to?(:assets)
  raise 'ShopifyApp requires Sprockets. You must add the folowing to your application.rb file: require "sprockets/railtie"'
end

All 5 comments

I am running into the same issue

Hi, this gem is intended to support a more full-featured rails app including assets and view rendering. If you're looking at building an API-only app look at using the shopify_api gem directly without shopify_app.

This is not a Rails API issue. Rails has a --skip-sprockets option. I imagine with webpacker more and more folks are using this. I know I am.

Instead of letting this bug remain and confuse folks with the NoMethodError (see #591 too) why not just do this:

require "sprockets/railtie" unless defined? Sprockets

Or this:

unless app.config.respond_to?(:assets)
  raise 'ShopifyApp requires Sprockets. You must add the folowing to your application.rb file: require "sprockets/railtie"'
end

Hey what do you know, I generated a new Shopify app, got the same confusing error, and ended up here again! 🎉🤣

2020 back for more!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robbyklein picture robbyklein  ·  4Comments

MisinformedDNA picture MisinformedDNA  ·  6Comments

charlesemarsh picture charlesemarsh  ·  6Comments

skillmatic-co picture skillmatic-co  ·  6Comments

bhavya-hulkapps picture bhavya-hulkapps  ·  5Comments