Skip to content

Bash extractor: a sourced file's . "$ROOT/lib/x.sh" gets no edge when ROOT is set by the script that loads it #5563

Done

Description

@yashsaxena

What happens

A library loads another library through a variable it does not set itself: the script that loads it sets the variable first.

# bin/tool:      ROOT="$(cd "$(dirname "$0")/.." && pwd)"; . "$ROOT/lib/alpha.sh"
# lib/alpha.sh:  . "$ROOT/lib/beta.sh"

This is common in bash: an entry script sets ROOT once, and every library it loads uses it.
Graphify resolves $ROOT in bin/tool, where it is set, but not in lib/alpha.sh, so lib/alpha.sh gets no edge to lib/beta.sh and its calls into it are lost.

Reproduced on graphifyy 0.9.122 (newest on PyPI), installed with pip, nothing patched, Python 3.12, Ubuntu.

Reproduction

Three files in a new git repo:

# bin/tool
#!/usr/bin/env bash
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
. "$ROOT/lib/alpha.sh"
alpha
# lib/alpha.sh
. "$ROOT/lib/beta.sh"
alpha() { beta; }
# lib/beta.sh
beta() { echo beta; }

Command:

graphify extract "$PWD" --code-only --out /tmp/out
jq -r '.links[] | select(.relation != "contains" and .relation != "defines")
       | "\(.relation) \(.source) -> \(.target)"' /tmp/out/graphify-out/graph.json

Expected

imports_from bin/tool -> lib/alpha.sh
imports_from lib/alpha.sh -> lib/beta.sh
calls lib/alpha.sh alpha -> lib/beta.sh beta

Got

calls bin/tool (script) -> lib/alpha.sh alpha
imports_from bin/tool -> lib/alpha.sh

Suggested fix

When a file loads $VAR/x without setting VAR, use the value VAR has in the files that load it (directly or through other loaded files).
If no loader sets it, keep today's behaviour.
Related: #2079 and #2172 (the ${VAR} source resolution within one file).

Activity

  1. github-actions commented on Nov 28, 2026

    @github-actions

    Thanks for opening this issue, @yashsaxena. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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