PowerShell / PSResourceGet

PSResourceGet is the package manager for PowerShell
https://www.powershellgallery.com/packages/Microsoft.PowerShell.PSResourceGet
MIT License
491 stars 94 forks source link

Find/Get-PSResource does not write error when it cannot find resource #699

Closed ThomasNieto closed 1 year ago

ThomasNieto commented 2 years ago

Prerequisites

Steps to reproduce

If you use Find-PSResource or Get-PSResource on a non-existent resource no error will be output.

Expected behavior

Error is written

Actual behavior

No error

Error details

N/A

Environment data

Name          Version
----          -------
PowerShellGet 3.0.14

Name                           Value
----                           -----
PSVersion                      7.2.5
PSEdition                      Core
GitCommitId                    7.2.5
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

SydneyhSmith commented 2 years ago

Thanks @ThomasNieto this was by design, so we are discussing whether this should be added... we are wondering if a warning might be more appropriate since the input is valid

ThomasNieto commented 2 years ago

@SydneyhSmith why was this design chosen? This is bad design in my opinion and has inconsistent behavior when compared to core cmdlets. Warnings are insufficient because they are difficult to automate against since they are text messages not a rich object with an error/exception and target object. Lets take Get-Item for example. If the user tries to get a non-existent item it will write a non-terminating error. As the user or scripter I can then make decisions based on the error exception, fully qualfied error id, target object or any other property. If I desire I can then catch only the ItemNotFoundException or PathNotFound and then do something with the TargetObject while leaving any other potential errors go up the call stack.

TLDR: If I tell the command to do something and it cannot for whatever reason it should write an error.

PS C:\> Get-Item -Path c:\doesnotexist.txt
Get-Item: Cannot find path 'C:\doesnotexist.txt' because it does not exist.

PS C:\> Get-Error

Exception             :
    Type                 : System.Management.Automation.ItemNotFoundException
    ErrorRecord          :
        Exception             :
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : Cannot find path 'C:\doesnotexist.txt' because it does not exist.
            HResult : -2146233087
        TargetObject          : C:\doesnotexist.txt
        CategoryInfo          : ObjectNotFound: (C:\doesnotexist.txt:String) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : PathNotFound
    ItemName             : C:\doesnotexist.txt
    SessionStateCategory : Drive
    TargetSite           :
        Name          : ExpandMshGlobPath
        DeclaringType : System.Management.Automation.LocationGlobber, System.Management.Automation, Version=7.2.5.500,
Culture=neutral, PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message              : Cannot find path 'C:\doesnotexist.txt' because it does not exist.
    Source               : System.Management.Automation
    HResult              : -2146233087
    StackTrace           :
   at System.Management.Automation.LocationGlobber.ExpandMshGlobPath(String path, Boolean allowNonexistingPaths,
PSDriveInfo drive, ContainerCmdletProvider provider, CmdletProviderContext context)
   at System.Management.Automation.LocationGlobber.ResolveDriveQualifiedPath(String path, CmdletProviderContext
context, Boolean allowNonexistingPaths, CmdletProvider& providerInstance)
   at System.Management.Automation.LocationGlobber.GetGlobbedMonadPathsFromMonadPath(String path, Boolean
allowNonexistingPaths, CmdletProviderContext context, CmdletProvider& providerInstance)
   at System.Management.Automation.LocationGlobber.GetGlobbedProviderPathsFromMonadPath(String path, Boolean
allowNonexistingPaths, CmdletProviderContext context, ProviderInfo& provider, CmdletProvider& providerInstance)
   at System.Management.Automation.SessionStateInternal.GetItem(String[] paths, CmdletProviderContext context)
   at System.Management.Automation.ItemCmdletProviderIntrinsics.Get(String path, CmdletProviderContext context)
   at Microsoft.PowerShell.Commands.GetItemCommand.ProcessRecord()
TargetObject          : C:\doesnotexist.txt
CategoryInfo          : ObjectNotFound: (C:\doesnotexist.txt:String) [Get-Item], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
InvocationInfo        :
    MyCommand        : Get-Item
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 19
    Line             : Get-Item -Path c:\doesnotexist.txt, c:\temp
    PositionMessage  : At line:1 char:1
                       + Get-Item -Path c:\doesnotexist.txt, c:\temp
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Get-Item
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
SydneyhSmith commented 1 year ago

Looks like find returns an error but the error message should be more clear, get still doesn't return anything

PS /Users/sysmith2/Downloads/PowerShellGet-interface> Find-PSResource fakepackage -Repository PSGallery
Find-PSResource: Package with search criteria: Name fakepackage could not be found due to: Response didn't contain properties element.
PS /Users/sysmith2/Downloads/PowerShellGet-interface> Get-PSResource -name fakePackage