Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,11 @@ protected override void ProcessRecord()
{
foreach (string filename in expandedPaths)
{
if (Directory.Exists(filename))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I actually think we should only ignore directories if they're among the paths that result from wildcard resolution.

If you specify an effectively literal directory path - whether via -LiteralPath or via -Path and a path that happens not to contain (unescaped) wildcard characters - you should continue to get an error (though arguably a better one than currently).

This will let users know that specifying directories is not supported. With this fix as-is, If you do something like Select-String foo ., you'll get no output and no error, which could be misinterpreted as the string not having been found in any files in the directory.

I think the behavior should be analogous to the following Get-ChildItem behavior:

Get-ChildItem -Directory -LiteralPath /NoSuchDir  # error
Get-ChildItem -Directory -Path /NoSuchDir  # error too, because the path contains no (unescaped) wildcard chars.
Get-ChildItem -Directory -Path /NoSuchDir*  # NO error, because the wildcard expression matched nothing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. We can reactivate #4820 and get the behavior fixed. @iSazonov what do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now Select-String behavior is the same as Unblock-File. There may be other examples (?)
So I believe it is another issue.
I agree that we could fix this with a "breaking change" symptom. If @mklement0 would done the analysis (what cmdlets should we fix?), I'd have solved the Issue.

{
continue;
}

bool foundMatch = ProcessFile(filename);
if (_quiet && foundMatch)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

$fileNameWithDots = $fileName.FullName.Replace("\", "\.\")

$subDirName = Join-Path $TestDrive 'selectSubDir'
New-Item -Path $subDirName -ItemType Directory -Force > $null
$pathWithoutWildcard = $TestDrive
$pathWithWildcard = Join-Path $TestDrive '*'

$tempFile = New-TemporaryFile
"abc" | Out-File -LiteralPath $tempFile.fullname
"bcd" | Out-File -LiteralPath $tempFile.fullname -Append
Expand All @@ -24,6 +29,14 @@
Pop-Location
}

It "Select-String does not throw on subdirectory (path without wildcard)" {
{ select-string -Path $pathWithoutWildcard "noExists" -ErrorAction Stop } | Should Not Throw
}

It "Select-String does not throw on subdirectory (path with wildcard)" {
{ select-string -Path $pathWithWildcard "noExists" -ErrorAction Stop } | Should Not Throw
}

It "LiteralPath with relative path" {
(select-string -LiteralPath (Get-Item -LiteralPath $fileName).Name "b").count | Should Be 2
}
Expand Down