Skip to content

Add fallback to session state if function definition in Ast is broken in PseudoBinding#27616

Open
MartinGC94 wants to merge 2 commits into
PowerShell:masterfrom
MartinGC94:fixCmdCompletion
Open

Add fallback to session state if function definition in Ast is broken in PseudoBinding#27616
MartinGC94 wants to merge 2 commits into
PowerShell:masterfrom
MartinGC94:fixCmdCompletion

Conversation

@MartinGC94

Copy link
Copy Markdown
Contributor

PR Summary

This PR updates the PseudoBinding logic so there is a fallback to the session state if a function definition in the script text is broken for whatever reason.
This is intended as a workaround for the issue where you can't tab complete function parameters inside a script if the function uses a PowerShell class. Like in this scenario:

enum Animals
{
    Dog    = 0
    Cat    = 1
}
function Demo
{
    [OutputType([string])]
    Param
    (
        [Parameter()]
        [Animals]
        $Param1
    )
}

Demo -<Tab>
# or
(Demo).<Tab>

After this change you can load the class and function definitions into the session state (select them and press F8 in ISE/VS code) and now the tab completion will use the definitions found in the session state.

Ideally the PseudoBinding would be able to handle PowerShell classes in a scenario like this, but it relies on compiling the function definition, and when it does that it can't find the class.
I believe a proper fix would be to revamp the PseudoBinding so it doesn't have to compile the function definitions it finds, and can instead use static analysis of the function definition ast to determine the available parameters and output type. However, in the interest of reducing the workload for both me and the reviewer(s) <Looks at the 276 pending PRs> I have opted for this simple approach that can hopefully get through the process a little quicker.

PR Context

This fixes one of my main issues when working with PowerShell classes.

PR Checklist

@MartinGC94 MartinGC94 requested a review from a team as a code owner June 20, 2026 22:06
Copilot AI review requested due to automatic review settings June 20, 2026 22:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates tab-completion’s pseudo-parameter binding to fall back to the current session state when a function definition found in script text can’t be compiled (e.g., due to unresolved types), improving parameter and member completion in those “broken AST” scenarios.

Changes:

  • Update PseudoParameterBinder to ignore RuntimeException from AST-based function compilation and then attempt command creation from session state for completion.
  • Add Pester coverage to validate parameter completion and output-member completion when the script text’s function definition references missing types.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/System.Management.Automation/engine/CommandCompletion/PseudoParameterBinder.cs Adds a guarded fallback path so tab completion can use an already-loaded session-state definition when AST compilation fails.
test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 Adds tests exercising the new fallback behavior for both parameter completion and member completion.

Comment thread test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Comment thread test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 Outdated
Comment thread test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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