Skip to content

PowerShellFar debugging

Roman Kuzmin edited this page Apr 9, 2024 · 2 revisions

VSCode debugger

The best debugging tool is the VSCode PowerShell extension. Configure its debugger: in global or local settings.json add the following entry to launch / configurations:

  {
    "type": "PowerShell",
    "request": "attach",
    "name": "PowerShell attach",
    "runspaceId": 1
  }

Select this debugger as active and start (F5). In the shown list of processes hosting PowerShell select Far Manager. Assuming you have set some breakpoints, run your PowerShell code in Far Manager. When breakpoints are hit, VSCode debugger opens the source code at the active breakpoint.

Known issues

(1) Run VSCode as admin if Far Manager runs as admin. Otherwise attaching the debugger may not work silently or with cryptic errors from OmniSharp.

(2) Use the latest VSCode PowerShell (v2024.2.0 or v2024.3.0-preview). In older versions attaching the debugger fails, see #4843.

(3) Breakpoints set in Far Manager are removed when debugger attaches, see #4489. Until this issue is fixed, set breakpoints in VSCode instead.

(4) On starting the debugger you may see the below message. PowerShell is not loaded yet, e.g. due to no .ps1 files in the workspace or loading in progress. Then try F5 in a few seconds. Or open VSCode in folders with some .ps1 files, e.g. scripts to be debugged.

Cannot debug or run a PowerShell script until the PowerShell session has started. Wait for the PowerShell session to finish starting and try again.

Add-Debugger.ps1

If VSCode is not an option for various reasons then use the script Add-Debugger and call it with a temp file for watching in a separate console:

ps: Add-Debugger $env:TEMP\debug.log

This way looks unusual but debugging is surprisingly robust. Also, it works in some cases when the usual debugging is tricky. For example any code started by Start-FarTask.

With Start-FarTask you may use two debuggers at the same time, to debug the code flow across two running parallel runspaces:

  • Add-Debugger for task code in its runspace (auto started on -AddDebugger [-Step]).
  • VSCode debugger for the main runspace code in job, ps:, run (attach it manually).