Skip to content
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

Get-ChildItem -Name and Get-ChildItem -Include / -Exclude unexpectedly follow symlinks with -Recurse #9126

Closed
mklement0 opened this issue Mar 12, 2019 · 7 comments
Assignees
Labels
Area-FileSystem-Provider specific to the FileSystem provider Issue-Bug Issue has been identified as a bug in the product Resolution-No Activity Issue has had no activity for 6 months or more WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module WG-Engine-Providers built-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

Comments

@mklement0
Copy link
Contributor

mklement0 commented Mar 12, 2019

PowerShell Core (unlike Windows PowerShell) by default is supposed to not follow symlinks (symbolic links) to directories during recursive traversal with Get-ChildItem -Recurse (you can opt in with -FollowSymlink).

While this generally works, using the -Name or -Include / -Exclude switches unexpectedly causes symlinks to be followed even without the -FollowSymlink switch.

Note: On Windows, the tests currently fail categorically, due to an unrelated bug: #9127

Steps to reproduce (on Unix)

Run the following Pester tests:

Describe "Get-ChildItem: symlink-following tests" {
  BeforeAll {
    Push-Location TestDrive:/
    # Create a directory with 2 files in it. 
    $null = New-Item -Type Directory someDir
    1..2 | % { $_ > "someDir/$_.txt" }
    # Create a symlink to that directory, as a sibling.
    New-Item -Type SymbolicLink someDirLink -Target someDir
  }
  AfterAll {
    Pop-Location
  }
  
  It "Get-ChildItem -Recurse doesn't follow dir. symlinks." {
    (Get-ChildItem -File -Recurse).Count | Should -Be 2
  }

  It "Get-ChildItem -Recurse -Path with a wildcard doesn't follow dir. symlinks." {
    (Get-ChildItem -File -Recurse -Path *).Count | Should -Be 2
  }

  It "Get-ChildItem -Recurse -Name doesn't follow dir. symlinks." {
    (Get-ChildItem -File -Recurse -Name).Count | Should -Be 2
  }

  It "Get-ChildItem -Recurse -Include doesn't follow dir. symlinks." {
    (Get-ChildItem -File -Recurse -Include *).Count | Should -Be 2
  }

  It "Get-ChildItem -Recurse -Exclude doesn't follow dir. symlinks." {
    (Get-ChildItem -File -Recurse -Exclude nosuch).Count | Should -Be 2
  }
  
}

Expected behavior

All tests should pass.

Actual behavior

The last 3 tests fail.

Environment data

PowerShell Core 6.2.0-rc.1
@mklement0 mklement0 added the Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a label Mar 12, 2019
@mklement0 mklement0 changed the title Get-ChildItem -Name and Get-ChildItem -Include / -Exclude unexpectedly follow symlinks Get-ChildItem -Name and Get-ChildItem -Include / -Exclude unexpectedly follow symlinks with -Recurse Mar 12, 2019
@ChrSchultz
Copy link

ChrSchultz commented Mar 13, 2019

on Windows all tests fails,
Describing Get-ChildItem: symlink-following tests
[-] Get-ChildItem -Recurse doesn't follow dir. symlinks. 6ms Expected 2, but got 3.
15: (Get-ChildItem -File -Recurse).Count | Should -Be 2
at <ScriptBlock>, C:\Users\ic-sc\test.ps1: line 15
[-] Get-ChildItem -Recurse -Path with a wildcard doesn't follow dir. symlinks. 5ms Expected 2, but got 3.
19: (Get-ChildItem -File -Recurse -Path *).Count | Should -Be 2 at <ScriptBlock>, C:\Users\ic-sc\test.ps1: line 19
[-] Get-ChildItem -Recurse -Name doesn't follow dir. symlinks. 7ms Expected 2, but got 3.
23: (Get-ChildItem -File -Recurse -Name).Count | Should -Be 2 at <ScriptBlock>, C:\Users\ic-sc\test.ps1: line 23
[-] Get-ChildItem -Recurse -Include doesn't follow dir. symlinks. 7ms Expected 2, but got 3.
27: (Get-ChildItem -File -Recurse -Include *).Count | Should -Be 2 at <ScriptBlock>, C:\Users\ic-sc\test.ps1: line 27
[-] Get-ChildItem -Recurse -Exclude doesn't follow dir. symlinks. 8ms Expected 2, but got 3.
31: (Get-ChildItem -File -Recurse -Exclude nosuch).Count | Should -Be 2 at <ScriptBlock>, C:\Users\ic-sc\test.ps1: line 31
Tests completed in 146ms
Tests Passed: 0, Failed: 5, Skipped: 0, Pending: 0, Inconclusive: 0

@mklement0
Copy link
Contributor Author

mklement0 commented Mar 13, 2019

@ChrSchultz: That is due to an unrelated bug, reported in #9127 - I've added this info to the OP.

@iSazonov iSazonov self-assigned this Jan 30, 2020
@iSazonov iSazonov added WG-Cmdlets-Core cmdlets in the Microsoft.PowerShell.Core module Issue-Bug Issue has been identified as a bug in the product and removed Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a labels Jan 30, 2020
@iSazonov iSazonov added Area-FileSystem-Provider specific to the FileSystem provider WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module WG-Engine-Providers built-in PowerShell providers such as FileSystem, Certificates, Registry, etc. and removed WG-Cmdlets-Core cmdlets in the Microsoft.PowerShell.Core module labels Nov 29, 2021
@Bertaz
Copy link

Bertaz commented Oct 9, 2022

By changing #L4 New-Item -Type SymbolicLink someDirLink -Target someDir into New-Item -Type SymbolicLink someDirLink -Target .\someDir the tests can ignore the unrelated bug.
That bug is specifically caused by #15161, which was closed as duplicate of the more comprehensive issue #15235. (#9127 is closed as fixed, but was only partially fixed)

Result:

Describing Get-ChildItem: symlink-following tests
  [+] Get-ChildItem -Recurse doesn't follow dir. symlinks. 5ms (4ms|2ms)
  [+] Get-ChildItem -Recurse -Path with a wildcard doesn't follow dir. symlinks. 5ms (4ms|1ms)
  [-] Get-ChildItem -Recurse -Name doesn't follow dir. symlinks. 7ms (7ms|1ms)
   Expected 2, but got 4.
   at (Get-ChildItem -File -Recurse -Name).Count | Should -Be 2, C:\pester\symFollow.Tests.ps1:23
   at <ScriptBlock>, C:\pester\symFollow.Tests.ps1:23
  [-] Get-ChildItem -Recurse -Include doesn't follow dir. symlinks. 8ms (8ms|1ms)
   Expected 2, but got 4.
   at (Get-ChildItem -File -Recurse -Include *).Count | Should -Be 2, C:\pester\symFollow.Tests.ps1:27
   at <ScriptBlock>, C:\pester\symFollow.Tests.ps1:27
  [-] Get-ChildItem -Recurse -Exclude doesn't follow dir. symlinks. 10ms (10ms|1ms)
   Expected 2, but got 4.
   at (Get-ChildItem -File -Recurse -Exclude nosuch).Count | Should -Be 2, C:\pester\symFollow.Tests.ps1:31
   at <ScriptBlock>, C:\pester\symFollow.Tests.ps1:31
Tests completed in 116ms
Tests Passed: 2, Failed: 3, Skipped: 0 NotRun: 0

Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

2 similar comments
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

@microsoft-github-policy-service microsoft-github-policy-service bot added Resolution-No Activity Issue has had no activity for 6 months or more labels Nov 16, 2023
Copy link
Contributor

This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-FileSystem-Provider specific to the FileSystem provider Issue-Bug Issue has been identified as a bug in the product Resolution-No Activity Issue has had no activity for 6 months or more WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module WG-Engine-Providers built-in PowerShell providers such as FileSystem, Certificates, Registry, etc.
Projects
None yet
Development

No branches or pull requests

4 participants