Skip to content

Tab completion unexpectedly returns intrinsic methods for objects with a type-native ForEach()/Where() #24729

Open

Description

@surfingoldelephant

Prerequisites

In tab completion, intrinsic methods are returned for collections (but not scalars).

If a type already has a type-native method of the same name, the intrinsic method cannot be accessed/invoked. However, it is still returned by tab completion.

The bug affects:

  • ForEach()
  • Where()
  • PSForEach()
  • PSWhere()

Steps to reproduce

# List<T> has a type-native ForEach() method.
# The intrinsic ForEach() is unavailable.
$list = [Collections.Generic.List[object]]::new()

$list.ForEach
# OverloadDefinitions
# -------------------
# void ForEach(System.Action[System.Object] action)

# Tab completion confusingly returns two methods of the same name.
# The second completion pertains to the intrinsic ForEach() which cannot be invoked.
(TabExpansion2 -inputScript '$list.For').CompletionMatches

# CompletionText ListItemText ResultType ToolTip
# -------------- ------------ ---------- -------
# ForEach(       ForEach          Method void ForEach(System.Action[System.Object] action)
# ForEach(       ForEach          Method ForEach(expression [, arguments...])

This issue doesn't occur in scenarios where the collection has yet to be instantiated.

(TabExpansion2 -inputScript '[Collections.Generic.List[object]]::new().For').CompletionMatches

# CompletionText ListItemText ResultType ToolTip
# -------------- ------------ ---------- -------
# ForEach(       ForEach          Method void ForEach(System.Action[System.Object] action)

Expected behavior

Tab completion should not return a match for an intrinsic method if an object already has a type-native method of the same name.

This test should pass:

$list = [Collections.Generic.List[object]]::new()
(TabExpansion2 -inputScript '$list.For').CompletionMatches | Should -HaveCount 1

Actual behavior

The test fails, because a completion match is unexpectedly returned for the intrinsic ForEach().

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.0-rc.1
PSEdition                      Core
GitCommitId                    7.5.0-rc.1
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. MartinGC94 commented on Jan 3, 2025

    @MartinGC94
    Contributor

    An easy fix would be to update this method to check the results for existing foreach/where completion results before adding new ones: https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs#L6637

    GitHub
    PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.
  2. theJasonHelmick commented on Feb 12, 2025

    @theJasonHelmick
    Collaborator

    The working group is investigating some possible options here. We will respond back shortly.

  3. theJasonHelmick commented on Mar 5, 2025

    @theJasonHelmick
    Collaborator

    Thanks folks - The Interactive WG agrees this is a bug and should be fixed.

  4. added
    WG-ReviewedA Working Group has reviewed this and made a recommendation
    and removed
    WG-NeedsReviewNeeds a review by the labeled Working Group
    on Mar 5, 2025
  5. surfingoldelephant commented on Mar 22, 2026

    @surfingoldelephant
    Author

    Ilya (@iSazonov) Please apply the KeepOpen label.

    To whoever ends up working on this: The bug also affects the newly added PSForEach()/PSWhere() methods added in PS v7.6, so the fix must also be applied to those.

  6. microsoft-github-policy-service commented on Sep 19, 2026

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  7. surfingoldelephant commented on Sep 19, 2026

    @surfingoldelephant
    Author

    Still an issue.

  8. rudy-07 commented on Nov 2, 2026

    @rudy-07

    I would like to work on this issue. I plan to update CompleteExtensionMethods in CompletionCompleters.cs to avoid adding intrinsic methods when matching type-native members are already present in the completion results, along with a regression test in BugFix.Tests.ps1. Could you please assign this to me?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productWG-Interactive-Consolethe console experienceWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions