Skip to content

[Bug]: PowerShell dot-source (. file.ps1) creates no imports edge, and script-scope calls are dropped #4356

Closed

Description

@FabioRueda

Version: graphifyy 0.9.63 · Microsoft Windows 11 Home Single Language · Python 3.11.9

Summary

In PowerShell files, two common cross-file patterns produce no edges:

  1. Dot-sourcing another script (. "$PSScriptRoot\lib.ps1") creates no imports edge from the caller file to the sourced file. Dot-sourcing is PowerShell's equivalent of an import, so the file-level dependency is lost.
  2. Calls made at script scope (outside any function) are dropped. Only calls inside a function body get an edge.

Calls inside functions do resolve across files (as INFERRED), so the gap is limited to these two cases. They are the usual shape of PowerShell entry-point scripts: dot-source a library at the top, then call its functions at script scope.

Minimal repro

lib.ps1:

function Get-Answer { return 42 }

main.ps1:

. "$PSScriptRoot\lib.ps1"
function Invoke-Main { Get-Answer }
Invoke-Main
from pathlib import Path
from graphify.extract import extract
d = Path(r"<folder with the two files>")
r = extract([d / "main.ps1", d / "lib.ps1"], cache_root=d)
print([n["id"] for n in r["nodes"]])
for e in r["edges"]:
    print(e["source"], e["relation"], e["target"], e.get("confidence"))

Actual output:

['main', 'main_invoke_main', 'lib', 'lib_get_answer']
main contains main_invoke_main EXTRACTED
lib contains lib_get_answer EXTRACTED
main_invoke_main calls lib_get_answer INFERRED

Expected

In addition to the edges above:

  • main imports lib (EXTRACTED), from the dot-source line.
  • main calls main_invoke_main, from the script-scope call on line 3 (with the file node as the caller).

Impact

In a real repo (a few PowerShell build/flash scripts that dot-source a shared toolchain.ps1 and a helper module), the graph showed the scripts as unconnected. Every --update that re-extracted those files dropped any hand-added edges, so they had to be re-added after each run.

Related, but different: #3992 and #4158 (PowerShell method-call resolution).

Activity

  1. github-actions commented on Oct 12, 2026

    @github-actions

    Thanks for opening this issue, @FabioRueda. 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.

  2. FabioRueda commented on Oct 12, 2026

    @FabioRueda
    ContributorAuthor

    Root cause and a fix for this, checked against v8 at a5ba60d (0.9.86). Fix with regression tests: #4369.

    Root cause (graphify/extractors/powershell.py)

    1. Dot-source (around L264-L280). The path is read only from a command_name child of command_name_expr. tree-sitter-powershell parses the common forms differently:
      • . ./lib.ps1, . .\lib.ps1 -> command_name (handled)
      • . "$PSScriptRoot\lib.ps1", . '.\lib.ps1' -> string_literal (skipped)
      • . $PSScriptRoot\lib.ps1 -> path_command_name (skipped)
      • . (Join-Path $PSScriptRoot "lib.ps1") -> parenthesized_expression (runtime-only path)
    2. Script-scope calls (L415-L416). walk_calls runs only over function_bodies, so commands in a script's main body never become calls edges or raw_calls. Calls inside functions do resolve across files, which is why this looked partial.

    Minimal repro

    repro/
      lib.ps1    function Helper { 'hi' }
      main.ps1   . "$PSScriptRoot\lib.ps1"
                 Helper
    
    from pathlib import Path
    from graphify.extractors.powershell import extract_powershell
    r = extract_powershell(Path("repro/main.ps1"))
    print([e for e in r["edges"] if e["relation"] == "imports_from"])  # [] on 0.9.86
    print(r["raw_calls"])                                               # [] on 0.9.86, Helper is missing

    Variant results on 0.9.86 / with #4369:

    Dot-source form 0.9.86 #4369
    . ./lib.ps1, . .\lib.ps1 edge edge
    . "$PSScriptRoot\lib.ps1" none edge
    . $PSScriptRoot\lib.ps1 none edge
    . '.\lib.ps1' none edge
    . (Join-Path $PSScriptRoot "lib.ps1") none none (known limit)

    The fix in #4369

    • Accept command_name, path_command_name and string_literal and strip surrounding quotes. The existing basename logic does the rest.
    • Also call walk_calls(root, file_nid, None). walk_calls already stops at function_statement/class_statement, so function bodies are not walked twice; a control test covers this.
    • _AST_CACHE_SCHEMA 8 -> 9, so cached results for unchanged .ps1 files are refreshed.

    The new tests fail on v8 (4 of 5; the 5th is the no-duplication control) and pass with the fix. Full suite and lint results are in the PR description. Prepared with Claude Code on my behalf.

  3. safishamsi commented on Oct 12, 2026

    @safishamsi
    Member

    Fixed in v0.9.88 (graphifyy 0.9.88 on PyPI) via #4369.

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