Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't modify frozen autoload_paths in development environment #365

Open
ortegacmanuel opened this issue Jun 5, 2020 · 6 comments · May be fixed by #2182
Open

Can't modify frozen autoload_paths in development environment #365

ortegacmanuel opened this issue Jun 5, 2020 · 6 comments · May be fixed by #2182
Labels

Comments

@ortegacmanuel
Copy link

ortegacmanuel commented Jun 5, 2020

Steps to reproduce

  1. Add gem "view_component" to Gemfile
  2. Add require "view_component/engine" to config/application.rb
  3. Run bin/rails generate component Example title content

Expected behavior

  1. The generator creates all files/templates for the new new ViewComponent

Actual behavior

  1. The generator crashes with the following errror:

`<<': can't modify frozen #<Class:#Array:0x00007ff3e041bcf8> (FrozenError

/home/manuel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/change_observer.rb:10:in `<<': can't modify frozen #<Class:#<Array:0x00007ff3e041bcf8>> (FrozenError)
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/change_observer.rb:10:in `<<'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/view_component-2.7.0/lib/view_component/engine.rb:29:in `block in <class:Engine>'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/railties-6.0.2.1/lib/rails/initializable.rb:32:in `instance_exec'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/railties-6.0.2.1/lib/rails/initializable.rb:32:in `run'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/railties-6.0.2.1/lib/rails/initializable.rb:61:in `block in run_initializers'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:228:in `block in tsort_each'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:431:in `each_strongly_connected_component_from'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:349:in `block in each_strongly_connected_component'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `each'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `call'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:347:in `each_strongly_connected_component'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:226:in `tsort_each'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/tsort.rb:205:in `tsort_each'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/railties-6.0.2.1/lib/rails/initializable.rb:60:in `run_initializers'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/railties-6.0.2.1/lib/rails/application.rb:363:in `initialize!'
	from /home/manuel/kodo/main-street/mainstreet/config/environment.rb:5:in `<top (required)>'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/zeitwerk-2.2.2/lib/zeitwerk/kernel.rb:23:in `require'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:325:in `block in require'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:291:in `load_dependency'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:325:in `require'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/spring-2.1.0/lib/spring/application.rb:106:in `preload'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/spring-2.1.0/lib/spring/application.rb:157:in `serve'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/spring-2.1.0/lib/spring/application.rb:145:in `block in run'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/spring-2.1.0/lib/spring/application.rb:139:in `loop'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/spring-2.1.0/lib/spring/application.rb:139:in `run'
	from /home/manuel/.rvm/gems/ruby-2.5.1/gems/spring-2.1.0/lib/spring/application/boot.rb:19:in `<top (required)>'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /home/manuel/.rvm/rubies/ruby-2.5.1/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from -e:1:in `<main>'

System configuration

Rails version: Rails 6.0.2.1

Ruby version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

Gem version: view_component-2.7.0

   # config/application.rb
   require 'rails/all'
   require "view_component/engine"
   module MyApp
    class Application < Rails::Application
      ...
      config.load_defaults "6.0"
      ...
    end
  end
@joelhawksley
Copy link
Member

@ortegacmanuel thank you for the report and for the proposed fix! Would you be willing to provide a demo Rails application with this issue?

@vinistock @juanmanuelramallo as creators of the generator and previews (where this exception is being raised: lib/view_component/engine.rb:29), do you have any thoughts on the best resolution here?

As an aside, I'm wondering if this exception involving bootsnap is a sign that we should start testing for bootsnap compatibility in our test suite. Perhaps that's something you could look into @ortegacmanuel? Having our suite fail on an error like this would be ideal.

@juanmanuelramallo
Copy link
Contributor

It might be related to:
image
https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoload-paths

We are mutating it directly https://github.com/github/view_component/blob/master/lib/view_component/engine.rb#L29

@vinistock
Copy link
Contributor

I think it's worth trying out what @juanmanuelramallo pointed out. We should always prefer the public API anyways and it might be the underlying issue.

@stale
Copy link

stale bot commented Aug 29, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 29, 2020
@stale stale bot closed this as completed Sep 5, 2020
@tvdeyen
Copy link

tvdeyen commented Dec 16, 2024

This is still an issue, because you are still mutating the ActiveSupport::Dependencies.autoload_paths array.

paths_to_add = options.preview_paths - ActiveSupport::Dependencies.autoload_paths
ActiveSupport::Dependencies.autoload_paths.concat(paths_to_add) if paths_to_add.any?

This happens only if paths_to_add.any?, which only happens if other paths than the default test/components/previews are used (ie. spec/components/previews) or if you add component previews from an Rails engine.

This is fixed by using test/components/previews in a Rails application, but still this is an unresolved issue for engine authors or people using non-default preview_paths.

tvdeyen added a commit to tvdeyen/view_component that referenced this issue Dec 16, 2024
Setting `preview_paths` to anything else than `test/components/previews`
is broken, because we mutate `ActiveSupport::Dependencies.autoload_paths`.

From the Rails guides:

> Please do not mutate ActiveSupport::Dependencies.autoload_paths;
> the public interface to change autoload paths is config.autoload_paths.

Fixes ViewComponent#365
@tvdeyen tvdeyen linked a pull request Dec 16, 2024 that will close this issue
@tvdeyen
Copy link

tvdeyen commented Dec 16, 2024

Fixed by #2182

tvdeyen added a commit to tvdeyen/lookbook that referenced this issue Dec 16, 2024
Setting `preview_paths` to anything else than `test/components/previews`
is broken, because we mutate `ActiveSupport::Dependencies.autoload_paths`.

From the Rails guides:

> Please do not mutate ActiveSupport::Dependencies.autoload_paths;
> the public interface to change autoload paths is config.autoload_paths.

Refs: ViewComponent/view_component#365
@Spone Spone reopened this Dec 16, 2024
tvdeyen added a commit to tvdeyen/view_component that referenced this issue Dec 16, 2024
Setting `preview_paths` to anything else than `test/components/previews`
is broken, because we mutate `ActiveSupport::Dependencies.autoload_paths`.

From the Rails guides:

> Please do not mutate ActiveSupport::Dependencies.autoload_paths;
> the public interface to change autoload paths is config.autoload_paths.

Fixes ViewComponent#365
tvdeyen added a commit to tvdeyen/view_component that referenced this issue Dec 17, 2024
Setting `preview_paths` to anything else than `test/components/previews`
is broken, because we mutate `ActiveSupport::Dependencies.autoload_paths`.

From the Rails guides:

> Please do not mutate ActiveSupport::Dependencies.autoload_paths;
> the public interface to change autoload paths is config.autoload_paths.

Fixes ViewComponent#365
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants