Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"help" command triggers an error #16880

Closed
5 tasks done
EricBuist opened this issue Feb 14, 2022 · 14 comments
Closed
5 tasks done

"help" command triggers an error #16880

EricBuist opened this issue Feb 14, 2022 · 14 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors WG-Engine core PowerShell engine, interpreter, and runtime

Comments

@EricBuist
Copy link

Prerequisites

Steps to reproduce

In Powershell Core 7.2.1 on Windows 10, type "help" command and press enter.

Expected behavior

The "help" command should display information about how to use Powershell.

Actual behavior

The command displays the following error message.

InvalidOperation:
Line |
 124 |              $consoleWidth = [System.Math]::Max([System.Console]::Wind …
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot invoke method. Method invocation is supported only on core types in this language mode.
Out-String:
Line |
 139 |  …         $help | Out-String -Stream -Width ($consoleWidth - 1) | & $pa …
     |                                              ~~~~~~~~~~~~~~~~~~~
     | Cannot validate argument on parameter 'Width'. The -1 argument is less than the minimum allowed range of 2. Supply an argument that is greater than or equal to 2 and then try the command again.

Error details

No response

Environment data

Powershell Core 7.2.1
Windows 10 21H2

Visuals

No response

@EricBuist EricBuist added the Needs-Triage The issue is new and needs to be triaged by a work group. label Feb 14, 2022
@iSazonov iSazonov added Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a WG-Interactive-HelpSystem help infrastructure and formatting of help WG-Interactive-Console the console experience labels Feb 14, 2022
@237dmitry
Copy link

what is $help variable?

The "help" command should display information about how to use Powershell.

help command is alias for Get-Help cmdlet and without arguments shows common information how to use help system.

@vexx32
Copy link
Collaborator

vexx32 commented Feb 14, 2022

It's not an alias, it's a built in function. So that code must be internal to the function.

From the error, it looks like you're running in constrained language mode. You'll probably need to use Get-Help rather than the builtin help function in that mode.

@jhoneill
Copy link

Method invocation is supported only on core types in this language mode.

That message normally means you have put PowerShell into a restricted language mode.
I couldn't repro the error even if select restricted (or no) language.

How are you starting PowerShell. If you just run pwsh -noProfile do you get the same error ? And what does
$ExecutionContext.SessionState.LanguageMode report ?

@237dmitry
Copy link

It's not an alias, it's a built in function.

I've always been sure that this is an alias. Very similar to the proxy function for Get-Help:

> gc function:help

<#
.FORWARDHELPTARGETNAME Get-Help
.FORWARDHELPCATEGORY Cmdlet
#>

> [Management.Automation.ProxyCommand]::Create((Get-Command Get-Help))

<#
.ForwardHelpTargetName Microsoft.PowerShell.Core\Get-Help
.ForwardHelpCategory Cmdlet
#>

@vexx32
Copy link
Collaborator

vexx32 commented Feb 14, 2022

This is what I have by default for Get-Content function:help. It's not a simple proxy function, it does additional handling for pagers and a few other things.

Expand for code
<#
.FORWARDHELPTARGETNAME Get-Help
.FORWARDHELPCATEGORY Cmdlet
#>
[CmdletBinding(DefaultParameterSetName='AllUsersView', HelpUri='https://go.microsoft.com/fwlink/?LinkID=113316')]
param(
    [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    ${Name},

    [string]
    ${Path},

    [ValidateSet('Alias','Cmdlet','Provider','General','FAQ','Glossary','HelpFile','ScriptCommand','Function','Filter','ExternalScript','All','DefaultHelp','DscResource','Class','Configuration')]
    [string[]]
    ${Category},

    [Parameter(ParameterSetName='DetailedView', Mandatory=$true)]
    [switch]
    ${Detailed},

    [Parameter(ParameterSetName='AllUsersView')]
    [switch]
    ${Full},

    [Parameter(ParameterSetName='Examples', Mandatory=$true)]
    [switch]
    ${Examples},

    [Parameter(ParameterSetName='Parameters', Mandatory=$true)]
    [string[]]
    ${Parameter},

    [string[]]
    ${Component},

    [string[]]
    ${Functionality},

    [string[]]
    ${Role},

    [Parameter(ParameterSetName='Online', Mandatory=$true)]
    [switch]
    ${Online},

    [Parameter(ParameterSetName='ShowWindow', Mandatory=$true)]
    [switch]
    ${ShowWindow})

    # Display the full help topic by default but only for the AllUsersView parameter set.
    if (($psCmdlet.ParameterSetName -eq 'AllUsersView') -and !$Full) {
        $PSBoundParameters['Full'] = $true
    }

    # Nano needs to use Unicode, but Windows and Linux need the default
    $OutputEncoding = if ([System.Management.Automation.Platform]::IsNanoServer -or [System.Management.Automation.Platform]::IsIoT) {
        [System.Text.Encoding]::Unicode
    } else {
        [System.Console]::OutputEncoding
    }

    $help = Get-Help @PSBoundParameters

    # If a list of help is returned or AliasHelpInfo (because it is small), don't pipe to more
    $psTypeNames = ($help | Select-Object -First 1).PSTypeNames
    if ($psTypeNames -Contains 'HelpInfoShort' -Or $psTypeNames -Contains 'AliasHelpInfo')
    {
        $help
    }
    elseif ($help -ne $null)
    {
        # By default use more on Windows and less on Linux.
        if ($IsWindows) {
            $pagerCommand = 'more.com'
            $pagerArgs = $null
        }
        else {
            $pagerCommand = 'less'
            # PSNativeCommandArgumentPassing arguments should be constructed differently.
            if ($EnabledExperimentalFeatures -contains 'PSNativeCommandArgumentPassing') {
                $pagerArgs = '-s','-P','Page %db?B of %D:.\. Press h for help or q to quit\.'
            }
            else {
                $pagerArgs = '-Ps"Page %db?B of %D:.\. Press h for help or q to quit\.$"'
            }
        }

        # Respect PAGER environment variable which allows user to specify a custom pager.
        # Ignore a pure whitespace PAGER value as that would cause the tokenizer to return 0 tokens.
        if (![string]::IsNullOrWhitespace($env:PAGER)) {
            if (Get-Command $env:PAGER -ErrorAction Ignore) {
                # Entire PAGER value corresponds to a single command.
                $pagerCommand = $env:PAGER
                $pagerArgs = $null
            }
            else {
                # PAGER value is not a valid command, check if PAGER command and arguments have been specified.
                # Tokenize the specified $env:PAGER value. Ignore tokenizing errors since any errors may be valid
                # argument syntax for the paging utility.
                $errs = $null
                $tokens = [System.Management.Automation.PSParser]::Tokenize($env:PAGER, [ref]$errs)

                $customPagerCommand = $tokens[0].Content
                if (!(Get-Command $customPagerCommand -ErrorAction Ignore)) {
                    # Custom pager command is invalid, issue a warning.
                    Write-Warning "Custom-paging utility command not found. Ignoring command specified in `$env:PAGER: $env:PAGER"
                }
                else {
                    # This approach will preserve all the pagers args.
                    $pagerCommand = $customPagerCommand
                    $pagerArgs = if ($tokens.Count -gt 1) {$env:PAGER.Substring($tokens[1].Start)} else {$null}
                }
            }
        }

        $pagerCommandInfo = Get-Command -Name $pagerCommand -ErrorAction Ignore
        if ($pagerCommandInfo -eq $null) {
            $help
        }
        elseif ($pagerCommandInfo.CommandType -eq 'Application') {
            # If the pager is an application, format the output width before sending to the app.
            $consoleWidth = [System.Math]::Max([System.Console]::WindowWidth, 20)

            if ($pagerArgs) {
                # Start the pager arguments directly if the PSNativeCommandArgumentPassing feature is enabled.
                # Otherwise, supply pager arguments to an application without any PowerShell parsing of the arguments.
                # Leave environment variable to help user debug arguments supplied in $env:PAGER.
                if ($EnabledExperimentalFeatures -contains 'PSNativeCommandArgumentPassing') {
                    $help | Out-String -Stream -Width ($consoleWidth - 1) | & $pagerCommand $pagerArgs
                }
                else {
                    $env:__PSPAGER_ARGS = $pagerArgs
                    $help | Out-String -Stream -Width ($consoleWidth - 1) | & $pagerCommand --% %__PSPAGER_ARGS%
                }
            }
            else {
                $help | Out-String -Stream -Width ($consoleWidth - 1) | & $pagerCommand
            }
        }
        else {
            # The pager command is a PowerShell function, script or alias, so pipe directly into it.
            $help | & $pagerCommand $pagerArgs
        }
    }

@237dmitry
Copy link

237dmitry commented Feb 14, 2022

I have so. I did not paste full output. I can't figure out what is causing the error in:

$consoleWidth = [System.Math]::Max([System.Console]::WindowWidth, 20)

[System.Math] is not a core type?

@vexx32
Copy link
Collaborator

vexx32 commented Feb 14, 2022

"Core type" just means "it's not one of the pre-approved types defined by the Restricted Language mode". Offhand I'm not sure what they are, but it's likely just PSObject and a scant handful of others.

Restricted Language mode is pretty much by design useless.

@EricBuist
Copy link
Author

EricBuist commented Feb 16, 2022 via email

@iSazonov iSazonov added Issue-Enhancement the issue is more of a feature request than a bug WG-Engine core PowerShell engine, interpreter, and runtime and removed Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a WG-Interactive-Console the console experience WG-Interactive-HelpSystem help infrastructure and formatting of help labels Feb 16, 2022
@iSazonov
Copy link
Collaborator

GetHelpPagingFunctionText() creates the function in InitialSessionState so we could set-restore LanguageMode.

@rkeithhill rkeithhill removed the Needs-Triage The issue is new and needs to be triaged by a work group. label Feb 17, 2022
@rkeithhill rkeithhill added the Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors label Feb 17, 2022
@SeeminglyScience
Copy link
Collaborator

The Engine WG reviewed this today. We agree that this function should either work the same in CLM or at the very least emit a better error message. We're marking this as up for grabs.

@SeeminglyScience SeeminglyScience removed their assignment Feb 18, 2022
@CarloToso
Copy link
Contributor

CarloToso commented Oct 27, 2022

I tested this issue in Powershell 7.2.7
$ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"
help
Get-Help

Both commands worked correctly, no errors

@kvprasoon
Copy link
Contributor

@iSazonov Looks like this can be closed

@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label Nov 15, 2023
@EricBuist
Copy link
Author

This issue does not occur anymore. I searched endlessly and cannot find a way to close it. Why can't the issue creator close it?

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Resolution-No Activity Issue has had no activity for 6 months or more label Nov 15, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label May 13, 2024
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors WG-Engine core PowerShell engine, interpreter, and runtime
Projects
None yet
Development

No branches or pull requests

9 participants