graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Ruby: constant-receiver singleton calls (Service.call, Model.where) never resolve — Rails codebases get near-zero cross-file edges

Open krishnateja7 opened this issue 4 months ago • 1 comments

Summary

Ruby singleton-style calls on a constant receiver — ClassName.method — never produce cross-file calls edges. This is the dominant idiom in Rails codebases (SettleService.call, Model.find_or_initialize_by, SomeJob.perform_async, SomeChannel.broadcast_*), and because Rails autoloading (Zeitwerk) means Ruby files carry no require/import statements either, a Rails app ends up with essentially no cross-file edges at all: classes become islands connected only by contains/method/inherits.

Environment

  • graphifyy 0.9.5 (uv tool install), Python 3.10, macOS arm64
  • Target repo: a mid-size Rails 8 API + React/TS monorepo (~2,000 extracted files → 11,077 nodes / 16,392 edges)

Repro (minimal)

# app/services/processor.rb
class Processor
  def self.call; end
end

# app/services/runner.rb
class Runner
  def run
    Processor.call   # no edge emitted
    Processor.new    # edge emitted (works)
  end
end

graphify update . then graphify path "Runner" "Processor" → path found only via the .new line; comment it out and there is no connection, even though Processor.call is right there and Processor + its .call() singleton method both exist as nodes.

Measured impact on a real Rails app

  • graphify affected "<service class>" --depth 2 → "No affected nodes found" for a service invoked from 10+ files (controllers, jobs, sibling services) — every call site is Namespace::Service.call(...).
  • graphify path between a Sidekiq job and the ActiveRecord model it writes → no path.
  • Meanwhile the TS side of the same monorepo is excellent (import edges): affected on a hook returned 80 dependents vs 85 by exhaustive grep.

Root cause

graphify/ruby_resolution.py (resolve_ruby_member_calls) intentionally resolves only two shapes:

  1. Const.new → edge to the class (callee == "new" is special-cased), and
  2. var.method where var's type was inferred from a local var = Const.new binding (receiver_type).

A member call with a capitalized (constant) receiver and any callee other than new falls through both branches, so Service.call / Model.where / Job.perform_async emit nothing — even when the receiver resolves to a unique class node and the callee matches an extracted singleton_method node.

Proposed fix (happy to PR)

Extend resolve_ruby_member_calls with a third shape, keeping your existing bail-on-ambiguity guard:

  • receiver is capitalized and callee != "new" → _unique_class(receiver);
    • if method_index[(class_nid, _key(callee))] exists → emit calls to that method node (covers def self.x singleton methods, which the extractor already indexes);
    • else → emit calls to the class node itself (covers inherited/dynamic class methods like ActiveRecord's where/find_by, where a class-level edge still gives correct blast-radius).
  • Optionally: carry the full scope_resolution text for namespaced receivers (Billing::Processor.call) so scoped receivers can resolve exactly even when the bare class name is ambiguous — bare ambiguous receivers keep bailing as today.

The class-node fallback matches the same conservative philosophy as the existing Const.new branch (single owning class or nothing), so it shouldn't reintroduce anything like #993's phantom edges.

If you're open to it I can put together a PR with tests along these lines.

krishnateja7 avatar Jul 03 '26 19:07 krishnateja7

Fixed in 13e2bdd (on v8, ships in 0.9.6). Your root-cause read of resolve_ruby_member_calls was exactly right, and the proposed fix is essentially what landed.

The resolver now handles a capitalized (constant) receiver with any callee:

  • binds to the class's owned singleton/instance method when present (def self.call — the extractor already indexes these), and
  • otherwise falls back to the class node itself, so inherited/dynamic class methods (ActiveRecord where/find_by, perform_async, etc.) still give correct blast-radius.

Namespaced receivers (Billing::Processor.call) resolve by the bare class name, and the single-owning-class god-node guard is kept throughout — an ambiguous receiver resolves to nothing rather than a wrong edge (verified with a two-Processor test), so no #993-style phantoms. Your Processor.call repro now produces the edge via the def self.call line, and Model.where links to the class node. Should make a real difference on Rails/Zeitwerk graphs.

safishamsi avatar Jul 04 '26 11:07 safishamsi