Skip to content

fix(powershell): dot-source quoted/$PSScriptRoot paths and script-scope calls (#4356) - #4369

Closed
FabioRueda wants to merge 1 commit into
Graphify-Labs:v8from
FabioRueda:fix/powershell-dot-source-script-scope
Closed

FabioRueda wants to merge 1 commit into
Graphify-Labs:v8from
FabioRueda:fix/powershell-dot-source-script-scope

Conversation

@FabioRueda

@FabioRueda FabioRueda commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4356. Two gaps in graphify/extractors/powershell.py dropped edges from ordinary PowerShell scripts:

  1. Dot-source with a quoted or $PSScriptRoot path. The handler only read the path when tree-sitter parsed it as command_name. . "$PSScriptRoot\lib.ps1" and . '.\lib.ps1' parse as string_literal, and unquoted . $PSScriptRoot\lib.ps1 as path_command_name, so no imports_from edge was emitted. The fix accepts all three node types and strips surrounding quotes; the existing basename logic handles the rest.
  2. Calls at script scope. walk_calls ran only over function/method bodies, so commands in a script's main body produced no calls edge and no raw_call. The fix also walks the file root with the file node as the caller. walk_calls already stops at function_statement/class_statement, so function bodies are not walked twice.

Also bumps _AST_CACHE_SCHEMA 8 -> 9 so cached results for unchanged .ps1 files are re-extracted (same approach as c68d167).

Dot-source form v8 (0.9.86) This PR
. ./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 (runtime-only path, see limitations)

Type of change

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

Verification & Invariants

Invariant: a .ps1 file that dot-sources another file gets an imports_from edge for every statically known path, and every command it invokes, in a function or at script scope, appears exactly once as a calls edge or raw_call attributed to the right caller. Calls inside a function must stay attributed to that function, not to the file; the control test test_powershell_script_scope_walk_does_not_duplicate_function_calls covers this.

Persisted state: AST cache entries written by schema 8 are invalidated by the schema bump.

  • 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?

Windows 11, Python via uv sync --all-extras, branch off v8 at a5ba60d (0.9.86).

Full suite on Python 3.10.21 and 3.13.15 (separate venvs per Python and per tree, uv run --frozen --all-extras --python X pytest tests -q), this branch vs unpatched v8:

Python v8 (a5ba60d) this PR
3.10.21 13 failed, 7291 passed, 32 skipped 13 failed, 7296 passed, 32 skipped
3.13.15 13 failed, 7290 passed, 33 skipped 13 failed, 7295 passed, 33 skipped

+5 passed = the new tests; the 13 failures are identical on both trees (Windows-specific, listed below).

uv run pytest tests/test_languages.py -q -k "quoted_or_variable or script_scope"
  # without the fix (extractor reverted): 4 failed, 1 passed (the passing one is the no-duplication control)
  # with the fix: 5 passed
uv run pytest tests/test_languages.py -q -k powershell        # 53 passed
uv run pytest tests/test_cache.py tests/test_grammar_cache_key.py tests/test_python_inherited_method_calls.py tests/test_rust_use_trees.py tests/test_languages.py -q
                                                              # 614 passed
uv run pytest tests -q                                        # 13 failed, 7295 passed, 33 skipped
  # the same 13 fail on unpatched v8 on this machine (Windows-specific):
  # tests/test_hooks.py (11) and test_absolute_path_lookup_skips_getcwd in
  # tests/test_resolve_memoization.py and tests/test_source_key_memoization.py
uv run ruff check .                                           # All checks passed!
uv run pyright                                                # 618 errors on v8 before and after; touched files: 5 before, 5 after (none new)

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (n/a: no skill sources touched)
  • 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.

Limitations

  • . (Join-Path $PSScriptRoot "lib.ps1") and other dot-sources that build the path at runtime stay unresolved.
  • Script-scope calls are attributed to the file node. Before this change they were not recorded at all.

Found while mapping a small PowerShell toolchain repo (FabioRueda/pic-baremetal), where build.ps1/flash.ps1 dot-source toolchain.ps1 via $PSScriptRoot and call Format-HexConfig at script scope. The analysis, fix, tests and this description were prepared with Claude Code (agent acting on my behalf).

🤖 Generated with Claude Code

https://claude.ai/code/session_013LcXGsdFdpLjYVHemLogkE

…pe calls (#4356)

Two gaps in the PowerShell extractor dropped edges from real-world
scripts:

- Dot-source only read the path when tree-sitter parsed it as
  command_name. `. "$PSScriptRoot\lib.ps1"` and `. '.\lib.ps1'` parse as
  string_literal, and unquoted `. $PSScriptRoot\lib.ps1` as
  path_command_name, so no imports_from edge was emitted. Accept all
  three and strip surrounding quotes; the existing basename logic
  already handles the rest.
- Calls were collected only from function/method bodies, so commands at
  script scope (the main body of most .ps1 scripts) produced no calls
  edge or raw_call. Walk the file root too, with the file node as the
  caller. walk_calls stops at function_statement/class_statement, so
  function bodies are not walked twice (covered by a control test).

`. (Join-Path $PSScriptRoot 'lib.ps1')` stays unresolved: the path is
only known at runtime.

AST cache schema 8 -> 9 so cached results for unchanged .ps1 files are
re-extracted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013LcXGsdFdpLjYVHemLogkE
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @FabioRueda. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@FabioRueda

Copy link
Copy Markdown
Contributor Author

Ran the full suite on Python 3.10 and 3.13 as requested (Windows 11, separate venvs per Python version and per tree), this branch vs unpatched v8 (a5ba60d):

Python v8 this PR
3.10.21 13 failed, 7291 passed, 32 skipped 13 failed, 7296 passed, 32 skipped
3.13.15 13 failed, 7290 passed, 33 skipped 13 failed, 7295 passed, 33 skipped

The +5 are the new tests. The 13 failures are the same on both trees and both versions, and are Windows-specific: 11 in tests/test_hooks.py (shell/backslash path allowlists) and test_absolute_path_lookup_skips_getcwd in tests/test_resolve_memoization.py and tests/test_source_key_memoization.py. I have not run it on Linux or macOS. The PR description is updated with the same table.

safishamsi added a commit that referenced this pull request Oct 12, 2026
Python property-accessor decorators and `Self` (#4385, #4386), Swift
`Self` (#4387), Go generic type params (#4388), the Python forward
type-hint file-node fix (#4343), PowerShell dot-source paths (#4369),
and the C# verbatim-interpolated-string recovery (#3612) all change AST
extraction output, so warm caches must re-extract.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.88, just released to PyPI as graphifyy 0.9.88. Landed via an authorship-preserving cherry-pick (your commit keeps your name and counts on your contributor graph), which is why GitHub marks this closed rather than merged. Thanks @FabioRueda!

@safishamsi safishamsi closed this Oct 12, 2026
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.

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

2 participants