Skip to content

Bash extractor: calls inside command substitution x=$(fn) never get call edges — value-returning helpers look dead #2978

Closed

Description

@thefreshfish

Summary

The bash extractor never emits a calls edge when a defined function is invoked inside command substitution — x=$(helper). This is the idiom for every shell function that returns a value, so in helper-library-heavy codebases most function-call sites are invisible to the graph. Every other call context I tested (bare, ||/&& lists, pipelines, if/while conditions, redirects, !, subshells) resolves correctly since #2141 — this is the one remaining hole, and it applies to same-file calls too, not just sourced ones.

Reproduction (v0.9.48, main b2cd362, tree-sitter 0.25.2 / tree-sitter-bash 0.25.1, Linux)

lib.sh

#!/usr/bin/env bash
helper() { echo ok; }

a.sh

#!/usr/bin/env bash
source ./lib.sh

bare()      { helper; }                       # 1
subst()     { x=$(helper); echo "$x"; }       # 2  <-- missing
orlist()    { helper || return 1; }           # 3
andlist()   { true && helper; }               # 4
pipe()      { helper | cat; }                 # 5
cond()      { if helper; then :; fi; }        # 6
loop()      { while helper; do break; done; } # 7
redir()     { helper >/dev/null; }            # 8
neg()       { ! helper; }                     # 9
subshell()  { ( helper ); }                   # 10
local_sub() { local y; y=$(helper); }         # 11 <-- missing
graphify update . --no-cluster
jq -r '.links[] | select(.relation=="calls") | select(.target|endswith("helper")) | .source' graphify-out/graph.json

Result: 9 of 11 edges. subst and local_sub — the two $(helper) cases — produce nothing.

Same-file control (no source involved) shows the same: with h2() defined in the same file, bare2/or2/pipe2/cond2 get edges, subst2() { x=$(h2); } does not. So this is independent of the cross-file resolver.

Root cause

graphify/extractors/bash.py — both walk() and walk_calls() bail on any command node whose ancestor is a command_substitution / process_substitution:

_BASH_EXPANSION_PARENTS = frozenset({"command_substitution", "process_substitution"})
...
if child.type == "command" and not is_inside_expansion(child):

The comment explains the intent: $(build) exposes build as a child command node, and the token-level metacharacter filter can't reject it, so the parent context is used to reject it instead. But the emit site right below already requires name in defined_functions — an external command inside $() is filtered by that check regardless. The expansion exclusion only ever removes true calls to user-defined functions.

Scale

On a ~117k-line bash ops codebase (913 defined functions, 512 scripts): 4,556 textual $(fn …) call sites to defined functions, vs 1,488 calls edges emitted in total (per caller→callee pair). Functions that exist to return a value — token=$(read_bot_token "$dir"), id=$(lookup_matter "$name") — are called from many scripts and have zero incoming call edges, so graphify explain/path/callers report them as dead while the imports_from edges to the file that defines them resolve fine.

Proposed fix

Keep is_inside_expansion for the source-path and script-runner detection (where a $(…) really is an expansion, not a call), but at the call-edge emit site let defined_functions membership win over the expansion check — i.e. a command whose name is a known function is a call wherever it sits. That preserves the "external commands fabricate nothing" property from #2141/#2157 (non-function names are still dropped) and also covers $(...) inside if, pipelines, local x=$(fn), and $( fn | other ).

If it helps, the extractor comment at _BASH_EXPANSION_PARENTS could note that the exclusion is for path/runner detection only, so the two concerns don't get re-fused later.

Happy to test a patch against the large corpus above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions