How to List a Specific File as a Dependency in a Pack using Packwerk and Packs-Rails?

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

Message originally sent by slack user U71TN2WF04X

Hello :wave: I’ve got a question.
Is there a way to list a dependency to a specific file inside a pack? I’ve been giving workshops to my coworkers about packwerk and packs-rails, and this question is been repeated a few times.

The question comes when in a specific pack I list another as a dependency to use whatever is defined in there, but what if I only need a very specific model as a dependency and not the whole pack? I imagine a way of doing this would be to create a pack inside like packs/another_pack/app/models/my_model/ → becomes a pack and inside I put my_model.rb, but would it make sense to make this happen? Maybe someone here has tried a different approach :eyes:

You’re describing one possible approach. We have this in our codebase in a couple of places (for god models excusively)

You can also use the packwerk extension PrivacyChecker to create the concept of package code privacy, which allows you to say that in a given package you want certain packages to form the public API, but hide every other class. If a package now accepts the dependency and depends only on the file in the public API, you won’t see any violations. If, however, that package also depends on non-public code, you will get a new kind of violation: a privacy violation

https://github.com/rubyatscale/packwerk-extensions#privacy-checker

Message originally sent by slack user U71TN2WF04X

Excelent! I had seen the privacy-checker but forgot about it :sweat_smile:
Thank you stephan!

Message originally sent by slack user U70TIGAX94P

It should be noted that a very similar but slightly more convoluted thing can be achieved without the privacy checker - the public API of a package can be a separate package. Like „inventory“ and „inventory_private“. By convention, nobody would depend on the private thing (except „inventory“ itself).

Privacy-checker will be a lot more intuitive once it is set up - just saying, once again, all packaging problems can be solved by introducing more packages :blush:

Very true (it is packages all the way down…)!

And if you wanted to enforce the “By convention, nobody would depend on…” you could use another packwerk-extension: VisibilityChecker :dotted_line_face: