From 21de8d3f8a4dc2929e9b11b05934b3e408cea42e Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Sun, 27 Apr 2025 13:47:24 +0200 Subject: [PATCH] Revert "Allow empty prefix string in 'Import-Module -Prefix' to override default prefix in manifest (#20409)" This reverts commit b8ed5be26d0a477e5061a216223ac0edd8eedb95. --- .../engine/Modules/ModuleCmdletBase.cs | 12 +++++++++++- .../Import-Module.Tests.ps1 | 12 ------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index 105704fb256..f47f9b45e48 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -1620,7 +1620,17 @@ internal PSModuleInfo LoadModuleManifest( if (bailOnFirstError) return null; } - string resolvedCommandPrefix = BasePrefix ?? defaultCommandPrefix ?? string.Empty; + string resolvedCommandPrefix = string.Empty; + + if (!string.IsNullOrEmpty(defaultCommandPrefix)) + { + resolvedCommandPrefix = defaultCommandPrefix; + } + + if (!string.IsNullOrEmpty(this.BasePrefix)) + { + resolvedCommandPrefix = this.BasePrefix; + } if (!string.IsNullOrEmpty(actualRootModule)) { 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 2cc21dc03f3..6885abe847d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -57,18 +57,6 @@ Describe "Import-Module" -Tags "CI" { (Get-Module TestModule).Version | Should -BeIn "1.1" } - - It 'Should override default command prefix if an empty string is provided' { - $ModuleName = "PrefixTestModule" - $ModulePath = Join-Path -Path $testdrive -ChildPath $ModuleName - $null = New-Item -Path $ModulePath -ItemType Directory -Force - 'function Use-Something{}' | Set-Content -Path "$ModulePath\$ModuleName.psm1" -Force - New-ModuleManifest -Path "$ModulePath\$ModuleName.psd1" -DefaultCommandPrefix MyPrefix -FunctionsToExport Use-Something -RootModule "$ModuleName.psm1" - $ImportedModule = Import-Module -Name "$ModulePath\$ModuleName.psd1" -Prefix "" -PassThru - $ImportedModule.ExportedCommands.ContainsKey('Use-Something') | Should -Be $true - Remove-Module -ModuleInfo $ImportedModule - } - It 'ProcessorArchitecture should work' { $currentProcessorArchitecture = switch ([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture) { 'X86' { 'x86' }