react-webpack-rails-tutorial icon indicating copy to clipboard operation
react-webpack-rails-tutorial copied to clipboard

Installation more portable and smooth. Closes #495

Open vtamara opened this issue 2 years ago • 3 comments

  • Installation more portable: tested on Ubuntu 21.10 and adJ/OpenBSD 7.1a1
  • Unnecessary or outdated gems removed: mini_racer, sass-rails
  • Gems and packages updated. Required gem after update added net-smtp
  • Using .env to manage user and password in environment variables (suggested by https://12factor.net/ )
  • Ready to run in recent platforms with more on less standard configuration
  • Different name for databases in development, tests and production
  • Documentation of installation udpated and reviewed, notes about specific platforms removed
  • In documentation removed almost all sections about non-existing Procfiles

This change is Reviewable

vtamara avatar Mar 24 '22 22:03 vtamara

Thanks for feedback

why are you removing mini_racer? Wouldn't we need something to replace it?

rails uses node.js. I see many people having trouble with mini_racer, for this example I had trouble with it in Ubuntu and adJ, see screenshots at https://github.com/vtamara/react-webpack-rails-tutorial/wiki/Using-on-adJ-(distribution-of-OpenBSD)-7.1a1 and https://github.com/vtamara/react-webpack-rails-tutorial/wiki/Using-on-Ubuntu-21.10.

I'm glad shakapacker doesn't use mini_racer neither pushes it in projects that use shakapacker. However react_on_rails is adding mini_racer to the Gemfile when you run bin/rails generate react_on_rails:install

I don't understand why you'd have these steps. I guess the other PR removes this.

If I dont' do these steps when I run it produces:

ERROR in ./node_modules/bootstrap-loader/loader.js 2:0-61                                                                                
Module not found: Error: Can't resolve './lib/bootstrap.loader' in ...

I thinks this happens because package.json is not using the npm package of bootstrap-loader (which would have the directory lib with the result of building) but it has a github repository that doesn't have the lib directory:

"bootstrap-loader": "judahmeek/bootstrap-loader#judahmeek/webpack-5",

If you accept #498, this will not be needed.

Would this work? and no need for dotenv-rails?

Yes it works without dotenv-rails, because extending ENV with fetch comes from rails.

The example you did would become:

host: <%= ENV.fetch("DB_HOST",  "localhost") %>
password: <%= ENV.fetch("DB_PASSWORD", nil) %>
port: <%= ENV.fetch("DB_PORT",  "5432") %>
username: <%= ENV.fetch("DB_USER", nil) %>

However leaving host in "" works fine in Ubuntu, and I guess it will work fine in MacOS.

I addressed the other comments by improving the PR with new commits.

Thank you.

vtamara avatar Mar 28 '22 15:03 vtamara

@vtamara are you 100% sure that ActiveSupport loads before database.yml?

Startup time in rails requires care in what can and can't be used.

justin808 avatar Mar 29 '22 08:03 justin808

Yes, not only because the patch I sent is working, but because I did the following experiment, with this repository:

  1. I cloned the rails repository to a local directory and switched to branch 6-1-stable (since this application is using '~> 6.1.4' in the Gemfile).
  2. I changed the Gemfile of this application replacing gem "rails", "~> 6.1.4" for gem "rails", path: "../tmp/rails" and run bundle
  3. In the sources of rails I looked where it was reading database.yml and found the function database_configuration, I set a breakpoint there in line https://github.com/rails/rails/blob/d38ff07a39c98f5db2b15ca3a51acee48c972205/railties/lib/rails/application/configuration.rb#L281
  4. I started the application and it stoped in my breakpoint, I did ENV.fetch('DB_USER') and it worked, besides checking the backtrace there it is:
--> #0  Rails::Application::Configuration.database_configuration at /home/vtamara/comp/rails/tmp/rails/railties/lib/rails/application/configuration.rb:282          #1  block (2 levels) in block (2 levels) in <class:Railtie> at /home/vtamara/comp/rails/tmp/rails/activerecord/lib/active_record/railtie.rb:221             
    ͱ-- #2  Module.class_eval(*args) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/lazy_load_hooks.rb:71                               
    #3  block in ActiveSupport::LazyLoadHooks.block in execute_hook(name#Symbol, base#Class, options#Hash, block#Proc) at /home/vtamara/comp/rails/tmp/rails/act
ivesupport/lib/active_support/lazy_load_hooks.rb:71                                                                                                             
    #4  ActiveSupport::LazyLoadHooks.with_execution_control(name#Symbol, block#Proc, once#NilClass) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/lazy_load_hooks.rb:61                                                                                                                                
    #5  ActiveSupport::LazyLoadHooks.execute_hook(name#Symbol, base#Class, options#Hash, block#Proc) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/act
ive_support/lazy_load_hooks.rb:66                                                                                                                               
    #6  block in ActiveSupport::LazyLoadHooks.block in run_load_hooks(name#Symbol, base#Class) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_su
pport/lazy_load_hooks.rb:52                                                                                                                                     
    ͱ-- #7  Array.each at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/lazy_load_hooks.rb:51                                                 #8  ActiveSupport::LazyLoadHooks.run_load_hooks(name#Symbol, base#Class) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/lazy_load_ho
oks.rb:51                                                                                                                                                       
    #9  <module:ActiveRecord> at /home/vtamara/comp/rails/tmp/rails/activerecord/lib/active_record/base.rb:315                                                      #10 <top (required)> at /home/vtamara/comp/rails/tmp/rails/activerecord/lib/active_record/base.rb:15                                                        
    ͱ-- #11 Kernel.require at /var/www/bundler/ruby/3.1/gems/zeitwerk-2.5.4/lib/zeitwerk/kernel.rb:35                                                           
    #12 Object.require(path#String) at /var/www/bundler/ruby/3.1/gems/zeitwerk-2.5.4/lib/zeitwerk/kernel.rb:35                                     
    #13 #<Class:ActiveRecord::QueryCache>.run at /home/vtamara/comp/rails/tmp/rails/activerecord/lib/active_record/query_cache.rb:31        
    #14 ActiveSupport::ExecutionWrapper::RunHook.before(target##<Class:0x000002906ff47ef8>) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/execution_wrapper.rb:28
    #15 block in ActiveSupport::Callbacks::CallTemplate.block in make_lambda at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/callbacks.rb
:427                                                                                                                                                                #16 block (2 levels) in #<Class:ActiveSupport::Callbacks::Filters::Before>.block (2 levels) in halting(callback_sequence#ActiveSupport::Callbacks::CallbackS
equence, user_callback#Proc, halted_lambda#Proc, filter#ActiveSupport::ExecutionWrapper::RunHook, name#Symbol) at /home/vtamara/comp/rails/tmp/rails/activesuppo
rt/lib/active_support/callbacks.rb:198                                                                                                                          
    #17 block (2 levels) in ActiveSupport::Callbacks::CallbackChain.block (2 levels) in default_terminator at /home/vtamara/comp/rails/tmp/rails/activesupport/l
ib/active_support/callbacks.rb:604                                                                                                                                  ͱ-- #18 Kernel.catch(*args) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/callbacks.rb:603                                         
    #19 block in ActiveSupport::Callbacks::CallbackChain.block in default_terminator at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/call
backs.rb:603                                                                                                                                                    
    #20 block in #<Class:ActiveSupport::Callbacks::Filters::Before>.block in halting(callback_sequence#ActiveSupport::Callbacks::CallbackSequence, user_callback
#Proc, halted_lambda#Proc, filter#ActiveSupport::ExecutionWrapper::RunHook, name#Symbol) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/
callbacks.rb:199                                                                                                                                                    #21 block in ActiveSupport::Callbacks::CallbackSequence.block in invoke_before(arg#ActiveSupport::Callbacks::Filters::Environment) at /home/vtamara/comp/rai
ls/tmp/rails/activesupport/lib/active_support/callbacks.rb:512                                                                                                  
    ͱ-- #22 Array.each at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/callbacks.rb:512                                                      #23 ActiveSupport::Callbacks::CallbackSequence.invoke_before(arg#ActiveSupport::Callbacks::Filters::Environment) at /home/vtamara/comp/rails/tmp/rails/activ
esupport/lib/active_support/callbacks.rb:512                                                                                                                    
    #24 ActiveSupport::Callbacks.run_callbacks(kind#Symbol) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/callbacks.rb:105
    #25 ActiveSupport::ExecutionWrapper.run! at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/execution_wrapper.rb:114
    #26 block in #<Class:ActiveSupport::ExecutionWrapper>.block in run!(reset#TrueClass) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/execution_wrapper.rb:77   
    #27 Kernel.tap at /home/vtamara/comp/rails/shaka/react-webpack-rails-tutorial/<internal:kernel>:90                                                          
    #28 #<Class:ActiveSupport::ExecutionWrapper>.run!(reset#TrueClass) at /home/vtamara/comp/rails/tmp/rails/activesupport/lib/active_support/execution_wrapper.rb:74                                                                                                                                                           
    #29 ActionDispatch::Executor.call(env#Hash) at /home/vtamara/comp/rails/tmp/rails/actionpack/lib/action_dispatch/middleware/executor.rb:12                  
    #30 ActionDispatch::Static.call(env#Hash) at /home/vtamara/comp/rails/tmp/rails/actionpack/lib/action_dispatch/middleware/static.rb:24                      
    #31 Rack::Sendfile.call(env#Hash) at /var/www/bundler/ruby/3.1/gems/rack-2.2.3/lib/rack/sendfile.rb:110                                                     
    #32 ActionDispatch::HostAuthorization.call(env#Hash) at /home/vtamara/comp/rails/tmp/rails/actionpack/lib/action_dispatch/middleware/host_authorization.rb:148                                                                                                                                                              
    #33 Webpacker::DevServerProxy.perform_request(env#Hash) at /var/www/bundler/ruby/3.1/gems/shakapacker-6.0.2/lib/webpacker/dev_server_proxy.rb:25            
    #34 Rack::Proxy.call(env#Hash) at /var/www/bundler/ruby/3.1/gems/rack-proxy-0.7.2/lib/rack/proxy.rb:67                                                      
    #35 Rails::Engine.call(env#Hash) at /home/vtamara/comp/rails/tmp/rails/railties/lib/rails/engine.rb:539                                                     
    #36 Puma::Configuration::ConfigMiddleware.call(env#Hash) at /var/www/bundler/ruby/3.1/gems/puma-5.6.2/lib/puma/configuration.rb:252                         
    #37 block in Puma::Request.block in handle_request(client#Puma::Client, lines#Puma::IOBuffer, requests#Integer) at /var/www/bundler/ruby/3.1/gems/puma-5.6.2/lib/puma/request.rb:77                                                                                                                                         
    #38 Puma::ThreadPool.with_force_shutdown at /var/www/bundler/ruby/3.1/gems/puma-5.6.2/lib/puma/thread_pool.rb:340                                           
    #39 Puma::Request.handle_request(client#Puma::Client, lines#Puma::IOBuffer, requests#Integer) at /var/www/bundler/ruby/3.1/gems/puma-5.6.2/lib/puma/request.rb:76                                                                                                                                                           
    #40 Puma::Server.process_client(client#Puma::Client, buffer#Puma::IOBuffer) at /var/www/bundler/ruby/3.1/gems/puma-5.6.2/lib/puma/server.rb:441             
    #41 block in Puma::ThreadPool.block in spawn_thread at /var/www/bundler/ruby/3.1/gems/puma-5.6.2/lib/puma/thread_pool.rb:147       

As you can see loading that file is done by using methods of ActiveSupport (It is interesting to notice that shakapacker is there very early in the initialization process in frame 33 with Webpacker::DevServerProxy.perform_request(env#Hash)).

vtamara avatar Mar 29 '22 16:03 vtamara

@ahangarha @Judahmeek anything useful here to extract or just close this one?

justin808 avatar Jan 10 '23 07:01 justin808

Looks like there's some npm package & engine versions that we should relax.

Other than that, I believe we've already addressed everything relevant.

Judahmeek avatar Feb 15 '23 04:02 Judahmeek