Skip to content

Import-Module -Prefix '' on a module with a Default Prefix defined does not remove the prefix #16936

Open

Description

Prerequisites

Steps to reproduce

  1. Make a module, and set Prefix to 'Test'
  2. Import-Module MyTestModule -Prefix ''
  3. All commands will still have test prefix

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.

Expected behavior

Commands without prefix

Actual behavior

Commands have default prefix

Error details

No response

Environment data

PS 7.2.1

Visuals

No response

Activity

  1. iSazonov commented on Mar 2, 2022

    @iSazonov
    Collaborator

    As alternative for discussion - introduce new parameter (-NoPrefix/-RemovePrefix).

    Also question for discussion - is the prefix a strong contact from module author?

  2. JustinGrote commented on Mar 9, 2022

    @JustinGrote
    ContributorAuthor

    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.

  3. JustinGrote commented on Mar 10, 2022

    @JustinGrote
    ContributorAuthor

    I made the following workaround, the regexes are fragile but it works for my use case

    function Import-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 unique
      New-Item -ItemType File -Force -Path $tempPath -Value $manifestContent | Out-Null
    
      Write-Verbose "Importing module $path without prefix from $tempPath"
      Import-Module $tempPath -Force:$Force -PassThru
    }
  4. iSazonov commented on Apr 28, 2025

    @iSazonov
    Collaborator

    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.

  5. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    and removed
    In-PRIndicates that a PR is out for the issue
    on Apr 28, 2025
  6. JustinGrote commented on Apr 28, 2025

    @JustinGrote
    ContributorAuthor

    Ilya (@iSazonov) thanks for the investigation!

  7. JustinGrote commented on Oct 27, 2025

    @JustinGrote
    ContributorAuthor

    Still Active

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugKeepOpenThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions