8000 Fix 'using module' when module has non-terminating errors handled with 'SilentlyContinue' by daxian-dbw · Pull Request #4711 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Fix 'using module' when module has non-terminating errors handled with 'SilentlyContinue' #4711

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

Merged
merged 1 commit into from
Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,13 @@ private static PSModuleInfo LoadModule(PSModuleInfo originalModuleInfo)
.AddParameter("Name", modulePath)
.AddParameter("PassThru");
var moduleInfo = ps.Invoke<PSModuleInfo>();
if (ps.HadErrors)

// It's possible that 'ps.HadErrors == true' while the error stream is empty. That would happen if
// one or more non-terminating errors happen when running the module script and ErrorAction is set
// to 'SilentlyContinue'. In such case, the errors would not be written to the error stream.
// It's OK to treat the module loading as successful in this case because the non-terminating errors
// are explicitly handled with 'SilentlyContinue' action, which means they don't block the loading.
if (ps.HadErrors && ps.Streams.Error.Count > 0)
{
var errorRecord = ps.Streams.Error[0];
throw InterpreterError.NewInterpreterException(modulePath, typeof(RuntimeException), null,
Expand Down
22 changes: 22 additions & 0 deletions test/powershell/Language/Classes/scripting.Classes.using.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -523,5 +523,27 @@ using module FooForPaths
}
}
}

Context "module has non-terminating error handled with 'SilentlyContinue'" {
BeforeAll {
$testFile = Join-Path -Path $TestDrive -ChildPath "testmodule.psm1"
$content = @'
Get-Command -CommandType Application -Name NonExisting -ErrorAction SilentlyContinue
class TestClass { [string] GetName() { return "TestClass" } }
'@
Set-Content -Path $testFile -Value $content -Force
}
AfterAll {
Remove-Module -Name testmodule -Force -ErrorAction SilentlyContinue
}

It "'using module' should succeed" {
$result = [scriptblock]::Create(@"
using module $testFile
[TestClass]::new()
"@).Invoke()
$result.GetName() | Should Be "TestClass"
}
}
}

0