Troubleshooting `packs-rails` for Defining Routes within a Package

This message was imported from the Ruby/Rails Modularity Slack server. Find more info in the import thread.

Message originally sent by slack user U7374XD6T52

anyone here using packs-rails to define routes inside the package? I’m attempting to do what’s spelled out in the readme, but I’m getting a zeitwerk error because my routes file (packs/components/config/routes/components.rb) doesn’t define a Components constant

Hi <@U7374XD6T52>, we do and have no issues, but the structure is sligthly different.
We tread each component as a mini Rails app, meaning that we put routes in components/<name>/config/routes.rb

Please note we use components as the root folder instead of packs, but that shouldn’t matter

Ah, I see there’s now a draw helper, we don’t use that and we’ve written our own instead.
Still, the docs gives this as an example:

# packs/my_domain/config/routes/my_domain.rb
Rails.application.routes.draw do
  resources :my_resource
end

# config/routes.rb
Rails.application.routes.draw do
  draw(:my_domain)
end

So your path above seems slightly off, it should be something like (replace pack_name) packs/pack_name/config/routes/pack_name.rb

Then, you should add the following to your root config/routes.rb file (again, replace pack_name):

draw(:pack_name)

Message originally sent by slack user U7374XD6T52

hmm. that’s what I think I’m doing - my pack name is components (referring to hardware related to installing rooftop solar), so packs/components/config/routes/components.rb should be correct

(we are trying to move away from engines for our packs - we don’t want to maintain separate gemspecs everywhere if we can help it)

the only way I have been able to get zeitwerk to behave is to define an empty Components module above Rails.application.routes.draw do

Message originally sent by slack user U7374XD6T52

it feels like it should be a clue that this breaks the functional tests for the controller, but the app is functional (including the controller in this pack) without defining Components

mmmh that’s interesting, it may be due to some meta-programming magic packs-rails does internally.
If it helps, here is what we do in our root config/routes.rb instead of calling draw :

Dir[Rails.root.join('packs/*/config/routes.rb')].each do |file_path|
  instance_eval(File.read(file_path))
end

It doesn’t let you mount packs in specific places, but it does work fine with Zeitwerk.
Still worth opening an issue though, you’re following the documentation and it doesn’t work, so it may be a bug

In the packs/my_domain/config/routes/my_domain.rb you don’t need to call draw:

resources :my_resource

So the right approach is:

# packs/my_domain/config/routes/my_domain.rb
resources :my_resource

# config/routes.rb
Rails.application.routes.draw do
  draw(:my_domain)
end

Packwerk do not do any magic here, it just adds packs/*/config/routes to the list of directories where Rails will look for route parts (see https://guides.rubyonrails.org/routing.html#breaking-up-very-large-route-file-into-multiple-small-ones)

Message originally sent by slack user U7374XD6T52

thanks! removing the Rails.application.routes.draw block seems to have done the trick. I thought I tried that before, but I must have missed something

I’ll open an issue about the readme for packs-rails

Aaaah good spot @dmytro!
Wait does that mean the docs are incorrect then :face_with_raised_eyebrow:?

Apparently, they are not :shrug:

Opened an MR https://github.com/rubyatscale/packs-rails/pull/70/

Message originally sent by slack user U722X9GVQWA

Incorrect docs are my fault, sorry!