You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Import-Module -Prefix '' on a module with a Default Prefix defined does not remove the prefix #16936
This is because the prefix test checks for IsNullOrEmpty instead of just null.
bool usePrefix = !string.IsNullOrEmpty(prefix);
As such it is impossible to override the prefix which affects the ability to write pester tests on modules, etc. and use the original function names so that things like references and go to definition work in IDEs like vscode.
Workaround
Make a secondary manifest that references the same module but does not have the prefix specified.
Ilya (@iSazonov) I don't think it should be a strong contract. A "proper" module (at least script wise) I write as if it has no prefix, all the commands and references are of the non-prefixed version. If a prefix is specified, everything continues to work as expected. The user can specify multiple prefixes potentially and import the module multiple times to address multiple target scenarios, so they can use Get-SourceMyFunction and Get-DestinationMyFunction for instance if the connection context is kept at module scope.
I do think that the specified bool should not be IsNullOrEmpty but simply a null check, so that an empty '' can indicate I don't want a prefix, however I understand that is more breaking than -NoPrefix so I"m fine with that too.
My current main issue pain point here is in Pester Testing, the only way I can specify my pester test commands after importing the module is to make a completely different non-prefixed manifest, or use a prefix (but then in VSCode, my GoTo references no longer work). I want to use the same non-prefixed commands that I have defined during testing without having to make an entirely separate manifest file.
I made the following workaround, the regexes are fragile but it works for my use case
functionImport-ModuleWithoutPrefix {
[CmdletBinding()]
param(
[Parameter(Mandatory,ValueFromPipeline)]$ManifestPath,
[Switch]$Force,$TempPath='TEMP:'
)
$path=Resolve-Path$ManifestPath$manifestDir=Split-Path$Path-Parent
$fileName= [io.path]::GetFileNameWithoutExtension($path)
#Manifest file name must be the same$tempPath="TEMP:\$(New-Guid)\$fileName.psd1"$manifestContent=Get-Content-Raw $path$rootModuleRegex='(?<=RootModule\s*?=\s*?[''"])(.+?)(?=[''"])'if ($manifestContent-notmatch$rootModuleRegex) {
throw"Could not find RootModule entry in manifest $path"
}
$rootModulePath= [io.path]::IsPathRooted($matches[1]) ?$matches[1] : $(Join-Path$manifestDir$matches[1])
$resolvedRootModulePath=Resolve-Path$rootModulePath$manifestContent=$manifestContent-replace$rootModuleRegex,$resolvedRootModulePath#Remove any prefix entry present. If this doesn't exist we don't error out$manifestContent=$manifestContent-replace'DefaultCommandPrefix\s*?=\s*?[''"].+?[''"]'#This should be uniqueNew-Item-ItemType File -Force -Path $tempPath-Value $manifestContent|Out-NullWrite-Verbose"Importing module $path without prefix from $tempPath"Import-Module$tempPath-Force:$Force-PassThru
}
We rolled back PR with fix due to a regression. The code is not trivial and has low test coverage.
We need to add more tests for importing modules with/without (default) prefixes.
Prefix parameter is internally BasePrefix which is initialized with string.Empty. So we should change this to null. But BasePrefix is used in some code branches which should be reviewed and fixed with covering by tests.
Activity
iSazonov commentedon Mar 2, 2022
As alternative for discussion - introduce new parameter (-NoPrefix/-RemovePrefix).
Also question for discussion - is the prefix a strong contact from module author?
JustinGrote commentedon Mar 9, 2022
Ilya (@iSazonov) I don't think it should be a strong contract. A "proper" module (at least script wise) I write as if it has no prefix, all the commands and references are of the non-prefixed version. If a prefix is specified, everything continues to work as expected. The user can specify multiple prefixes potentially and import the module multiple times to address multiple target scenarios, so they can use
Get-SourceMyFunctionandGet-DestinationMyFunctionfor instance if the connection context is kept at module scope.I do think that the specified bool should not be IsNullOrEmpty but simply a null check, so that an empty '' can indicate
I don't want a prefix, however I understand that is more breaking than-NoPrefixso I"m fine with that too.My current main issue pain point here is in Pester Testing, the only way I can specify my pester test commands after importing the module is to make a completely different non-prefixed manifest, or use a prefix (but then in VSCode, my GoTo references no longer work). I want to use the same non-prefixed commands that I have defined during testing without having to make an entirely separate manifest file.
JustinGrote commentedon Mar 10, 2022
I made the following workaround, the regexes are fragile but it works for my use case
iSazonov commentedon Apr 28, 2025
We rolled back PR with fix due to a regression. The code is not trivial and has low test coverage.
We need to add more tests for importing modules with/without (default) prefixes.
Prefix parameter is internally BasePrefix which is initialized with
string.Empty. So we should change this to null. But BasePrefix is used in some code branches which should be reviewed and fixed with covering by tests.JustinGrote commentedon Apr 28, 2025
Ilya (@iSazonov) thanks for the investigation!
JustinGrote commentedon Oct 27, 2025
Still Active