Repository navigation
Description
Summary
The #1756 fix (shipped 0.9.13, context="script_invocation") resolves an
exec-position script path literally, so it only fires when the command name is
already a plain path — ./b.sh, /abs/b.sh, or bash b.sh. Any exec-position path
built from a variable produces no edge, and "$script_dir/x.sh" is the ordinary way
a shell script runs a sibling.
The cause is an asymmetry between the two branches: the source branch reads its
path argument with _read_text(...), which preserves $, while the exec branch
relies on cmd = literal(cmd_name_node) — and literal() returns None for
anything containing $ by design. So the exec branch is never entered for a
variable-built path, and the _bash_source_suffix / _BASH_LEADING_EXPANSION
machinery that already exists for source (#2079, #2596) is never applied to it.
Separately, a source path whose leading segment is a full $(cd … && pwd)
substitution is also missed, because _BASH_LEADING_EXPANSION matches ${VAR} and
$VAR but not $(...).
Version
Reproduced on 0.9.55. Python 3.12.3, tree-sitter-bash 0.25.1.
Reproduction
Five files in an empty directory:
# b.sh
#!/bin/bash
echo hi
# f1_literal.sh
#!/bin/bash
./b.sh
# f2_inline_var.sh
#!/bin/bash
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
"$script_dir/b.sh" --go
# f3_assigned_var.sh
#!/bin/bash
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
helper="$script_dir/b.sh"
"$helper" --go
# f4_source_cmdsub.sh
#!/bin/bash
. "$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/b.sh"from pathlib import Path
from graphify.extractors.bash import extract_bash
for f in ["f1_literal.sh", "f2_inline_var.sh", "f3_assigned_var.sh", "f4_source_cmdsub.sh"]:
r = extract_bash(Path(f))
cross = [e for e in r["edges"]
if e["relation"] in ("calls", "imports_from", "imports")
and "b_sh" in str(e.get("target", "")) + str(e.get("target_file", ""))]
print(f, len(cross), [e["relation"] for e in cross])Actual
f1_literal.sh 1 ['calls']
f2_inline_var.sh 0 []
f3_assigned_var.sh 0 []
f4_source_cmdsub.sh 0 []
Expected
All four should yield a cross-file edge to b.sh. f1 already does.
Cause
graphify/extractors/bash.py:477 (exec branch):
raw = cmd if cmd.endswith(".sh") else None
if cmd in _BASH_SCRIPT_RUNNERS and args:
raw = literal(args[0])
if raw and raw.endswith(".sh"):
resolved = (path.parent / raw).resolve()cmd comes from literal(cmd_name_node) at :311, and literal() at :223 rejects
any candidate containing $:
if any(token in raw for token in ("$", "`", "$(", "<(", ">", "|", ";", "&")):
return NoneThat filter is correct for its purpose — it is a name filter — but it means the
exec branch cannot see a path. Contrast the source branch at :320, which reads the
path directly and therefore keeps $ available for _bash_source_suffix:
raw = _read_text(args[0], source).strip().strip("'\"")For f4, _BASH_LEADING_EXPANSION (:16) matches only ${VAR} / $VAR segments, so
$(cd -P -- … && pwd)/b.sh still contains $ after substitution and
_bash_source_suffix returns None.
Suggested fix
For f2, reading the command name as a path when literal() declines, then reusing
the existing suffix helper, is enough. Tested patch:
elif cmd not in defined_functions:
raw = cmd if (cmd and cmd.endswith(".sh")) else None
if cmd and cmd in _BASH_SCRIPT_RUNNERS and args:
raw = literal(args[0])
if raw is None and cmd_name_node is not None:
# The command name is an expansion, e.g. `"$script_dir/x.sh"`.
# literal() rejects any "$" by design, so read the path the way the
# `source` branch does and strip the leading ${VAR}/ segments.
cand = _read_text(cmd_name_node, source).strip().strip("'\"")
if cand.endswith(".sh") and "$" in cand:
raw = _bash_source_suffix(cand) or None
if raw and raw.endswith(".sh"):
...The existing resolved.is_file() guard is untouched, so the B-1 traversal
protection still holds — an edge is only emitted for a file present in the tree.
With that patch f2 emits calls / script_invocation and f1 is unchanged. f3 and
f4 still need work and I have not attempted patches for them:
- f3 needs the variable-assignment tracking that Bash ${VAR} source: track literal assignments to avoid a wrong-directory false positive #2172 already built for
source(var_bases) extended to exec position. - f4 needs
_BASH_LEADING_EXPANSIONto also consume a leading$(...)
segment, which needs one level of paren balancing.
One caveat on the patch, in case it matters to how you take it: on a real corpus a
few inline exec-position sites still produced no edge that I could not account for
— possibly is_inside_expansion rejection for calls nested in substitutions. I did
not chase it, so treat the patch as covering the canonical form rather than as
complete.
Impact
Measured on a private shell-heavy repo, 794 files, ~500 shell scripts. Counting real
cross-file dependencies by hand and comparing against graph.json:
- 417 of 528 (78%) missing on 0.9.55.
- The dominant idiom is
name="$script_dir/x.sh"(242 sites, f3) followed by inline
exec position (124 sites, f2).source/.accounts for a small minority. - The knock-on effect is worse than the missing edges:
graphify affected <file> 3
returned the same set as depth 1 for shell targets, because the intermediate
scripts are only ever invoked by path. Transitive reachability is effectively
unavailable for a shell codebase. graphify couplingreported a 3,139-line dashboard script as reaching 0
external files when it reaches 3 and is invoked by 5.
Applying the f2 patch above to that corpus added 10 script_invocation edges, all
of which I verified by reading the cited line — no false positives. So f2 alone is a
small win; f3 is where the volume is.
Activity
fix(bash): resolve exec-position script paths built from a variable