Skip to content

fix(bash): resolve a source through a variable a loaded file sets (#5574) - #5775

Open
ishyverma wants to merge 3 commits into
Graphify-Labs:v8from
ishyverma:fix/bash-var-from-loaded-file
Open

ishyverma wants to merge 3 commits into
Graphify-Labs:v8from
ishyverma:fix/bash-var-from-loaded-file

Conversation

@ishyverma

@ishyverma ishyverma commented Dec 4, 2026 •

Copy link
Copy Markdown

What does this PR do?

Fixes reproduction 1 of #5574: a script that loads an env file which sets ROOT, then loads "$ROOT/lib/greet.sh", now gets the imports_from edge to greet.sh and the calls edge to greet.

# bin/tool
HERE="$(cd "$(dirname "$0")/.." && pwd)"
. "$HERE/lib/env.sh"        # sets ROOT
. "$ROOT/lib/greet.sh"      # was: no edge
greet

Before: imports_from bin_tool -> lib_env only.
After: also imports_from bin_tool -> lib_greet (INFERRED, 0.85, same as #5563's caller-defined bases) and calls bin_tool__entry -> lib_greet_greet.

How:

  • extractors/bash.py: each file now records bash_exported_vars, the top-level variable state it leaves set once it has run. It is the same snapshot that source sites already use. Sources processed from function bodies are tagged in_function. Top-level sources that may not run (inside if, loops, case, &&/|| lists) are tagged conditional, and each top-level source records source_order (its byte offset) so loads on one line are ordered as bash runs them.
  • ARCHITECTURE.md: a short paragraph next to the existing Bash note describing the rule and its limits.
  • symbol_resolution.py: in _propagate_bash_var_sources, a site whose variable is unset in its own file first looks at the files its script sourced earlier at top level: on an earlier line, or earlier on the same line (. env.sh; . "$ROOT/lib/x.sh"). The last such load that sets or clears the variable decides, as in bash. That value takes precedence over one passed down by a loader (Bash extractor: a sourced file's . "$ROOT/lib/x.sh" gets no edge when ROOT is set by the script that loads it #5563), since it runs later. Loads resolved through a variable take part via the existing fixed-point loop.

Only step 1 of the issue is implemented. Step 2 (searching parent folders for a variable no scanned file sets) is a guess, and CONTRIBUTING asks to fail closed, so it is left out. Happy to follow up if maintainers want it behind an option.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariant: a loaded file's variable is used only when it is certain to be set when the site runs. Otherwise the site stays unresolved, never guessed. Fail-closed cases, each covered by a test:

  • the variable is assigned or unset in a branch, a loop, an &&/|| list or a function body of the loaded file (also when it is reassigned or unset conditionally after a top-level assignment). These are exported as blocked, not as a base;
  • the load itself may not run (inside an if, a loop or an &&/|| list);
  • the load comes after the site (a later line, or later on the same line);
  • the load is inside a function body (its line says nothing about when it runs);
  • a later load sets the variable to a value that cannot be known (blocks even though an earlier load set it).

When two earlier loads set the variable differently, the later one wins, including two loads on the same line.

No change to existing behaviour when no loaded file mentions the variable. Resolution then falls back to the #5563 loader propagation as before.

Cache: the bash extractor output gains fields (bash_exported_vars, and source_order on source entries), so AST cache entries made before this change lack them. Such scripts resolve as before (line-only ordering) until re-extracted. Leaving the AST cache schema bump to the maintainers' release step, as usual.

Limitation: only files the script sources directly export their state. A variable that a loaded file itself received from a file it loaded is not carried back further.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

How was this tested?

uv run pytest tests/test_extract.py -k 5574        # 18 passed
# without the source change: the positive cases fail; the review cases (conditional load / conditional unset, same-line ordering) also fail before their follow-up commits
uv run pytest tests -n auto                        # 13037 passed, 166 skipped, 5 xfailed
uv run pytest tests/test_architecture_doc.py        # 38 passed
uv run ruff check .                                # All checks passed!
uv run pyright                                     # 784 errors, identical count to v8; none in the changed lines

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (n/a)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

Refs #5574

@ishyverma
ishyverma requested a review from safishamsi as a code owner December 4, 2026 17:58

@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 review View in Graphify

Review before merging · Merge confidence 4/5

1 bug · High impact

P2  An unused function assignment can hide a real imported file · bash.py:1040

What this review covered
  • We didn't review 1 file that isn't source code, such as docs, config, images, or generated or third-party code.
  • Some lookups asked for files or lines that do not exist; the review was told and carried on.
  • 3 of 3 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 4304 functions depend on this change
  • 2 functions became more coupled. resolve_bash_source_edges() (18 callers), extract_bash_source() (3 callers).
  • 692 of 692 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed 0df9dd1 · 1 of 1 earlier findings resolved

…aphify-Labs#5574)

A script that loads an env file setting ROOT, then loads "$ROOT/lib/x.sh",
got no edge to x.sh: the extractor only knew the variables set in the
script itself or passed down by the scripts that load it (Graphify-Labs#5563).

The extractor now records what each file leaves set at top level once it
has run (bash_exported_vars). The resolver lets a site's unset variable
take its folder from the files its own script sourced on an earlier
top-level line, the last such load deciding, as in bash; that value takes
precedence over one a loader passed down, since it runs later.

Fails closed: a variable assigned in a branch, loop or function body of
the loaded file, a load on a later line, a load inside a function body,
a later load that sets the variable to an unknown value, and two loads on
one line that disagree all leave the site unresolved.

Only step 1 of the issue is taken: the parent-folder search for a
variable no scanned file sets is a guess, so it is left out.

Co-Authored-By: Claude <noreply@anthropic.com>
Comment thread graphify/symbol_resolution.py Resolved
Comment thread graphify/extractors/bash.py Resolved
…-file vars (Graphify-Labs#5574)

Review follow-up on Graphify-Labs#5775:

- A top-level `source` inside an `if`, a loop, a `case` or an `&&`/`||`
  list may not run, so it is now tagged `conditional` (as are sources in
  function bodies, `in_function`, now also on variable-built sources) and
  never decides what a later source line sees.
- An `unset` in a branch, list or function body of a loaded file may or
  may not run, so the variable is exported as blocked instead of keeping
  its earlier base.

Tests: six new cases (both findings, three shapes each) fail without
this change.

Co-Authored-By: Claude <noreply@anthropic.com>
@ishyverma
ishyverma force-pushed the fix/bash-var-from-loaded-file branch from 1af44d1 to 5c51b61 Compare December 4, 2026 18:19
Comment thread graphify/symbol_resolution.py Resolved
…abs#5574)

Review follow-up on Graphify-Labs#5775: `_bash_loaded_file_base` compared source
sites by line only, so `. env.sh; . "$ROOT/lib/x.sh"` dropped the load
that runs first on the site's own line, and two loads on one line were
treated as disagreeing instead of the later one winning.

Top-level source entries now carry `source_order`, the command's byte
offset, and the resolver orders sites by `(line, offset)`. An entry
without it (an older cache) keeps the previous line-only behaviour.

Tests: three new cases; the same-line load and the two-loads-on-one-line
cases fail without this change.
@ishyverma

Copy link
Copy Markdown
Author

@safishamsi could you take a look when you have a moment? All three review findings are addressed and the description matches the latest commit. Thanks!

Comment on lines +1037 to +1040
def _collect_nested_assignments(node, top_level: bool) -> None:
if node.type == "variable_assignment" and not top_level:
name_node = node.child_by_field_name("name")
if name_node is not None and (name := _read_text(name_node, source).strip()):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  An unused function assignment can hide a real imported file

Sourcing a file that sets ROOT at top level and also defines a function that assigns ROOT can make the later source "$ROOT/lib/greet.sh" disappear from the graph, even if that function is never called.

Exclude assignments inside function bodies from _collect_nested_assignments' exported-state blocking.

Evidence · checked against the code

Expected: The top-level ROOT value remains available to resolve the later source because defining the function does not run its assignment.
Actually: The function-body assignment marks ROOT blocked in the sourced file's exported state, so propagation cannot use the top-level value and may omit the import edge.

  • Independent source review: Checked the resolver's handling of earlier concrete source loads and exported variable state. It does not exclude function-body assignments from the blocked export; a blocked name makes the loaded base unknown. Conditional and function-contained source sites are excluded from ordered loads, but that does not guard this case: the source that loads the file can be top-level and unconditional.
Prompt to fix with AI
In graphify/extractors/bash.py, _collect_nested_assignments (lines 1037-1040):
Problem: An unused function assignment can hide a real imported file
Expected: The top-level `ROOT` value remains available to resolve the later source because defining the function does not run its assignment.
Actually: The function-body assignment marks `ROOT` blocked in the sourced file's exported state, so propagation cannot use the top-level value and may omit the import edge.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed this case exists, but it only drops an edge: an assignment inside a function body makes ROOT unknown, so the later source is skipped rather than linked to a wrong file. That is the cautious side, and modelling whether the function is ever called is outside this fix. Happy to handle it in a follow-up if you want it.

This branch has not been deployed

No deployments
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.

1 participant