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

Generators and tasks do not work in engines #138

Open
pbhogan opened this issue Jul 8, 2024 · 4 comments
Open

Generators and tasks do not work in engines #138

pbhogan opened this issue Jul 8, 2024 · 4 comments

Comments

@pbhogan
Copy link

pbhogan commented Jul 8, 2024

When using Stimulus in an engine, running the stimulus controller generator doesn't respect the engine namespace. So it'll generate in app/javascript/controllers instead of app/javascript/engine_name/controllers

Furthermore, the stimulus tasks are not available, so the automatic execution of stimulus:manifest:update by the generator fails.

I know this can all be done manually, but it's unintuitive that it doesn't work when other Rails generators operate correctly.

@dhh
Copy link
Member

dhh commented Aug 16, 2024

Feel free to explore a fix to this 👍

@pbhogan
Copy link
Author

pbhogan commented Aug 29, 2024

Small progress update here:

Fixing the Stimulus controller generator seems trivial and I'll submit a pull request for that.

However, more challenging is the getting Stimulus tasks to show up in the engine root without modifying the engine's Rakefile. Any tasks declared with Rails::Railtie.rake_tasks, Rails::Engine.rake_tasks or in lib/tasks will be scoped under namespace :app due to Rakefile loading rails/tasks/engine.rake in Railties, which then scopes under :app and loads tasks.

Critically, there doesn't appear to be any way to add unscoped tasks within a Rails::Engine.

I think the solution is making changes to Railties to have something like Rails::Railtie.unscoped_rake_tasks or Rails::Railtie.engine_rake_tasks (not sure on the best nomenclature here) that would allow an engine to declare tasks that will not appear in host apps, but can be run in the engine root during development.

engine.rake accomplishes this for database-related tasks by doing it manually for Rails, so there is a use-case, albeit niche. Any engine with tasks other than generators could use this.

In the case of Stimulus, we would want app:stimulus:manifest:update to exist for the dummy app, but also stimulus:manifest:update for development of the engine itself when run in the engine project root.

Thoughts? Should I pursue a more significant pull request on Railties to accomplish this @dhh ?

@dhh
Copy link
Member

dhh commented Aug 29, 2024

That's a good question. I don't have the answer, but maybe @rafaelfranca does.

@pbhogan
Copy link
Author

pbhogan commented Sep 3, 2024

I've made a pull request to address this. It's not tiny, but the more I got into it, the more related issues and edge-cases I found to fix and clean up. :)

The one drawback is I had to include a small monkeypatch to Rake's DSL to allow creating unscoped top-level tasks.

Incidentally, I discovered ActionCable also has a few similar issues with its generator that needs addressing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants