Shopify/packwerk

Mounting an engine raises violations

Spence1115 opened this issue · 2 comments

Description
When I mount an engine in the main app for the purposes of accessing the API, I receive violations from packwerk check

Privacy violation: '::SomeService::Engine' is private to 'modules/some_service' but referenced from '.'.
Is there a public entrypoint in 'modules/some_service/public/' that you can use instead?

Am I missing something in terms of how we should be accessing our engines?

To Reproduce

# ~/config/routes.rb

Rails.application.routes.draw do
  mount SomeService::Engine, at: "/some_service"
end
# ~/package.yml

enforce_dependencies: true
enforce_privacy: false
dependencies:
- "modules/some_service"

Hi @Spence1115 !

So packwerk doesn't know about engines or the preferred way to mounting them out of the box. In this case, packwerk is considering this a privacy violation because modules/some_service/package.yml has set enforce_privacy to true, and ::SomeService::Engine is not in the public folder of modules/some_service.

In this case, it seems reasonable that SomeService::Engine is the public API to using your engine and connecting it to Rails. I'm not sure if there's a Rails limitation here (let me know if there is), but you should be able to move ::SomeService::Engine to live in modules/some_service/public so it is considered public API. Your other options are:

  • Turn off enforce_privacy
  • Use the other API to enforce_privacy to specify private constants
  • Have packwerk ignore config/routes by setting an exclusion in packwerk.yml.

Let me know if this doesn't help!

Packwerk should never ask people to move Rails defaults to other places. That is the main reason why enforce_privacy is being removed from Packwerk. Don't move your engine to modules/some_service/public. Packwerk should not be forcing you to do that.

So the other three options Alex listed are your best ones.

We need a better mechanism than folder collocation to define what is public API and what is isn't. Unfortunately the solution is Packwerk isn't good enough and it is going to be removed. Maybe the upcomin privacy plugin will build a different solution.

Thank you for the issue.