Skip to content
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

Code Formatter: Don't remove module names from cmdlet calls #5011

Open
2 tasks done
markdomansky opened this issue Jun 28, 2024 · 4 comments
Open
2 tasks done

Code Formatter: Don't remove module names from cmdlet calls #5011

markdomansky opened this issue Jun 28, 2024 · 4 comments
Labels
Area-Code Formatting Issue-Enhancement A feature request (enhancement).

Comments

@markdomansky
Copy link

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.

Summary

Currently, with the code formatter, if you have a cmdlet call such as MicrosoftTeams\Get-CsLisCivicAddress, and run the formatter, it will remove the module name resulting in Get-CsLisCivicAddress. Including the module name gives specificity with clobbered cmdlets, as in my case. Get-CsLisCivicAddress is a valid function in two different modules.

As I understand it, including the module name in functions/modules is actually a best practice.

I've been through all the code formatting settings and none seem to relate to the above experience, including 'Auto Correct Aliases'. Disabling this did not change the behavior.

Proposed Design

At a minimum, I would like an option to enable/disable this, perhaps "Auto Remove Module Name from Cmdlets".

Even better would be expanding this to automatically add the module name to cmdlets when possible. So writing Get-CsLisCivicAddress would be converted to MicrosoftTeams\Get-CsLisCivicAddress. The option could be a drop down with Remove/Do Nothing/Add.

@markdomansky markdomansky added Issue-Enhancement A feature request (enhancement). Needs: Triage Maintainer attention needed! labels Jun 28, 2024
@JustinGrote
Copy link
Collaborator

Thanks for the submission! Most of the "refactors" for the code formatter are actually currently provided by PSScriptAnalyzer, can you verify that this isn't specific to PSScriptAnalyzer? If it is then it needs to be handled there.

@JustinGrote JustinGrote added Area-Code Formatting and removed Needs: Triage Maintainer attention needed! labels Jun 29, 2024
@markdomansky
Copy link
Author

I'm unable to reproduce with Invoke-ScriptAnalyzer -path test.ps1 (with or without -fix). It doesn't change the file and doesn't throw any output.

VSCode also does not list it as a Problem in the UI but only fixes it during formatting.

@JustinGrote
Copy link
Collaborator

JustinGrote commented Jul 2, 2024

So I can't reproduce using either "Format Document", "Format on Paste", or "Format on Save"

$x = Microsoft.PowerShell.Management\Get-Service -Name 'wuauserv'
$x.status

This does not get shortened to Get-Service

Can you provide a reproducible example? Maybe record a gif using something like ScreenToGif?

EDIT: Apologies, it appears your use case is when there is a specific clobber conflict. My guess is what's happening is the normal module resolve is happening and not realizing you have two cmdlets for whatever reason, so I agree this should defer to the fully qualified and have a option checkbox for whether to preserve fully qualified or shorten if possible. I haven't looked at the code that actually does this work, maybe @andyleejordan or @SeeminglyScience have more input.

@markdomansky
Copy link
Author

markdomansky commented Jul 2, 2024

Here's the screen cap. As it shows, the only module that I can find affected is MicrosoftTeams (though I certainly haven't tested all modules). It also shows PSSA is working with the Problems tab pulled up.

To be clear, this isn't specifically a clobbering issue, that was just how I encountered it. In this case, the Get-CsOnlineLisCivicAddress cmdlet only exists in one module (though I have 2 versions, only 1 is loaded. Pester and PowerShellGet both have multiple versions but are not affected).

I honestly have no idea why just the MicrosoftTeams module is affected. I don't have any snippets or anything I'm aware of that I've done that would cause this.

Repro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Code Formatting Issue-Enhancement A feature request (enhancement).
Projects
None yet
Development

No branches or pull requests

2 participants