Skip to content

Get-Help for dynamic function created from ScriptBlock does not work. #8234

@hubuk

Description

@hubuk

Reproducible on 6.1.0 and on master b525b1f

Steps to reproduce

$sb = {
    <#
    .SYNOPSIS
    Sample synopsis.
    #>    
    process { }
}
$str = [String]$sb

Set-Item -Path function:private:FunctionFromString -Value $str
(Get-Help FunctionFromString).Synopsis

Set-Item -Path function:private:FunctionFromScriptBlock -Value $sb
(Get-Help FunctionFromScriptBlock).Synopsis

Expected behavior

Sample synopsis.

Sample synopsis.

Actual behavior

Sample synopsis.

FunctionFromScriptBlock

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Analysis

The difference between these two cases can be observed in the following block of code:

else if (ast == rootAst)
{
startTokenIndex = savedStartIndex = 0;
lastTokenIndex = tokens.Length - 1;
}
else
{
// This case should be rare (but common with implicit remoting).
// We have a script block that was used to generate a function like:
// $sb = { }
// set-item function:foo $sb
// help foo
startTokenIndex = savedStartIndex = FirstTokenInExtent(tokens, ast.Extent) - 1;
lastTokenIndex = LastTokenInExtent(tokens, ast.Extent, startTokenIndex);
Diagnostics.Assert(tokens[startTokenIndex + 1].Kind == TokenKind.LCurly,
"Unexpected first token in script block");
Diagnostics.Assert(tokens[lastTokenIndex].Kind == TokenKind.RCurly,
"Unexpected last token in script block");
}

[ScriptBlock] cases are handled by the else statement where the startTokenIndex is being set to the position before script block itself. Changing - 1 to + 1 in the line

startTokenIndex = savedStartIndex = FirstTokenInExtent(tokens, ast.Extent) - 1;

solves the problem.

May I prepare a PR for this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Interactive-HelpSystemhelp infrastructure and formatting of help

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions