PSEverything icon indicating copy to clipboard operation
PSEverything copied to clipboard

Select-EverythingString should not pass directories to Select-String

Open nightroman opened this issue 2 years ago • 0 comments

This is not necessarily a bug but a nuisance that hopefully may be avoided with a tweak. The steps to reproduce in Windows PowerShell console, e.g. v5.1:

  • change to this repository root directory
  • invoke
Select-EverythingString Search-Everything

As a result, the command emits errors like:

Microsoft.PowerShell.Utility\Select-String : The file ...\PSEverything\.git
cannot be read: Access to the path '...\PSEverything\.git' is denied.
At line:1 char:1
+ Select-EverythingString Search-Everything
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-String], ArgumentException
    + FullyQualifiedErrorId : ProcessingFile,Microsoft.PowerShell.Commands.SelectStringCommand

Presumably this happens because the underlying Search-Everything gets and passes all file system entry paths to Select-String, including folders. Select-String does not like input directory paths in v5.1.

NOTE: The same command works without errors in PowerShell Core. Perhaps Select-String learned to ignore directory paths.

I am thinking of the fix of SelectEverythingStringCommand.cs like:

    if (searchParams.TryGetValue(nameof(Filter), out var filterValue))
    {
        searchParams[nameof(Filter)] = "file:" + filterValue;
    }
    else
    {
        searchParams.Add(nameof(Filter), "file:");
    }

With the above we tell Search-Everything to return just files, so that Select-String is happy in any PowerShell.

If you find the suggested fix reasonable and a PR is welcome I can take care of this.

nightroman avatar Jun 06 '22 21:06 nightroman