palkan/view_component-contrib

Rails 7.1 autoload_lib issue

Closed this issue ยท 6 comments

igas commented

What did you do?

Upgraded rails

What did you expect to happen?

No errors due to the gem.

What actually happened?

With new initializer:

config.autoload_lib(ignore: ["assets", "tasks"])

lib/generators not being ignored which causes errors.

Additional context

I'm happy to send a PR, curious how would you want to handle it? Add docs in readme or adjust generator?

Environment

Ruby Version: 3.2.2

Framework Version (Rails, whatever): 7.1.0.beta1

View Component Contrib Version: 0.1.4

palkan commented

I think, it should be fixed at the Rails side; lib/generators is pretty common.

@fxn Do you remember why you didn't included generators to the default ignore list? It's not clear from the PR (and previous PRs mentioned generators in multiple places).

fxn commented

Hey!

This was a tricky decision, indeed. I opted to generate the directories that the Rails application generator creates under lib.

There are common directories out there that could be ignored, like lib/templates, lib/generators, lib/middlewares, etc. You could always miss something in that list, and having all that generated by default without even existing seemed weird to me. Also, people could have legit namespaces in those subdirectories, you don't know in advance.

So, I opted for the subdirectories we know at generation time, and linked to the docs in a generated code comment just above the generated call.

I tend to lean also towards explicit APIs, so no hidden defaults.

Note that upgrading Rails does not get you config.autoload_lib(ignore: ["assets", "tasks"]). If the upgraded application has that line, it was written by someone, and you need to understand the call.

Open to improve this, however, after the user experience we see.

fxn commented

@palkan for view_component-contrib I see two possible ways to address this:

One option is to document that if the application has lib in the autoload paths (this could happen in the past), they should ignore the generated lib/generators. In Rails 7.1 you can document the config.autoload_lib call. Before, it is

Rails.autoloaders.main.ignore(...)

Another option is to execute the line above in an initializer of the railtie. Zeitwerk does not assume ignored files or directores exist or live under the managed directories. They can be duplicated too. However, the downside is this would be implicit, maybe too much, depending on personal thresholds :).

fxn commented

@igas did the application have lib in the autoload paths before?

palkan commented

I opted to generate the directories that the Rails application generator creates under lib.

Totally makes sense. Thanks for the explanation @fxn !

I think, in our case, we should:

  • Add a note to the documentation
  • Inform a user to update their autoload_lib configuration (if any present) at the end of the installation script
fxn commented

After this discussion, I believe a code comment that explains the ignore list on the spot in config/application.rb is going to be more clear than a pointer to the docs. (diff)