r/rails • • 15d ago

Open source Autoloading can remove a require without removing the architectural dependency

I created and maintain ArchUnitRuby. Writing assisted by AI.

One thing I wanted to avoid in an architecture test was confusing “no explicit require” with

“no dependency.” These are different spellings of a relationship:

require_relative '../retrieval/store'

Retrieval::Store.new

In an autoloaded application, the second spelling is ordinary Ruby. If your team has decided

that a controller or API component shouldn't know about that implementation, an import-only

rule isn't the complete policy you meant to write.

ArchUnitRuby 1.0 can combine file-load edges and known project constant references through

`dependency_analysis: :imports_and_constants`. That mode is opt-in. The resulting graph can

be checked from the normal Ruby test suite.

This isn't a claim to understand every Rails association, callback, reflection path or

metaprogrammed reference. Packwerk's package workflow and ArchSpec's component/convention

rules also serve useful needs; this approach is about a test-native graph policy.

[Tested release and source]

For a Rails boundary you actually maintain, would file-load edges and constant references

express enough of the policy, or is the important coupling somewhere else?

0 Upvotes

4 comments sorted by

2

u/CaptainKabob 15d ago

What's the problem you're trying to solve?

1

u/Ready_Reception8949 11d ago

yeah the post jumps straight into implementation without really explaining the actual pain point first

0

u/Tristan_Kruse 15d ago

Mainly to catch changes that still work but cross a boundary the team agreed on earlier when setting up the software architecture.
For example a controller should call a service, but someone adds Retrieval::Store.new directly.
Request specs might still pass, but the architecture test flags that reference.

The autoloading point is just that looking for require alone would miss it.
Here is an example.

1

u/jaypeejay 14d ago

We have a cop that detects exactly what you’re describing.