KEMBAR78
Fix completion that relies on pseudobinding in script blocks by MartinGC94 · Pull Request #25122 · PowerShell/PowerShell · GitHub
Skip to content

Conversation

@MartinGC94
Copy link
Contributor

PR Summary

Fixes an issue in the pseudobinding code where a parse exception would prevent the pseudobinding from working when attempting to parse an incomplete script. This would prevent this: & {(New-Guid).<Tab> from working.
It would fail because the script text has errors (due to incomplete input) and the safe value visitor would try to create a scriptblock based on the incomplete text.

PR Context

PR Checklist

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Mar 5, 2025
{
try
{
commandName = GetSafeValueVisitor.GetSafeValue(commandAst.CommandElements[0], null, GetSafeValueVisitor.SafeValueContext.ModuleAnalysis) as string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please add a code comment to document the scenario, maybe with examples of command names we expect here?

@iSazonov iSazonov self-assigned this Mar 5, 2025
string commandName = commandAst.GetCommandName();
if (commandName is null)
{
// GetCommandName only works if the name is a string constant. GetSafeValueVistor can evaluate some safe dynamic expressions
Copy link
Collaborator

Choose a reason for hiding this comment

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

GetCommandName? Comment for the method says about scripts, modules, aliases. So I'd like to understand what could be going on here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GetCommandName is the method that was run on line 280. It failed to get the name because it only handles constants but there's also this GetSafeValueVisitor that can handle a bit more dynamic expressions but still be safe. When this visitor sees a scriptblock expression, it tries to create a new scriptblock using the text but that fails when there's any parse errors in the script text. This is why: & {(New-Guid).<Tab> fails, but & {(New-Guid).N<Tab> does not, because the N removes the parser error from the missing name after the member access token.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks! I see GetSafeValue() process StringConstantExpressionAst too so we could not call commandAst.GetCommandName(). Perhaps it was added as fast way vs visitor.

@iSazonov

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@iSazonov iSazonov enabled auto-merge (squash) March 5, 2025 17:05
@iSazonov iSazonov merged commit 650189e into PowerShell:master Mar 5, 2025
39 of 41 checks passed
@microsoft-github-policy-service
Copy link
Contributor

microsoft-github-policy-service bot commented Mar 5, 2025

📣 Hey @MartinGC94, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants