Skip to content

Make the kit's functions global so closures can call them - #18

Merged
HalRiveria merged 3 commits into
mainfrom
fix/global-functions
Oct 28, 2026
Merged

HalRiveria merged 3 commits into
mainfrom
fix/global-functions

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

What was wrong. Suppose a script dot-sources the kit and is run from a prompt (.\script.ps1, or & script). The kit's functions then exist only in that script's scope, and a .GetNewClosure() block can only see global functions. So:

  • Invoke-PkProcessBatch failed every command. Its internal callbacks are closures, and they couldn't find Start-PkProcess / New-PkBatchResult.
  • Any caller closure that called a kit function failed too, for example Invoke-PkRun { ... }.GetNewClosure(), or a -ResultHandler closure using Write-PkMessage.

Run with powershell -File, everything worked. That's how the harness, CI and every earlier test ran, so this has been there since v1.0. It turned up when the host-check script was run from a Windows Terminal prompt.

What changed.

  • At the end of the file, the kit also defines each of its functions globally, so kit functions work from any closure however the script was started.
  • Get-Help still works on the global copies.
  • The README notes that the functions are global. It also says a closure still can't call the script's own functions when run from a prompt, so define those as function global:Name.

Testing. A new harness test runs a script in a fresh process through -Command "& script", the way a prompt runs it. It checks:

  • a batch completes with the right exit code;
  • a .GetNewClosure() block calling a kit function works.

Both fail against main ("New-PkBatchResult is not recognized") and pass here. 219 assertions pass on Windows PowerShell 5.1 and PowerShell 7.6, and 220 when merged with #16. It merges cleanly with #13, #15, #16 and #17.

🤖 Generated with Claude Code

HalRiveria and others added 3 commits October 28, 2026 06:10
When a script dot-sourced the kit and was run from a prompt
(.\script.ps1 or & script), the kit's functions existed only in that
script's scope. A .GetNewClosure() block can only see global functions,
so Invoke-PkProcessBatch - whose internal callbacks are closures -
failed every command ("Start-PkProcess is not recognized"), and so did
any caller closure that called a kit function. Under powershell -File it
worked, which is how every test ran, so this went unnoticed since v1.0.

The kit now also defines each of its functions globally when loaded.
README: notes the global functions, and that a closure can't call the
script's own functions when run from a prompt unless they're global.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The export walked every function in the file the kit's code sits in, so
a script that pasted the kit in had its own functions made global, and
one defined after the kit caused a 'Cannot find path' error at load
(fatal under strict mode). The loop also ran in the caller's scope,
overwriting and removing two of its variables. It now only exports
Pk-named functions and Initialize-ParallelKit, from a child scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
HalRiveria added a commit that referenced this pull request Oct 28, 2026
…cessTree

Brings in the process-tree kill changes and the global functions so the
help describes the behaviour they add: Stop-PkProcessTree takes several
programs, and Invoke-PkProcessWithTimeout / the dispatcher's force-stop
kill a program's children too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@HalRiveria
HalRiveria merged commit fca69c3 into main Oct 28, 2026
2 checks passed
@HalRiveria
HalRiveria deleted the fix/global-functions branch October 28, 2026 16:48
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.

2 participants