-
Notifications
You must be signed in to change notification settings - Fork 384
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
'PROCESS' is implicitly aliasing 'Get-PROCESS' #1402
Comments
I'm running into the same issue. I am working on a project where I am coding multiple advanced functions as separate .ps1 files. I have not isolated the triggering event but I most often notice it after I save a the file I'll see the number of errors listed go from zero to some number greater than zero. I now first go look at the process{} block of the function and I will find that it has been renamed to Get-Process. Sometimes when I go to fix the change it will revert back as I am typing. In these cases I need to close the editor window and re-open it and then make the correction. PSScriptAnalyzer 1.18.3 I experienced the same behavior when I switched to PowerShell stable version and disabled all my extensions. |
@RichMath thanks for opening this issue! The AST version comes from PowerShell so it would be helpful if you and @rjkgithub could please provide A repro script where this issue is observed would also be very helpful. Thanks! |
I cannot reproduce using PowerShell 5.1 or 7 on Windows using the following example. Please specify the example that triggers it for you and supply more info about your info such as PS and OS version function Get-Noun {
process {
}
} |
A script that exhibits this problem is attached to this reply. Windows version: PS C:\Users\richm> $PSVersionTable Name Value PSVersion 5.1.18362.145 |
I included all the above in my reply to SydneyhSmith. |
@RichMath Using your supplied Can you please run |
I think I found the problem. I wasn't able to repro the problem today . . . but I'd made a change to the code last night. Before that change the two "enums" in the script were placed before the "begin{" block. PSScriptAnalyzer did NOT flag that as an error, but Powershell did. The change I made was to move the "begin {" to include those two enums, and that made the "Process" problem disappear. |
Thanks, I can reproduce now with your new example (using PowerShell Core, not a PowerShell version issue). A much simpler repro is (and the cause seems to be the enum) function Import-Cut{
[CmdletBinding()]
param()
enum foo {
bar
baz
}
process {
}
} Although the enum is the cause of it, the implicit To conclude, the red herring caused by |
Sorry I was away for 2 days. I read through the comments and I have some additional information to what you requested. $PSVersionTable PSVersion 5.1.18362.145 My example does not have an enum definition in the code so while it may be a trigger it is not the root cause. I have attached one file where this happens for me but I have about 20 of these files that exhibit this behavior. Each file (.ps1) contains a single advanced function with CmdletBinding that are packaged in a module that serves as an API wrapper. The basic format of all the files is: function Verb-Noun {
[CmdletBinding()]
param ()
begin {}
process {}
end {}
} |
As I stated my problem seemed to be random and I was able to narrow the scope of the triggers. The automatic change to replace what is seen as an alias (I know it's a setting I can disable in VSCode) happens whenever I save the file I am working on and there is a very specific type of error in the block above the process block. The below code has such an error (missing assignment operator in a hashtable) and when I would save this file it would do the replace. function Test-Script{
[CmdletBinding()]
[OutputType([hashtable])]
param ()
begin {
$foobar = @{
foo foo
}
}
process {
$foobar}
end {}
} This sample also has an error and triggers the rule: function Test-Script{
[CmdletBinding()]
[OutputType([hashtable])]
param ()
begin {
$foobar = @{
foo = foo{
}
}
process {
$foobar}
end {}
} In fact I tried several tests and any configuration that resulted in triggering "MissingEndCurlyBrace" just before the process block also triggered the "PSAvoidUsingCmdletAliases" rule. It looks like there is some pattern matching on this rule that is tripping up on the open and close curly brackets that expects no only a matched pair but that there is an operator (maybe more characters) in between them as well. Which is why the enum before the process block triggers but also any hashtable that is missing an operator. That is why the example below, while still containing an error (missing right side of operator in hashtable, does not trigger the "PSAvoidUsingCmdletAliases" rule. function Test-Script{
[CmdletBinding()]
[OutputType([hashtable])]
param ()
begin {
$foobar = @{
foo =
}
}
process {
$foobar}
end {}
} Currently this is tagged as "Area-Rules" but I think the rule is OK and it is the code that enumerates the commands in the script that has a bug. |
I've been also experiencing this bug. |
I've been trying to find the issue where I commented on this earlier, but I just want to point out that the constraints of this bug are narrower than I think the original issue and #1402 (comment) imply. PSScriptAnalyzer uses the AST that the PowerShell parser returns when a script is parsed, and the parser defines the structure and therefore the meaning of tokens in the script according to its rules. There's no pattern matching or anything like that involved; PSScriptAnalyzer just processes the structure provided to it by the parser, based on whatever the script input was. There are a couple of scenarios here, so I'll start with the most explicit: function Test
{
process { "Hi" }
enum T { One; Two }
} Trying to execute this in PowerShell (i.e. I'm not running PSScriptAnalyzer) results in a parser error:
This is because a PowerShell function or filter either implicitly has a single block (the So how does the parser know whether we're in a named block? Well, if we're in a function/filter definition and no statements have been seen yet, we check the next token to see if it's a block name keyword. If it is, we have explicit blocks. If it's not we don't. Once the parser has identified that explicit blocks are being used, no other statements are allowed directly within the function/filter definition. So if you put function Test
{
enum T { One; Two }
process { "Hello" }
} Now executing this function definition in PowerShell succeeds, but let's try running it:
This has given us a cryptic error (because this can be a valid usage in PowerShell), but really exposes a bug in our code. The tokens inside the function body comprise a statement (one defining an enum), meaning the parser sees an implicit So, then the remaining issue is that when there's an existing syntax error in the script, the parser doesn't correctly repair the structure of the error and incorrectly classifies the
I won't close this issue just yet, since I've seen this case crop up a few times and perhaps there's something we can do about the specific case of |
Thinking about this again. I agree that the parser is at 'fault' (not saying we should do anything there). But having said that, I think we can also agree that there is no case where one can actually run I suggest:
|
@KUTlime As per above discussion, your syntax around |
@bergmeister I see, thx for clarification. |
Using version 1.18.3:
In a .psm1 file using a "process" block, this diagnostic is produced:
'PROCESS' is implicitly aliasing 'Get-PROCESS' because it
is missing the 'Get-' prefix. This can introduce possible
problems and make scripts hard to maintain. Please consider
changing command to its full name.
The text was updated successfully, but these errors were encountered: