diff --git a/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs b/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs index 9917f8c35..4903fabed 100644 --- a/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs +++ b/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs @@ -629,6 +629,12 @@ namespace Microsoft.PowerShell.Commands } else if (Directory.Exists(rootedPath)) { + // If the path ends with a directory separator, remove it + if (rootedPath.EndsWith(Path.DirectorySeparatorChar)) + { + rootedPath = Path.GetDirectoryName(rootedPath); + } + // Load the latest valid version if it is a multi-version module directory foundModule = LoadUsingMultiVersionModuleBase(rootedPath, ManifestProcessingFlags.LoadElements | diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 index 305053bb5..04577e954 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -18,6 +18,7 @@ Describe "Import-Module" -Tags "CI" { BeforeEach { Remove-Module -Name $moduleName -Force (Get-Module -Name $moduleName).Name | Should -BeNullOrEmpty + Remove-Module -Name TestModule -Force -ErrorAction SilentlyContinue } AfterEach { @@ -30,6 +31,15 @@ Describe "Import-Module" -Tags "CI" { (Get-Module -Name $moduleName).Name | Should -BeExactly $moduleName } + It "should be able to load a module with a trailing directory separator: " -TestCases @( + @{ modulePath = (Get-Module -ListAvailable $moduleName).ModuleBase + [System.IO.Path]::DirectorySeparatorChar; expectedName = $moduleName }, + @{ modulePath = Join-Path -Path $TestDrive -ChildPath "\Modules\TestModule\"; expectedName = "TestModule" } + ) { + param( $modulePath, $expectedName ) + { Import-Module -Name $modulePath -ErrorAction Stop } | Should -Not -Throw + (Get-Module -Name $expectedName).Name | Should -BeExactly $expectedName + } + It "should be able to add a module with using ModuleInfo switch" { $a = Get-Module -ListAvailable $moduleName { Import-Module -ModuleInfo $a } | Should -Not -Throw