Skip to content

Allow empty prefix string in 'Import-Module -Prefix' to override default prefix in manifest #20409

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 5 commits into from
Feb 27, 2025

Conversation

MartinGC94
Copy link
Contributor

PR Summary

Updates the check for the user specified prefix so it allows empty strings, which can then be used to override the prefix specified in a module manifest like so: Import-Module MyPrefixedModule -Prefix ""
Previously, if you provided an empty string it would be silently ignored.

PR Context

Fixes #16936

PR Checklist

@pull-request-quantifier-deprecated

This PR has 19 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +15 -4
Percentile : 7.6%

Total files changed: 2

Change summary by file extension:
.cs : +6 -4
.ps1 : +9 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Oct 8, 2023
@StevenBucher98 StevenBucher98 added the PowerShell-Docs not needed The PR was reviewed and doesn't appear to require a PowerShell Docs update label Oct 9, 2023
@SeeminglyScience SeeminglyScience added WG-Engine core PowerShell engine, interpreter, and runtime WG-NeedsReview Needs a review by the labeled Working Group labels Dec 4, 2024
@powercode powercode removed Review - Needed The PR is being reviewed WG-NeedsReview Needs a review by the labeled Working Group labels Feb 10, 2025
@powercode
Copy link
Collaborator

The engine working group reviewed this and approved the idea.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Feb 11, 2025
@iSazonov
Copy link
Collaborator

@MartinGC94 Please resolve merge conflicts.

@iSazonov

This comment was marked as outdated.

This comment was marked as outdated.

@powercode powercode added the WG-Reviewed A Working Group has reviewed this and made a recommendation label Feb 12, 2025
MartinGC94 and others added 2 commits February 14, 2025 23:06
…se.cs

Co-authored-by: Staffan Gustafsson <staffangu@outlook.com>
@iSazonov

This comment was marked as outdated.

This comment was marked as outdated.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Feb 22, 2025
@iSazonov iSazonov self-assigned this Feb 27, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label Feb 27, 2025
Co-authored-by: Ilya <darpa@yandex.ru>
@iSazonov

This comment was marked as outdated.

This comment was marked as outdated.

@iSazonov iSazonov changed the title Allow empty prefix string in Import-Module to override default prefix in manifest Allow empty prefix string in 'Import-Module -Prefix' to override default prefix in manifest Feb 27, 2025
@iSazonov iSazonov enabled auto-merge (squash) February 27, 2025 16:58
@iSazonov iSazonov merged commit b8ed5be into PowerShell:master Feb 27, 2025
39 of 41 checks passed
@JustinGrote
Copy link
Contributor

@iSazonov @MartinGC94 thank you!

@MartinGC94 MartinGC94 deleted the PrefixOverrideFix branch February 27, 2025 17:47
@wi-fr
Copy link

wi-fr commented Apr 19, 2025

Starting in 7.6.0-preview.4 a default command prefix to be exported from a module gets ignored. At least we can override the default prefix using the Import-Module’s -Prefix parameter. This unexpected behaviour might be related to the merging of this pull request.

@MartinGC94
Copy link
Contributor Author

@wi-fr Can you elaborate on what the unexpected behavior is? If I create a module like this:

$ModuleDir = "$HOME\TestModule1"
$null = New-Item $ModuleDir -ItemType Directory -Force
New-ModuleManifest -Path "$ModuleDir\TestModule1.psd1" -RootModule TestModule1 -DefaultCommandPrefix Pref
Set-Content -Path "$ModuleDir\TestModule1.psm1" -Value 'function Get-Demo (){"Test"}'

Then if I run Import-Module "$HOME\TestModule1" I get the "Pref" prefix as expected and the command must be called with: Get-PrefDemo. If I run Import-Module "$HOME\TestModule1" -Prefix "" the prefix is removed so it must be called like: Get-Demo. All of this is working exactly as I'd expect.

@iSazonov
Copy link
Collaborator

iSazonov commented Apr 19, 2025

@MartinGC94 It seems we skipped that BasePrefix is initialized with string.Empty in line 120.

@totkeks
Copy link

totkeks commented Apr 20, 2025

Starting in 7.6.0-preview.4 a default command prefix to be exported from a module gets ignored. At least we can override the default prefix using the Import-Module’s -Prefix parameter. This unexpected behaviour might be related to the merging of this pull request.

Got the same issue since the update and was wondering what is going on. I didn't see a bug report opened though.

@totkeks
Copy link

totkeks commented Apr 20, 2025

Had a quick look at the code:

string resolvedCommandPrefix = BasePrefix ?? defaultCommandPrefix ?? string.Empty;

This is not a good replacement for (!string.IsNullOrEmpty(this.BasePrefix)) since it misses the string.Empty case, which BasePrefix is initialized with.

So, I'd suggest either not initialize it with string.Empty and deal with the fallout everywhere BasePrefix is used or add the empty check back.

@iSazonov
Copy link
Collaborator

iSazonov commented Apr 21, 2025

@totkeks Please open new issue with repro steps.

So, I'd suggest either not initialize it with string.Empty and deal with the fallout everywhere BasePrefix is used or add the empty check back.

BasePrefix is used in another code branches. So it is more safe to revert to IsNullOrEmpty check. Although it is necessary to check that other code branches will work correctly.

MartinGC94 added a commit to MartinGC94/PowerShell that referenced this pull request Apr 27, 2025
iSazonov pushed a commit that referenced this pull request Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small PowerShell-Docs not needed The PR was reviewed and doesn't appear to require a PowerShell Docs update WG-Engine core PowerShell engine, interpreter, and runtime WG-Reviewed A Working Group has reviewed this and made a recommendation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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