-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Change to not expose unsupported aliases/cmdlets in Unix #3595
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than removing this test, can you skip it for non-windows platforms?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea (from @lzybkr) is that we should put all alias tests in one place (Default-Aliases.Tests.ps1) rather than spread on many files.
|
@iSazonov thanks for the fix! |
f45592d to
dd16aa5
Compare
|
@daxian-dbw I am glad to fix this aliases too while the ears in this. Now I catched interensting issue with "gin" alias. Please help me understand. As for
|
9b3179f to
5546b3b
Compare
What I would do is:
For cmdlets like Basically, there are also 4 aliases that are currently exposed on unix plats by mistake. You can run the following command to observe. |
|
@daxian-dbw Thanks! I still don't understand
@lzybkr Could you please comment too? |
Yes, we can have a test for it. Basically, we need to get the list of types that shouldn't be available on different platforms, for example,
Declaration of aliases in For the legacy code, I'm inclined to keep them that way, as I'm not sure if changing them would cause some unobvious regression. For new cmdlet implementation, I think |
|
Can we create new attribute like What unobvious regression you expect with aliases? I believe that there is no functional code which we can break down, no new code. On the other hand, if we move the aliases in cmdlets ( |
Those alias declarations are publicly accessible, via So it's hard to anticipate how people will depend on them ... that's why I said unobviously regression.
A class that should be excluded from a platform may not be a cmdlet, so I don't think a new attribute will help. Not excluding a file that is supposed to be excluded on a platform won't be a big issue really. I think we can just fix it when it comes up. |
|
Clear about new attribute. I tried to add |
|
@iSazonov thanks for the extra validation exercise! Then I'm fine moving the aliases from |
|
@daxian-dbw Thanks for approve! I believe we can continue here to preserve the usefull discussion. I have already started to prepare new tests. |
|
I created new file with tests to check default alias and cmdlet lists. @daxian-dbw Please review the test. After that the way will be open for safe code edits. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"nwsn -> New-PSWorkflowSession" = $FullCLR
I don't see this alias on windows powershell ...
PS:4> Get-Alias -Definition New-PSWorkflowSession
Get-Alias : This command cannot find a matching alias because an alias with the definition 'New-PSWorkflowSession'
does not exist.
At line:1 char:1
+ Get-Alias -Definition New-PSWorkflowSession
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (New-PSWorkflowSession:String) [Get-Alias], ItemNotFoundException
+ FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand
Environment
Name Value
---- -----
PSVersion 5.1.14393.1066
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.1066
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😕 I see on 10.0.10240 and on 10.0.16179
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. It's defined in the module PSWorkflow which is not loaded by default. Let's keep it in the list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gtz is also available in windows powershell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No on 10.0.10240
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PS:3> Get-Alias gtz
CommandType Name Version Source
----------- ---- ------- ------
Alias gtz -> Get-TimeZone 3.1.0.0 Microsoft.PowerShell.Manag...
Available in 10.0.14393.1066
Name Value
---- -----
PSVersion 5.1.14393.1066
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.1066
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What version we should use as a base?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't show full list wrong aliases
doesn't show the list of different aliases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment hasn't been addressed yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove this from Default-Aliases.Tests.ps1 as the last step in the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good.
|
@iSazonov the test looks mostly good. I left a few comments. |
6f99fd4 to
8bbd6e2
Compare
|
@daxian-dbw I fixed the original list of aliases.
|
|
@daxian-dbw - Please take another look. |
|
@iSazonov sorry for coming back so late.
I agree the alias option settings there seem a bit messed up, for example, aliases for
Yep,
This can be explained by the same It turns out there are good reasons to keep alias declarations in InitialSessionState.cs. I suggest we go back to the proposal I made in #3595 (comment) |
|
@iSazonov - Please update the PR title and description to reflect the current state of the PR. This PR has slowly moved beyond the original intent and I'd like that reflected in the title and description. |
Fix 'gsv','sasv' and 'spsv' Fix 'fhx' alias Fix 'cfs' alias Fix 'gcb' and 'scb' aliases Fix 'gtz' and 'stz' aliases Fix 'gin'
8abdc86 to
880e21f
Compare
89d6933 to
113e43b
Compare
|
@mirichmo the PR title and description was updated. @daxian-dbw Thanks for great comments! I moved aliases to InitialSessionState.cs |
| /// </summary> | ||
| [Cmdlet(VerbsCommon.Get, "ComputerInfo", | ||
| HelpUri = "https://go.microsoft.com/fwlink/?LinkId=799466")] | ||
| [Alias("gin")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should move gin to InitialSessionState.cs. I think for all aliases that are declared using Alias() attribute, we should keep them that way. They are not core cmdlets, and I don't see a problem to have them with 'None' option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For those aliases that are already declared in InitialSessionState.cs, I prefer to keep them there for now, for the sake of backward compatibility.
For those aliases that are declared with AliasAttribute, I also prefer to keep them that way unless it's a core cmdlet that should be set with options other than 'None'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have found no other [Alias()] in the repo. So we should keep consistency and use the same approach - place aliases in InitialSessionState.cs.
Also I set ReadOnly because later we can replace it on None based on feedback but changing None to ReadOnly will be breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For those aliases that are already declared in InitialSessionState.cs, we should keep them there for backward compatibility. For new aliases, I believe they should be declared using AliasAttribute, unless there is an explicit reason to set it with an option other than None.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we ignore here performance of PowerShell Core start-up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My opinion doesn't stem from the performance concern. I just think we should no longer keep stuffing unnecessary settings to InitialSessionState.cs. Keep alias declaration in individual cmdlet is good for at least 2 reasons:
- alias declaration is together with the cmdlet definition -- easy to read/discover and the alias exists in the cmdlet as a metadata information which can be analyzed using reflection by tools (if there is any tool doing so).
- we want to stop the pattern of declaring aliases in a centralized location, so that when multiple people working on different cmdlets at the same time, they will have less conflicts that are resulted from editing the same file.
For those existing alias declarations in InitialSessionState.cs, we cannot move them out due to the ScopeItemOption settings (BTW, I think for some of the aliases there, the authors probably just copied the existing options without knowing if they should actually be applied). But for new aliases, I think it's better to have them coupled with the cmdlet unless an option other than 'none' is really desired.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are other benefits to the Alias attribute:
- AllScope is terrible for performance - there is little benefit to have most of these aliases as AllScope
- Consistency - if a non-default runspace is constructed and imports one of these modules, that runspace would not get the aliases defined by default. For example:
function DoIt($createArg)
{
[powershell]::Create($createArg).
AddScript('$ExecutionContext.InvokeCommand.GetCommand("clc", "Alias")').
Invoke()
}
DoIt ([System.Management.Automation.RunspaceMode]::NewRunspace)
DoIt ([System.Management.Automation.RunspaceMode]::CurrentRunspace)
$iss = [InitialSessionState]::Create()
$iss.ImportPSModule('Microsoft.PowerShell.Management')
$iss.LanguageMode = 'FullLanguage'
DoIt $issThis prints:
CommandType Name Version Source
----------- ---- ------- ------
Alias clc -> Clear-Content
Alias clc -> Clear-Content
Note - only 2 results instead of 3. This is a little weird because Clear-Content is in Microsoft.PowerShell.Management, so why isn't clc?
We should perhaps consider adding support for AllScope/ReadOnly in the Alias attribute, perhaps as an internal only option so that it isn't abused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daxian-dbw @lzybkr Thanks for clarify!
So a plan is:
1.In the PR make a fix as @daxian-dbw requested
2.Open new Issue (to get feedback for removing AllScope) and then new PR to add internal only option to add support for AllScope/ReadOnly in the Alias attribute and migrate (1) alias and (2) cmdlet declarations from InitialSessionState.cs to modules.
Is it Ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a plan. When creating the new issue, please make sure all our discussion here are captured in the issue. Thanks @iSazonov!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done #3796
| /// </summary> | ||
| [Cmdlet(VerbsCommon.Get, "TimeZone", DefaultParameterSetName = "Name", | ||
| HelpUri = "https://go.microsoft.com/fwlink/?LinkId=799468")] | ||
| [Alias("gtz")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same to gtz and stz in TimeZoneCommands.cs
| { | ||
| // We add aliases from custom modules with 'ScopedItemOptions.None' | ||
| // because only a module consumer (not the module author) decides | ||
| // whether the alias is read only and available in all scopes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comments actually should go to AliasAttribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the comment or copy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. This comment should be put here.
However, the comment may need a bit change, since AliasAttribute can be used in built-in module, not custom modules only. Maybe the following is a little better:
"Alias declared by AliasAttribute is set with the option 'ScopedItemOptions.None', because we believe a user of the cmdlet, instead of the author of it, should be the one to decide the option of the alias usage."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe:
"Alias declared by AliasAttribute is set with the option 'ScopedItemOptions.None', because we believe a user of the cmdlet, instead of the author of it, should be the one to decide the option ('ScopedItemOptions.ReadOnly' and/or 'ScopedItemOptions.AllScopes') of the alias usage."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good 👍
| @@ -1,4 +1,6 @@ | |||
| using System; | |||
| #if !CORECLR | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetClipboardCommand.cs and SetClipboardCommand.cs are currently not included in the build (see here). We use those excluded compile entries in csproj files as a living document to track what built-in cmdlets are not in powershell core yet. So the if/def CORECLR is not necessary in those files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
|
If we shall move alias definitions to multiple files (modules) maybe add tests in |
| /// Displays the hexidecimal equivalent of the input data. | ||
| /// </summary> | ||
| [Cmdlet(VerbsCommon.Format, "Hex", SupportsShouldProcess = true, HelpUri ="https://go.microsoft.com/fwlink/?LinkId=526919")] | ||
| [Alias ("fhx")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This alias declaration shouldn't be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry. Fixed.
| "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug", | ||
| "Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile", "Get-Verb", "Format-Hex" | ||
| FunctionsToExport= "Import-PowerShellDataFile" | ||
| AliasesToExport= "fhx" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? Currently Format-Hex cmdlet is binary. I believe it was left from the time when the cmdlet was function.
Furthermore we now control all the aliases in the tests and can safely export all aliases by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If alias is not declared in .psd1 file, using the alias when the module hasn't been loaded will not trigger the module autoloading. Try the following and you will see it:
PS:44> gmo | rmo
PS:45> gmo
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
PS:46> gin
gin : The term 'gin' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ gin
+ ~~~
+ CategoryInfo : ObjectNotFound: (gin:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the behavior different from cmdlets export?
If we move aliases from InitialSessionState.cs we should add all ones to psd1?
gcm | ? Source -eq "Microsoft.PowerShell.Management" | % {Get-Alias -Definition $_.Name -ErrorAction SilentlyContinue}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the behavior different from cmdlets export?
Not sure what you mean by "cmdlet export".
If we move aliases from InitialSessionState.cs we should add all ones to psd1?
From what I observed, it seems to be so. But it feels natural to me -- module should control what aliases to expose, and it's more discoverable if the .psd1 files always tell the complete truth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean by "cmdlet export".
Defaults is AliasesToExport="" and CmdletsToExport="". For first case the module autoload don't works, for second it works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it won't work. If you remove a cmdlet from the "CmdletsToExport" then that cmdlet will not be exposed from the module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarify! It seems I confused with the IntelliSense behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
| PowerShellVersion="3.0" | ||
| NestedModules="Microsoft.PowerShell.Commands.Management.dll" | ||
| HelpInfoURI = 'https://go.microsoft.com/fwlink/?linkid=390785' | ||
| AliasesToExport = @("gin", "gtz", "stz") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same - we now control all the aliases in the tests and can safely export all aliases by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same reason as explained above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
| "Unblock-File", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug", | ||
| "Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "Get-Verb", "Format-Hex" | ||
| FunctionsToExport= "ConvertFrom-SddlString" | ||
| AliasesToExport= "fhx" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same - we now control all the aliases in the tests and can safely export all aliases by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same reason as explained above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
| CLRVersion="4.0" | ||
| NestedModules="Microsoft.PowerShell.Commands.Management.dll" | ||
| HelpInfoURI = 'https://go.microsoft.com/fwlink/?linkid=390785' | ||
| AliasesToExport = @("gcb", "scb", "gin", "gtz", "stz") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same - we now control all the aliases in the tests and can safely export all aliases by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same reason as explained above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
| $(Get-PSBreakpoint).Id.length | Should Be ($NumberOfBreakpoints -1) | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. Shouldn't be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous test do that so the test is alias related only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#Closed
|
|
||
| $var1 | Should Be #nothing. it should be Nothing at all. | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here. Shouldn't be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous test do that so the test is alias related only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#Closed
| it "Should return an array data type when multiple matches are found" { | ||
| $result = $testinputtwo | Select-String -Pattern "hello" | ||
| ,$result | Should BeOfType "System.Array" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. Both tests shouldn't be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first - Previous test do that so the test is alias related only.
The second is not removed - it is below - diff trick 😕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#Closed
| # instead of Get-Alias and Get-Command | ||
| # but latest is more simple use. | ||
| #$iis = [initialsessionstate]::CreateDefault() | ||
| #$currentCmdletList = $iis.Commands | Where-Object { $_.CommandType -eq "Cmdlet"} | Select-Object -ExpandProperty Name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comments don't seem necessary to keep.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Removed.
| # if all aliases is Ok we output nothing | ||
| $result | Write-Host | ||
| $result | Should Be $null | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this test the same as the one in Default-Aliases.Tests.ps1? Are you planning to remove Default-Aliases.Tests.ps1 and replace it with this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll remove the duplication. I leave it for easy review and for checking new tests in DefaultCommands.Tests.ps1.
That sounds great. |
|
@iSazonov The changes look good to me, and special thanks for the new tests covering the complete list of built-in aliases/cmdlets! |
|
@daxian-dbw I removed |
|
@daxian-dbw Thanks for help and great comments! |
Fix #3577.
Make changes to not expose the aliases "gin", "gsv", "sasv" and "spsv" in Unix platforms.
Also refactored aliases related tests and added new tests covering the complete list of built-in aliases/cmdlets.