Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ jobs:
- name: Exercise opt-in Rails import checks
run: bundle exec ruby scripts/check_rails_imports.rb

- name: Exercise opt-in Rails association checks
run: bundle exec ruby scripts/check_rails_associations.rb

- name: Exercise read-only source exclusions
run: bundle exec ruby scripts/check_source_exclusions.rb

Expand Down Expand Up @@ -171,6 +174,7 @@ jobs:
ruby scripts/check_namespace_receivers.rb
ruby scripts/check_static_paths.rb
ruby scripts/check_rails_imports.rb
ruby scripts/check_rails_associations.rb
ruby scripts/check_source_exclusions.rb
ruby scripts/check_analysis_diagnostics.rb
ruby scripts/check_baselines.rb
Expand Down
12 changes: 12 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,18 @@ and unknown operands remain unresolved. No Rails boot or automatic autoload-root
The option is immutable, validated and part of graph-cache identity; it composes with either
dependency-analysis mode. Cross-file method replacement and dynamic Rails/glob values are not modeled.

### Explicit Rails associations (unreleased)

`CheckOptions.new(rails_associations: true)` independently enables dependencies from supported
Rails model calls such as `belongs_to :store, class_name: '::Storage::Store'`. It can be combined
with either analysis mode and with `rails_imports`; neither previously shipped option enables it.
A unique known class declaration contributes a `:rails_association` edge with call-site and
definition lines. It shares an edge with any import or constant reference to the same file.

Use `--rails-associations` or `--no-rails-associations` in the CLI. Enabled scopes have separate
baseline identities and report candidate, unresolved and ambiguous counts; disabled output and
fingerprints stay unchanged. See [supported syntax and limitations](GUIDE.md#rails-associations).

For autoloaded projects, opt into known project constant references alongside imports:

~~~ruby
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.1.1 (unreleased)

- Add opt-in static Rails association dependencies for explicit literal `class_name` on supported
model-body macros, with evidence, isolated baselines and CLI uncertainty counts. No Rails boot,
automatic inflection, or change to existing default graphs.
- Lazily reuse immutable node/internal/external-edge views within each cached graph analysis
across file and layer checks. Invalidation, per-rule filtering, callbacks and logging stay intact;
views trade additional retained memory for less repeated projection work.
Expand Down
41 changes: 40 additions & 1 deletion GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,45 @@ the helper explicitly. Rails itself documents
[`require_dependency` as obsolete for modern autoloading](https://api.rubyonrails.org/classes/ActiveSupport/Dependencies/RequireDependency.html).
This compatibility option models existing legacy/engine code, not complete Rails runtime behavior.

<a id="rails-associations"></a>

### Explicit Rails association class names (unreleased, opt-in)

An association may name a model in a string, with no import or ordinary constant reference:

~~~ruby
class Order < ApplicationRecord
belongs_to :store, class_name: '::Storage::Store'
end
~~~

Use `ArchUnit::CheckOptions.new(rails_associations: true)` or
`bundle exec archunit check --rails-associations`. The option is separate from both
`rails_imports` and `dependency_analysis`. It uses the same included-source declaration index
as expanded mode, so it adds cold analysis work even if ordinary constant reads remain off.

The supported sites are bare or `self.` calls directly in a named class body whose written
superclass is `ApplicationRecord` or `ActiveRecord::Base` (optionally root-qualified).
Supported macros are `belongs_to`, `has_one`, `has_many` and `has_and_belongs_to_many`.
Use a symbol association name and a literal quoted string or symbol as `class_name:`, with
an optional literal lambda scope. Names use ASCII constant path segments. A string hash key such
as `"class_name" => ...` is not a Rails symbol option. Dynamic values, keyword splats, duplicate
keys, polymorphic targets,
`as:`, `through:`, and names that would need inflection are unresolved. Indirect calls, custom
macros, concerns, alternate receivers, and custom base-class chains are outside the scanned sites.

With a leading `::`, lookup starts at root. Otherwise, names are tried under the model's full
name, then enclosing namespaces, then root (with Rails' root-first exception when the supplied
name equals the model's basename). This is a bounded approximation of [Active Record lookup](https://github.com/rails/rails/blob/v8.1.0/activerecord/lib/active_record/inheritance.rb#L242-L269):
the first known candidate must have a class declaration in one included file. Ambiguous and
non-class candidates are not guessed past. No Rails boot, class ancestry checks, autoload mapping,
runtime aliases, overridden macro behavior, or custom inflection are evaluated.

The CLI reports `rails_association_sites`, `unresolved_rails_associations`, and
`ambiguous_rails_associations` in enabled analysis scopes. They count only the supported call
sites encountered and do not measure semantic coverage. An import and an association can share a
file edge and retain separate evidence. Legacy checks and baselines are unchanged when disabled.

### Constant References (Opt-in)

For autoloaded applications, enable dependencies from known project constants alongside imports:
Expand Down Expand Up @@ -706,7 +745,7 @@ Overlapping observed membership raises UserError instead of choosing a layer sil
patterns can be overridden with `PresetOptions.folders` (String or Array of source globs per layer).
Unknown/duplicate keys and empty patterns are errors. For arbitrary policies use `project_layers`.
The Rails preset is opinionated and expects a services layer; it is not full Rails/DDD verification
and does not automatically enable `rails_imports` or constant analysis.
and does not automatically enable `rails_imports`, constant analysis, or Rails association analysis.
For a Zeitwerk app, pass expanded analysis at check time:

~~~ruby
Expand Down
21 changes: 21 additions & 0 deletions benchmark/study/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ medians are 150,124 → 148,852 KiB. Even graph-only RSS varies 118,360 → 128,
identical retained Ruby heap; do not attribute every high-water change to the indexes. Windows RSS
remains unavailable/null. There is no memory-reduction claim.

## Explicit Rails association opt-in cost (#102)

Run `bundle exec ruby benchmark/study/rails_association_cost.rb pkg/association-cost.json` from the
library root. The study reuses the synthetic fixture generator and measurement helpers, replacing
the API files with models containing explicit `class_name:` strings. It alternates five pairs
of imports-only checks with the feature disabled/enabled across the same 2,000 files. It verifies
zero cross-file edges off and exactly 1,000 expected, evidenced association edges on, and checks
snapshot reuse in both modes. No target source, dependencies, or Rails boot are executed.

[Raw Windows samples](rails-association-windows.json), Ruby 4.0.6, five-trial medians:

| Opt-in | Cold graph | Cold allocated objects | Warm graph lookup |
| --- | ---: | ---: | ---: |
| Off | 1.614 s | 629,466 | 3.42 ms |
| On | 1.860 s | 926,503 | 2.64 ms |

In this fixture the opt-in adds about 0.25 s cold and 0.50 MiB of post-GC Ruby heap growth; the
two graphs have different dependency coverage. Warm samples are small lookup timings, not evidence
that enabling the option is faster. These local samples are not performance guarantees, and
there is no machine-speed gate. Windows peak RSS is unavailable.

## Shared-policy study

`run.rb` generates Rails-like and four-engine layouts with 200 Ruby files by default. Density
Expand Down
210 changes: 210 additions & 0 deletions benchmark/study/rails-association-windows.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
{
"runtime": {
"ruby": "4.0.6",
"platform": "x64-mingw-ucrt"
},
"pairs": 1000,
"trials": 5,
"samples": [
[
{
"enabled": false,
"cold": {
"seconds": 1.9492177999927662,
"allocated_objects": 629898
},
"warm": {
"seconds": 0.0034218000364489853,
"allocated_objects": 237
},
"before": {
"ruby_heap_bytes": 3807800,
"peak_rss_kb": null
},
"retained": {
"ruby_heap_bytes": 4459205,
"peak_rss_kb": null
}
},
{
"enabled": true,
"cold": {
"seconds": 2.0290882000117563,
"allocated_objects": 926704
},
"warm": {
"seconds": 0.004275399958714843,
"allocated_objects": 237
},
"before": {
"ruby_heap_bytes": 4457189,
"peak_rss_kb": null
},
"retained": {
"ruby_heap_bytes": 4984061,
"peak_rss_kb": null
}
}
],
[
{
"enabled": true,
"cold": {
"seconds": 1.5120650000171736,
"allocated_objects": 926502
},
"warm": {
"seconds": 0.002165399957448244,
"allocated_objects": 237
},
"before": {
"ruby_heap_bytes": 4993781,
"peak_rss_kb": null
},
"retained": {
"ruby_heap_bytes": 4986117,
"peak_rss_kb": null
}
},
{
"enabled": false,
"cold": {
"seconds": 1.6137248000013642,
"allocated_objects": 629464
},
"warm": {
"seconds": 0.0024252000148408115,
"allocated_objects": 237
},
"before": {
"ruby_heap_bytes": 4994581,
"peak_rss_kb": null
},
"retained": {
"ruby_heap_bytes": 4465573,
"peak_rss_kb": null
}
}
],
[
{
"enabled": false,
"cold": {
"seconds": 1.5577825999935158,
"allocated_objects": 629464
},
"warm": {
"seconds": 0.0022588999709114432,
"allocated_objects": 237
},
"before": {
"ruby_heap_bytes": 4463621,
"peak_rss_kb": null
},
"retained": {
"ruby_heap_bytes": 4466413,
"peak_rss_kb": null
}
},
{
"enabled": true,
"cold": {
"seconds": 1.8740935000241734,
"allocated_objects": 926503
},
"warm": {
"seconds": 0.002644499996677041,
"allocated_objects": 237
},
"before": {
"ruby_heap_bytes": 4464421,
"peak_rss_kb": null
},
"retained": {
"ruby_heap_bytes": 4987597,
"peak_rss_kb": null
}
}
],
[
{
"enabled": true,
"cold": {
"seconds": 1.8599448000313714,
"allocated_objects": 926503
},
"warm": {
"seconds": 0.004911200026981533,
"allocated_objects": 237
},
"before": {
"ruby_heap_bytes": 4997061,
"peak_rss_kb": null
},
"retained": {
"ruby_heap_bytes": 4988437,
"peak_rss_kb": null
}
},
{
"enabled": false,
"cold": {
"seconds": 1.6385640999651514,
"allocated_objects": 629466
},
"warm": {
"seconds": 0.004657200013753027,
"allocated_objects": 237
},
"before": {
"ruby_heap_bytes": 4997861,
"peak_rss_kb": null
},
"retained": {
"ruby_heap_bytes": 4468853,
"peak_rss_kb": null
}
}
],
[
{
"enabled": false,
"cold": {
"seconds": 1.3632072999607772,
"allocated_objects": 629466
},
"warm": {
"seconds": 0.00357220001751557,
"allocated_objects": 237
},
"before": {
"ruby_heap_bytes": 4466901,
"peak_rss_kb": null
},
"retained": {
"ruby_heap_bytes": 4469693,
"peak_rss_kb": null
}
},
{
"enabled": true,
"cold": {
"seconds": 1.2875181999988854,
"allocated_objects": 926504
},
"warm": {
"seconds": 0.0021222999785095453,
"allocated_objects": 237
},
"before": {
"ruby_heap_bytes": 4467701,
"peak_rss_kb": null
},
"retained": {
"ruby_heap_bytes": 4990877,
"peak_rss_kb": null
}
}
]
]
}
Loading