Repository navigation
Conversation
There was a problem hiding this comment.
Graphify review 
1 bug · High impact
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>
…-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>
1af44d1 to
5c51b61
Compare
…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.
|
@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! |
| 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()): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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 theimports_fromedge togreet.shand thecallsedge togreet.Before:
imports_from bin_tool -> lib_envonly.After: also
imports_from bin_tool -> lib_greet(INFERRED, 0.85, same as #5563's caller-defined bases) andcalls bin_tool__entry -> lib_greet_greet.How:
extractors/bash.py: each file now recordsbash_exported_vars, the top-level variable state it leaves set once it has run. It is the same snapshot thatsourcesites already use. Sources processed from function bodies are taggedin_function. Top-level sources that may not run (insideif, loops,case,&&/||lists) are taggedconditional, and each top-level source recordssource_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
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:
unsetin 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;if, a loop or an&&/||list);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, andsource_orderon 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.
How was this tested?
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (n/a)Refs #5574