Skip to content

Import-Module does not properly report errors to users #9855

@KirkMunro

Description

@KirkMunro

If you have a script module that generates a terminating error, the error message shown to users does not indicate that the error came from Import-Module. Instead it simply shows the script module internals. This is inappropriate for users who may not have authored that script module.

If you have a function in a script module that generates a terminating error, and you invoke that function when the module is not loaded, the module will load, the terminating error will not be shown to end users at all, and the function will run. The suppression of the terminating error in this scenario needs to be corrected so that users see the error came from Import-Module which was implicitly invoked by invoking the command while auto-loading is enabled.

Note: This issue is closely related to the problem with terminating error handling in PowerShell (for which a collection of RFCs have been submitted as a PR). Terminating error handling aside, this issue focuses on how Import-Module does a poor job of reporting errors, sometimes not even reporting them at all.

Steps to reproduce

$moduleName = 'ModuleErrorHandlingTest'
$modulePath = Join-Path -Path $([Environment]::GetFolderPath('MyDocuments')) -ChildPath PowerShell/Modules/${moduleName}
New-Item -Path $modulePath -ItemType Directory -Force > $null
$nmmParameters = @{
    Path = "${modulePath}/${moduleName}.psd1"
    RootModule = "./${moduleName}.psm1"
    FunctionsToExport = @('Test-ErrorHandling')
}
New-ModuleManifest @nmmParameters
$scriptModulePath = Join-Path -Path $modulePath -ChildPath "${moduleName}.psm1"
New-Item -Path $scriptModulePath -ItemType File | Set-Content -Encoding UTF8 -Value @'
    1/0 # Oops!
    function Test-ErrorHandling {
        [cmdletBinding()]
        param()
        'Error handling output'
    }
'@

# This next command generates an error while loading the module, but the module still
# loads, and the error message does not tell the user that it came from Import-Module
# nor does it tell the user which module was being loaded when the error occurred.
Import-Module $moduleName
Remove-Module $moduleName

# This next command generates an error while implicitly loading a module, but the error
# is silenced and the module loads anyway so the end user has no idea that something
# happened.
Test-ErrorHandling
Remove-Module $moduleName
Remove-Item -LiteralPath $modulePath -Recurse -Force

Expected behavior

Note the differences in the actual error text that are shown in what should be expected when a module is loaded over what is actually shown below this code block.

Import-Module: Unexpected error while importing ModuleErrorHandlingTest module. Attempted to divide by zero.
At line:23 char:1
+ Import-Module $moduleName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

Import-Module: Unexpected error while implicitly importing ModuleErrorHandlingTest module. Attempted to divide by zero.
At line:29 char:1
+ Test-ErrorHandling
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

Error handling output

Actual behavior

Attempted to divide by zero.
At C:\Users\kirka\OneDrive\Documents\PowerShell\Modules\ModuleErrorHandlingTest\ModuleErrorHandlingTest.psm1:1 char:5
+     1/0 # Oops!
+     ~~~
+ CategoryInfo          : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

Error handling output

Environment data

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Corecmdlets in the Microsoft.PowerShell.Core module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions