Visitar URL original
Avoid extra SMB queries when formatting file names by daxian-dbw · Pull Request #28038 · PowerShell/PowerShell · GitHub
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 @@ -1953,7 +1953,10 @@ public static string NameString(PSObject instance)
{
if (instance?.BaseObject is FileSystemInfo fileInfo)
{
if (InternalSymbolicLinkLinkCodeMethods.IsReparsePointLikeSymlink(fileInfo))
// Checking the cached attributes first avoids an extra filesystem query for every non-link,
// which is particularly expensive when formatting items on an SMB share.
if (InternalSymbolicLinkLinkCodeMethods.IsReparsePoint(fileInfo)
&& InternalSymbolicLinkLinkCodeMethods.IsReparsePointLikeSymlink(fileInfo))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The IsReparsePointLikeSymlink method is used in RemoveDirectoryInfoItem(), so I guess it makes sense to insert this check there too.

@daxian-dbw Dongbo Wang (daxian-dbw) Oct 9, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's out of scope for this PR. This PR is only for fixing the formatting.

{
return $"{PSStyle.Instance.FileInfo.SymbolicLink}{fileInfo.Name}{PSStyle.Instance.Reset} -> {fileInfo.LinkTarget}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ Describe 'Formatting for FileInfo objects' -Tags 'CI' {
$dir.NameString | Should -BeExactly "$($PSStyle.FileInfo.Directory + $dir.Name + $PSStyle.Reset)"
}

It 'NameString should not query link metadata for a cached non-reparse point' -Skip:(-not $IsWindows) {
$testFile = Join-Path -Path $TestDrive -ChildPath 'cached-metadata'
$file = New-Item -ItemType File -Path $testFile
$null = $file.Attributes

# Removing the file makes an unnecessary second filesystem query observable.
[System.IO.File]::Delete($testFile)

# Without the cached-attributes check, NameString queries the deleted path for link metadata and throws.
[Microsoft.PowerShell.Commands.FileSystemProvider]::NameString($file) | Should -BeExactly $file.Name
}

It 'Executable should have correct color' {
if ($IsWindows) {
$exePath = Join-Path -Path $TestDrive -ChildPath 'myExe.exe'
Expand Down
Loading