codegraph icon indicating copy to clipboard operation
codegraph copied to clipboard

Swift properties (stored & computed) are never indexed — swift.ts uses nameField: 'name' but the name nests in a pattern

Open monochrome3694 opened this issue 5 months ago • 1 comments

Summary

Swift properties are not indexed as symbols at all — neither stored (let/var) nor computed properties become nodes. Types, methods, and functions index fine; properties are silently dropped. As a result query/codegraph_search/codegraph_node can't find a property by name, and codegraph_explore/blast-radius never surface them.

Root cause is in the Swift extractor config (details below), and it's still present on main.

Environment

  • Observed on v0.9.9 (isolated repro below).
  • Confirmed still present on main by source inspection: src/extraction/languages/swift.ts still uses nameField: 'name' for property_declaration / constant_declaration.
  • Platform: macOS (darwin-arm64).

Minimal repro

Repro.swift:

import Foundation

struct ReproConfig {
    let reproStoredValue: Int                 // stored property
    var reproComputedFlag: Bool {             // computed property
        reproStoredValue > 0
    }
    func reproControlMethod() -> Bool {       // control: a method
        reproComputedFlag
    }
}

final class ReproService {
    private let reproClassStored: String = "x"   // stored property
    var reproClassComputed: Int { reproClassStored.count } // computed property
}
$ codegraph init .
●  5 nodes, 5 edges

$ sqlite3 .codegraph/codegraph.db \
    "SELECT kind, name FROM nodes WHERE kind NOT IN ('file','import') ORDER BY kind;"
class|ReproService
method|reproControlMethod
struct|ReproConfig

Expected: the four properties (reproStoredValue, reproComputedFlag, reproClassStored, reproClassComputed) appear as property/variable nodes. Actual: zero of the four are indexed. Only the struct, class, and method are.

codegraph query reproComputedFlag / query reproStoredValue return nothing; query reproControlMethod (the method) is found.

Real-world impact

On a 1,218-Swift-file production codebase this means properties are effectively invisible to the graph: codegraph status reports only 4 property nodes out of ~18.3k total, and searching a real, heavily-used computed property (e.g. nonisolated var isCloudProxy: Bool, read on 4 lines) returns "No results found." Agents that trust an empty search then conclude a real property doesn't exist.

Root cause

The generic extractor resolves a symbol's name via getChildByField(node, extractor.nameField) (src/extraction/tree-sitter.ts, ~line 69). The Swift extractor sets:

// src/extraction/languages/swift.ts:54-55
variableTypes: ['property_declaration', 'constant_declaration'],
nameField: 'name',

But in tree-sitter-swift a property's name is not under a name field — it nests as property_declaration → <name> pattern → … → simple_identifier. So getChildByField(node, 'name') returns nothing, the node is treated as nameless, and it's dropped.

Notably, the codebase already knows about this exact shape:

  • src/extraction/tree-sitter.ts:206-240 has a helper whose comment reads: "Swift's property name nests as property_declaration → <name> pattern → bound_identifier → simple_identifier; this resolves it" — but it's used for the shadow-prune path, not for Swift's primary name extraction.
  • Kotlin already handles the identical shape: src/extraction/languages/kotlin.ts uses nameField: 'simple_identifier' plus a property_declaration branch (lines ~85, ~98), and Kotlin properties index correctly.

Suggested fix

Route Swift property-name extraction through the same path Kotlin uses (and that the tree-sitter.ts:206-240 helper already implements) — e.g. switch Swift's nameField to 'simple_identifier' and/or add a property_declaration name branch mirroring Kotlin. That should make both stored and computed Swift properties index as symbols.

Happy to test a fix against a large Swift codebase if useful.

monochrome3694 avatar Jun 27 '26 16:06 monochrome3694

Fixed in #1024 (on main, ships in the next release) — thanks for the precise repro and the careful root-cause writeup. You were right that computed properties were invisible, and the SwiftUI angle is worse than the report: every view's var body: some View { … } was being dropped too.

One nuance worth flagging, since you tested against a large codebase: the report is half-stale. Stored properties (your reproStoredValue / reproClassStored) were actually fixed back in #708, shipped in v1.0.0 — they index as field/constant/variable nodes now. v0.9.9 (what you tested) predates that. The nameField: 'name' in swift.ts is a bit of a red herring — Swift in-class properties are handled by a dedicated branch elsewhere, not that generic path — which is why source-inspecting swift.ts alone read as "still broken." So if you're on v0.9.9, just upgrading already gets you stored properties; this PR is what closes the remaining computed-property gap (and protocol property requirements, a third category that was also never indexed).

What changed:

  • Computed properties + protocol property requirements now index as property nodes, so query/codegraph_explore find them.
  • A computed property's getter is read as its body, so a SwiftUI body links to the subviews and helpers it builds — the render flow is traceable through the property.

Quick check on Alamofire: property nodes went 0 → 348 (real symbols like asAFError, isInvalidURLError), with a stable re-index and no node explosion.

Your offer to test against a large Swift codebase still stands open if you're up for it once it's released — that's exactly the scale (the 1.2k-file repo you mentioned) where I'd most want a second pair of eyes. Closing as fixed; reopen if anything looks off on the released build.

colbymchenry avatar Jun 27 '26 19:06 colbymchenry