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

Add EngineApiBoundary cop #6

Merged
merged 3 commits into from
Dec 5, 2019
Merged

Add EngineApiBoundary cop #6

merged 3 commits into from
Dec 5, 2019

Conversation

maxh
Copy link

@maxh maxh commented Nov 19, 2019

This cop enforces modular isolation of Rails Engines. It allows engines to define an API surface in an internal api/ directory. Code outside the engine is only allowed to interact with the engine through that API surface. For more details, check out in the class comments in engine_api_boundary.rb.

Whereas Flexport/GlobalModelAccessFromEngine prevents an engine from reaching out into the global app/models, this cop prevents any code outside an engine from reaching in to access that engine.

A change in the main RuboCop repo is a prerequisite for this PR: "Allow cops to invalidate results cache" (rubocop/rubocop#7496).

@maxh maxh requested a review from ankurd1 November 19, 2019 15:06
@maxh maxh changed the base branch from maxh/initial-extension to master November 19, 2019 17:27
@maxh maxh added the new cop label Nov 19, 2019
lib/rubocop/cop/flexport/engine_api_boundary.rb Outdated Show resolved Hide resolved
@maxh maxh merged commit f2f7158 into master Dec 5, 2019
@maxh maxh deleted the maxh/engine-api-cop branch December 5, 2019 22:30
@tleish
Copy link

tleish commented Dec 5, 2019

Can I use this feature to prevent boundaries across multiple directories? For example, if a project included an engines directory (rails gems) and a gems directory (non-rails gems).

├── app
├── bin
├── config
├── engines <-- boundary
├── gems <-- boundary
├── lib
└── spec

As to why you may want to have both gems and engines seperated, see:
https://medium.com/@dan_manges/the-modular-monolith-rails-architecture-fb1023826fc4

Excited about this cop.

@maxh
Copy link
Author

maxh commented Dec 6, 2019

Can I use this feature to prevent boundaries across multiple directories?

Interesting. The directory tree of our codebase is similar, with top-level gems and engines directories. But we haven't seen a ton of boundary violation issues with gems -- only amongst engines. So our focus has been on engines.

This cop will prevent code in the gems from accessing code in engines. Indeed, it prevents any code outside the engine from accessing code in the engine.

It will not, however, prevent engines from directly accessing code in gems. Nor will it prevent either gems or engines from accessing "main app" models. To prevent engines from accessing "main app" models, you can use this cop: #5.

It may be possible to support both of these cops to support gems, or to create new gem-focused cops. Would you want to define an api/ surface to a gem?

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