Declare a specific constant as a Dependency
Closed this issue · 3 comments
Description
I am relatively new to Packwerk and working on building out different modularized portions of our application. As I am slowly chipping away at the backlog of packages needed, there are a lot of dependencies from the original structure of our application.
For example, I am building out a package that references a specific Model in the original app/models
location.
Package:
- File path:
app/packages/home_operations
- Specific class in question:
app/packages/home_operations/services/cleaning.rb
Constant:
- File path: 'app/models/job.rb`
- Constant:
::Job
Expected Behaviour
Instead of declaring the entire package or an entire directory, I'd like to be able to simply say ::Job
is a dependency, creating a much more concise dependency declaration.
Version Information
- Packwerk: 2.1.1
- Ruby: 3.0
- Rails: 6.1.5
Additional Context
When running packwerk update-deprecations
, this is the output added to my deprecated-references.yml
:
".":
"::Job":
violations:
- dependency
files:
- app/packages/home_operations/services/cleaning.rb
I don't see anything in the documentation\USAGE.md that would seem to point to the expected formatting of the dependencies
entry in package.yml
.
Any guidance would be greatly appreciated!
Packwerk doesn't support dependencies between anything other than packages. However, packages can be very small to the point where a package contains just one file.
You could create a package that only contains ::Job
.
In our work we've found that we rarely want to be this specific. Most of the time there is a chuck of behavior that a package should be allowed to depend on, and there's no reason why that behavior should be implemented in just one file.
Note that you can also create a package that contains all your job logic, and then use package privacy to only expose the one constant that you think should be reused.
@exterm thanks for the reply! I figured as much but thought I'd at least reach out and see if I was missing something. I appreciate the suggestions.
@exterm I think it'd still be useful to make this explicit in the readme. Or even better, show an error when running the check. The format isn't specified anywhere and there's no error if you make a typo in the component name. Or if you try to add dependencies the wrong way. I ended up in this issue because I was trying to list specific files/folders/constants as dependencies. It isn't that obvious for a first time reader
Update: Of course 5 minutes after writing I noticed that validate
notifies you about that 😅. I would still place this error within check
and write it in the readme though 🙂
Thanks in any case!