Skip to content

fix(extract): keep Scala file-scope val/var type references (#2054) - #4036

Closed
Faisal-Fayaz wants to merge 1 commit into
Graphify-Labs:v8from
Faisal-Fayaz:fix/scala-top-level-binding-refs
Closed

Faisal-Fayaz wants to merge 1 commit into
Graphify-Labs:v8from
Faisal-Fayaz:fix/scala-top-level-binding-refs

Conversation

@Faisal-Fayaz

Copy link
Copy Markdown
Contributor

Problem

Scala 3 drops the boilerplate wrapper: val/var can be written directly at file scope with no enclosing object/class/trait. The val_definition/var_definition type-reference dispatch was gated on and parent_class_nid, so a file-scope binding produced no node and no edge of any kind, while the byte-identical definition nested one level inside any container resolved correctly.

The missing container was the only difference. I verified this against tree-sitter-scala 0.26.0 before writing the fix — both positions parse to the same node type carrying the same type field:

class_definition
  template_body
    val_definition  fields=[(identifier, pattern), (type_identifier, type), (string, value)]
compilation_unit
  val_definition    fields=[(identifier, pattern), (type_identifier, type), (string, value)]

This is the Scala instance of a generic defect in the walker's default recursion path that had already caused and been fixed once for Python (#1050) — a position that legitimately has no container must fall back to the file node rather than be skipped.

Implementation

One anchor fallback in the existing dispatch, reusing the owner_nid = parent_class_nid or file_nid idiom already present in the property branch above:

  • drop and parent_class_nid from the gate
  • compute owner_nid = parent_class_nid or file_nid
  • emit the edge from owner_nid, and move the self-reference guard onto the same anchor so a file-scope binding cannot mint an edge from the file node to itself

Class-nested bindings are untouched; a regression test pins that case explicitly.

Scope

Edges only. No new node type, no config change, no skillgen output (_SCALA_CONFIG registers none of these node types — they are handled ad-hoc in the walker).

Verification

uv run pytest tests/test_scala_top_level_binding.py -q     # 8 passed
uv run pytest tests/ -q                                   # 28 failed, 6144 passed, 101 skipped
uv run ruff check .                                       # All checks passed!
uv run pyright                                            # 635 errors
  • 6 of the 8 new tests fail on the pre-fix tree. The 2 that pass either way are deliberate: the in-class control (must not regress) and the self-edge guard (trivially empty before the fix, meaningful after it).
  • The 28 failures are byte-identical to the clean-tree baseline (28 failed, 6136 passed), which I captured on an unmodified checkout and diffed test-id-by-test-id. They are missing tree-sitter grammars for erlang/R/solidity/vbnet plus ollama retry tests, and none touch Scala.
  • pyright is at parity: 635 errors before and after. That is pre-existing debt in this repo, not something this change introduces or claims to fix.

New tests cover: file-scope val and var, the in-class control, a file-scope type that resolves to a real declaration elsewhere, the no-self-edge guard, EXTRACTED confidence tagging, non-null source_location, and that a generic annotation keeps its generic_arg context distinct from a plain field one.

Limitations

  • A file-scope binding attributes its type reference to the file node, so several file-scope bindings of the same type produce parallel edges from that one node. This is the same shape the existing contains-from-file model already produces; collapsing them would need a node for the binding itself, which is out of scope here.
  • Node counts grow very slightly on corpora that use Scala 3 file-scope bindings. Expected and correct — those definitions were previously absent entirely.

🤖 Generated with Claude Code

…-Labs#2054)

Scala 3 allows `val`/`var` to be written directly at file scope with no
enclosing object/class/trait. The `val_definition`/`var_definition`
type-reference dispatch was gated on `and parent_class_nid`, so a file-scope
binding produced no node and no edge of any kind, while the byte-identical
definition nested one level inside any container resolved correctly. The
missing container was the only difference: both positions parse to the same
node type carrying the same `type` field.

This is the Scala instance of a generic defect in the walker's default
recursion path that had already caused and been fixed once for Python
(Graphify-Labs#1050) -- a position that legitimately has no container must fall back to
the file node rather than be skipped. The fix reuses the `owner_nid =
parent_class_nid or file_nid` idiom already present in the property branch
above, and moves the self-reference guard onto the same anchor so a
file-scope binding cannot mint an edge from the file node to itself.

Behaviour is unchanged for class-nested bindings; a regression test pins
that case explicitly.

Verified:
- uv run pytest tests/test_scala_top_level_binding.py -q  (8 passed)
- 6 of the 8 new tests fail on the pre-fix tree, 2 pass either way
  (the in-class control, and the self-edge guard which is trivially empty
  before the fix and meaningful after it)
- uv run pytest tests/ -q -> 28 failed, 6144 passed, 101 skipped; the 28
  failures are byte-identical to the clean-tree baseline (missing
  tree-sitter grammars for erlang/R/solidity/vbnet, plus ollama retry
  tests) and none touch Scala
- uv run ruff check . -> clean
- uv run pyright -> 635 errors, identical to the clean-tree baseline
  (pre-existing debt; this change adds none)

Co-Authored-By: Claude <[email protected]>
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown

Thanks for the pull request, @Faisal-Fayaz. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Looks safe to merge — no coupling regressions and no blocking issues, checked against the code graph (not a self-assessment).

Formal verification. PR-changed functions: 0/1 verified (0 proven, 0 may-equivalent, 0 distinguished) · 1 not verified (1 unsupported).

Not verified on this run: \_extract\_generic (unsupported).


Graphify review — findings

Fixes Scala 3 file-scope val/var bindings disappearing from the graph: their type references now fall back to the file node as owner instead of being skipped when there is no enclosing class. The self-reference guard follows the same owner, so a binding typed with the file's own stem doesn't create a file → file edge. In-class bindings keep their class-owned edges, and new tests cover the field vs generic_arg context, the EXTRACTED confidence tag, and per-line source locations.

No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 740 functions depend on the 260 functions this change touches.

Health — this change adds coupling hotspots:

  • new: _extract_generic() — 18 callers, 30 callees
  • new: extract_js() — 87 callers, 4 callees
  • new: extract_xaml() — 19 callers, 17 callees
  • new: extract_objc() — 27 callers, 9 callees
  • new: extract_julia() — 19 callers, 7 callees
  • new: extract_cpp() — 31 callers, 3 callees
  • new: extract_vue() — 10 callers, 7 callees
  • new: walk() — 1 callers, 67 callees
  • …and 9 more — each is listed as a finding

Verification — 740 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 675 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

26 of 319 test file(s) selected (8%) via static blast radius.

  • tests/test_astro_extraction.py — impact
  • tests/test_build.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_php_closures.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_indirect_call_block_scoped_shadow.py — impact
  • tests/test_indirect_dispatch.py — impact
  • tests/test_indirect_dispatch_assign_return.py — impact
  • tests/test_indirect_dispatch_getattr.py — impact
  • tests/test_js_exported_scalar_bindings.py — impact
  • tests/test_languages.py — impact
  • tests/test_multilang.py — impact
  • tests/test_python_underscore_resolution.py — impact
  • tests/test_rationale.py — impact
  • tests/test_ruby_resolution.py — impact
  • tests/test_scala_self_type.py — impact
  • tests/test_scala_top_level_binding.py — impact, changed-test
  • tests/test_swift_computed_properties.py — impact
  • tests/test_swift_protocol_requirements.py — impact
  • tests/test_trailing_newline_not_a_syntax_error.py — impact
  • tests/test_ts_new_expression_calls.py — impact
  • tests/test_typescript_module_extensions.py — impact
  • tests/test_vue_extraction.py — impact

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Docs that may be stale (advisory)

Formal verification

Could not verify: Could not verify \_extract\_generic.

The verifier did not have enough to check \_extract\_generic, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `config` is annotated `LanguageConfig` — outside the synthesizable primitive/collection set

· 17 more finding(s) on lines outside this diff (see the check run).

@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.75 (live on PyPI as graphifyy==0.9.75). Landed on v8 via an authorship-preserving cherry-pick, so your original commit authorship is kept. Thanks @Faisal-Fayaz for Scala file-scope val/var type references 🙏

Closing as shipped.

@safishamsi safishamsi closed this Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants