GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Exclude directories from -Path in Select-String by iSazonov · Pull Request #4829 · PowerShell/PowerShell

Conversation

@iSazonov

@iSazonov Ilya (iSazonov) commented Sep 13, 2017

Copy link
Copy Markdown
Collaborator

Fix #4820.

Select-String can search in files only so we should skip directory
objects.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@iSazonov
Exclude directories from -Path in Select-String
7e2324d
Select-String can search in files only so we should skip directory
objects.
@iSazonov Ilya (iSazonov) assigned Dongbo Wang (daxian-dbw) Sep 13, 2017
@iSazonov
Ilya (iSazonov) requested review from James Truher (JamesWTruher), Paul Higinbotham (PaulHigin) and Dan Travison (dantraMSFT) as code owners September 13, 2017 11:21
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-already-signed label Sep 13, 2017
daxian-dbw
Dongbo Wang (daxian-dbw) requested changes Sep 13, 2017
View reviewed changes
Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs Outdated
{
foreach (string filename in expandedPaths)
{
if (System.IO.Directory.Exists(filename))

@daxian-dbw Dongbo Wang (daxian-dbw) Sep 13, 2017

Copy link
Copy Markdown
Member

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 is a using directive for System.IO.Directory, so any reason to use the full type name here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@iSazonov Ilya (iSazonov) Sep 14, 2017

Copy link
Copy Markdown
Collaborator Author

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 Outdated
}

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

@daxian-dbw Dongbo Wang (daxian-dbw) Sep 13, 2017

Copy link
Copy Markdown
Member

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 current behavior is to throw a non-terminating error, so for this test, it will pass even if it actually failed. see [+] ee 297ms below

PS:122> describe "aa" { It "ee" { { Select-String -Path * hi } | should not throw } }
Describing aa
Select-String : The file F:\tmp\test\pp cannot be read: Access to the path 'F:\tmp\test\pp' is denied.
At line:1 char:29
+ describe "aa" { It "ee" { { Select-String -Path * hi } | should not t ...
+                             ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-String], ArgumentException
    + FullyQualifiedErrorId : ProcessingFile,Microsoft.PowerShell.Commands.SelectStringCommand

 [+] ee 297ms

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@dantraMSFT Dan Travison (dantraMSFT) Sep 13, 2017

Copy link
Copy Markdown
Contributor

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.

Like the above should not throw test, you should verify the expected error (ProcessingFile,Microsoft.PowerShell.Commands.SelectStringCommand)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 Outdated
}

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

@daxian-dbw Dongbo Wang (daxian-dbw) Sep 13, 2017

Copy link
Copy Markdown
Member

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 not throw might not be a good test here because it doesn't throw with the current behavior -- a non-terminating error is written out.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@dantraMSFT Dan Travison (dantraMSFT) Sep 13, 2017

Copy link
Copy Markdown
Contributor

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 think this test is fine but you should also verify the reported error (ProcessingFile,Microsoft.PowerShell.Commands.SelectStringCommand) either in the test or in another test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@iSazonov Ilya (iSazonov) Sep 14, 2017

Copy link
Copy Markdown
Collaborator Author

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 skipped '-ErrorAction Stop' - Fixed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
dantraMSFT
Dan Travison (dantraMSFT) reviewed Sep 13, 2017
View reviewed changes
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 Outdated
$fileNameWithDots = $fileName.FullName.Replace("\", "\.\")

$subDirName = Join-Path $TestDrive 'selectSubDir'
New-Item -Path $subDirName -ItemType Directory > $null

@dantraMSFT Dan Travison (dantraMSFT) Sep 13, 2017

Copy link
Copy Markdown
Contributor

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.

Suggest using -ErrorAction Stop.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@iSazonov Ilya (iSazonov) Sep 14, 2017

Copy link
Copy Markdown
Collaborator Author

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 we usualy use -Force - I added.
Please clarify about -ErrorAction Stop - I don't found such samples in our tests.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@dantraMSFT Dan Travison (dantraMSFT) Sep 14, 2017

Copy link
Copy Markdown
Contributor

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 me, its all about failure diagnosability. The test is dependent on the directory and if new-item fails, there's no point in the test continuing. Using -EA Stop ensures you get the error at the point of the failure instead of later on when it may be muddled by test or product code.
Its just a suggestion.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iSazonov reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@iSazonov
Minor fixes
f601ebf
daxian-dbw
Dongbo Wang (daxian-dbw) approved these changes Sep 14, 2017
View reviewed changes
dantraMSFT
Dan Travison (dantraMSFT) approved these changes Sep 14, 2017
View reviewed changes
@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit a4cdb80 into PowerShell:master Sep 14, 2017
@iSazonov
Ilya (iSazonov) deleted the select-string-dir branch September 15, 2017 03:04
mklement0
Michael Klement (mklement0) reviewed Sep 15, 2017
View reviewed changes
Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs
{
foreach (string filename in expandedPaths)
{
if (Directory.Exists(filename))

@mklement0 Michael Klement (mklement0) Sep 15, 2017

Copy link
Copy Markdown
Contributor

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.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@daxian-dbw Dongbo Wang (daxian-dbw) Sep 15, 2017

Copy link
Copy Markdown
Member

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@iSazonov Ilya (iSazonov) Sep 15, 2017

Copy link
Copy Markdown
Collaborator Author

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.

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 Michael Klement (@mklement0) would done the analysis (what cmdlets should we fix?), I'd have solved the Issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@iSazonov @daxian-dbw
Exclude directories discovered from '-Path' in Select-String (PowerSh…
aa5e463
…ell#4829)

Select-String can search in files only so we should skip directories.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@daxian-dbw Dongbo Wang (daxian-dbw) daxian-dbw approved these changes
@JamesWTruher James Truher (JamesWTruher) Awaiting requested review from JamesWTruher
@PaulHigin Paul Higinbotham (PaulHigin) Awaiting requested review from PaulHigin
+2 more reviewers
@mklement0 Michael Klement (mklement0) mklement0 left review comments
@dantraMSFT Dan Travison (dantraMSFT) dantraMSFT approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@daxian-dbw Dongbo Wang (daxian-dbw)

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@iSazonov @daxian-dbw @mklement0 @dantraMSFT @msftclas
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.