Skip to content

ConvertFrom-Markdown throws Null Exception when run inside Runspace #9390

Closed

Description

When running the ConvertFrom-Markdown command from within a new runspace, the command throws a null reference exception.

Steps to reproduce

$pool = [runspacefactory]::CreateRunspacePool(1, 2, $Host)
$pool.Open()

$ps = [powershell]::Create()
$ps.RunspacePool = $pool
$ps.AddScript({
    try {
        '# test' | ConvertFrom-Markdown | Out-Default
    }
    catch {
        $_ | Out-Default
    }
}) | Out-Null

$ps.Invoke()
$ps.Dispose()

Expected behavior

The command should return a MarkdownInfo object

Actual behavior

The command throws a null reference exception:

ConvertFrom-Markdown : Object reference not set to an instance of an object.
At line:3 char:21
+         '# test' | ConvertFrom-Markdown | Out-Default
+                     ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [ConvertFrom-Markdown], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.ConvertFromMarkdownCommand

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Linux 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

(note: the same happens on Windows, too)

Notes

Looking into the code, it seems to be caused by the following line:

_mdOption = this.CommandInfo.Module.SessionState.PSVariable.GetValue("PSMarkdownOptionInfo", new PSMarkdownOptionInfo()) as PSMarkdownOptionInfo;

The this.CommandInfo.Module is null when running within a new runspace. The line appears to create a default PSMarkdownOptionInfo object if the session variable is null - so if the module is null maybe create a default PSMarkdownOptionInfo?

Activity

  1. added
    Issue-Question
    on Apr 17, 2019
  2. iSazonov commented on Apr 18, 2019

    @iSazonov
    Collaborator

    /cc Aditya Patwardhan (@adityapatwardhan) Steve Lee (@SteveL-MSFT)

    I debug the code. It is bug.
    this.CommandInfo.Module is null here:

    protected override void BeginProcessing()
    {
    _mdOption = this.CommandInfo.Module.SessionState.PSVariable.GetValue("PSMarkdownOptionInfo", new PSMarkdownOptionInfo()) as PSMarkdownOptionInfo;

  3. added
    Issue-Bug
    and removed
    Issue-Question
    on Apr 18, 2019
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-BugResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions