Skip to content

Fix null reference when module is null in hosting scenarios - #9404

Merged
Dongbo Wang (daxian-dbw) merged 9 commits into
PowerShell:masterfrom
adityapatwardhan:FixMarkdownIssue
May 3, 2019
Merged

Dongbo Wang (daxian-dbw) merged 9 commits into
PowerShell:masterfrom
adityapatwardhan:FixMarkdownIssue

Conversation

@adityapatwardhan

@adityapatwardhan Aditya Patwardhan (adityapatwardhan) commented Apr 18, 2019 •

Copy link
Copy Markdown
Member

PR Summary

Fixes #9390

If the CommandInfo.Module is null, then default to a new PSMarkdownOptionInfo.

PR Context

In hosting scenarios as module are loaded as snapins, the CommandInfo.Module is null. In those cases we default back to default PSMarkdownOptionInfo.

PR Checklist

{
throw new InvalidOperationException();
}
_mdOption = this.CommandInfo.Module?.SessionState.PSVariable.GetValue("PSMarkdownOptionInfo", new PSMarkdownOptionInfo()) as PSMarkdownOptionInfo;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you please clarify for my education why is this.CommandInfo.Module null in the scenario?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Its part of the PR context.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

My question is why this is null if it is running in new runspace. I do not understand the difference and therefore I am afraid that there may be a bug.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.commandinfo.module?view=powershellsdk-1.1.0#System_Management_Automation_CommandInfo_Module indicates that this shouldn't be null since this cmdlet is part of a module. Seems like there's a bug somewhere else. I don't think this would block this PR, but we should have an issue open to investigate this.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We already have #9390 for tracking. If it is "a bug somewhere else". Since we are at the beginning of a new milestone we could only add pending test in the PR without temporary fix.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, if it's a snapin, it won't show up as a module

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yep, it's because powershell built-in modules are loaded as snapins with [runspacefactory]::CreateRunspacePool(1, 2, $Host), as well as [runspacefactory]::CreateRunspace().
This issue repros with the following script too:

PS:100> $ps = [powershell]::Create()
PS:101> $ps.AddScript("'# test' | convertfrom-markdown").Invoke()
PS:102> $ps.Streams.Error
convertfrom-markdown : Object reference not set to an instance of an object.
At line:1 char:12
+ '# test' | convertfrom-markdown
+            ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [ConvertFrom-Markdown], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.ConvertFromMarkdownCommand

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for clarify. If snapins is deprecated should we change loading built-in modules too? If possible this would be the best fix.

@daxian-dbw Dongbo Wang (daxian-dbw) Apr 19, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The public interface of PSSnapin is deprecated, meaning user cannot define a snapin to work with PowerShell, but the snapin itself is still useful, especially in the hosting scenario.
Loading as snapin doesn't require the modules to be present (module folder or .psd1 file and etc.), so for some hosting scenario, this is convenient as the hosting application doesn't need to ship the built-in powershell modules along with the application.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I wonder - we can create an assembly with Add-Type and load it with Import-Module. We use this in our tests without creating module folder and etc.

* Instead of using the module scope variable, we now use a dictionary with the runspace as the key to store the options.
* Update tests accordingly.
@adityapatwardhan

Copy link
Copy Markdown
Member Author

Dongbo Wang (@daxian-dbw) Can you have a look again.

When moduleInfo is available we use the module scope variable.
But, when it is not available, we use a concurrent dictionary with runspace instance ID as the key to store the options.
@daxian-dbw

Copy link
Copy Markdown
Member

Also talked to Aditya Patwardhan (@adityapatwardhan) offline, and we agreed it's better to move all Get/Set psmarkdownoption code to a single class.

@adityapatwardhan

Copy link
Copy Markdown
Member Author

Dongbo Wang (@daxian-dbw) I have updated to address your comments. Please have another look.

// If we have the moduleInfo then store are module scope variable
if (command.Module != null)
{
return command.Module.SessionState.PSVariable.GetValue(MarkdownOptionInfoVariableName, new PSMarkdownOptionInfo()) as PSMarkdownOptionInfo;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I wonder that the API to force creating default value before trying to get a value.

@adityapatwardhan

Copy link
Copy Markdown
Member Author

Dongbo Wang (@daxian-dbw) Ping..

@adityapatwardhan

Copy link
Copy Markdown
Member Author

Dongbo Wang (@daxian-dbw) This is ready for merging.

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 2737e74 into PowerShell:master May 3, 2019
@adityapatwardhan
Aditya Patwardhan (adityapatwardhan) deleted the FixMarkdownIssue branch June 8, 2020 18:24
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
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.

ConvertFrom-Markdown throws Null Exception when run inside Runspace

4 participants