Skip to content

Conversation

@cdonnellytx
Copy link
Contributor

@cdonnellytx cdonnellytx commented Jan 30, 2025

By default if you pass a relative path to the .NET APIs, it will use [Environment]::CurrentDirectory as its base, not your actual PowerShell working directory.

The problem is that the two are almost never the same.

This changes it so that the entry path is resolved by PowerShell, not .NET.

Description

This resolves relative paths to absolute in PowerShell's working directory context instead of .NET's.

By default if you pass a relative path to the .NET APIs, it will use [Environment]::CurrentDirectory as its base, not your actual PowerShell working directory.

The problem is that the two are almost never the same.

This changes it so that the entry path is resolved by PowerShell, not .NET, by using PSCmdlet.GetResolvedProviderPathFromPSPath to resolve the path.

Related Issue

#19

Motivation and Context

It lets me specify relative paths to Find-Item.

How Has This Been Tested?

Ran Find-Item manually:

How Example Result
without a specified path Find-Item Works same as before
with a relative path Find-Item foo Searches $PWD\foo instead of $HOME\foo
with an absolute path Find-Item D:\github\cdonnellytx\foo Works same as before
with a PSPath Find-Item 'Microsoft.PowerShell.Core\FileSystem::D:\github\cdonnellytx\foo' Searches as if it was passed D:\github\cdonnellytx\foo

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

By default if you pass a relative path to the .NET APIs, it will use [Environment]::CurrentDirectory as its base,
not your actual PowerShell working directory.

The problem is that the two are almost never the same.

This changes it so that the entry path is resolved by PowerShell, not .NET.
@cdonnellytx
Copy link
Contributor Author

Definitely not finished yet, found a fun bug wherein if the path doesn't resolve, the Path variable is never updated, and so a failed path ends up looking in $HOME anyway.

I'll fix it and will ensure there are tests before going live with this.

PowerShell expects things to be written to Write-Error so they can be handled in a standard fashion based on the ErrorActionPreference.
@eizedev eizedev requested a review from Copilot September 5, 2025 12:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Fixes relative path handling in the Find-Item function to use PowerShell's working directory instead of .NET's Environment.CurrentDirectory, which are typically different. This allows users to pass relative paths that resolve correctly from their current PowerShell location.

  • Uses PSCmdlet.GetResolvedProviderPathFromPSPath to resolve paths in PowerShell context
  • Updates error handling to use Write-Error instead of throwing exceptions
  • Improves parameter documentation clarity

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@eizedev
Copy link
Owner

eizedev commented Sep 5, 2025

@cdonnellytx sorry, i completely missed your issue and pull request. also i didn't looked into this repository for a long time.
I will check/review, fix some bugs in the deployment process and get back to you in the next days.

@eizedev eizedev marked this pull request as ready for review September 5, 2025 15:43
…anged in newer versions)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
eizedev
eizedev previously approved these changes Sep 5, 2025
@eizedev
Copy link
Owner

eizedev commented Sep 5, 2025

@cdonnellytx

When using the provider, since -Path can include wildcards (e.g., C:\Users\*\Documents) and is expanded to multiple root paths by the provider we could get problems. So i decided to only use the first element of the array for now.
But you gave me a great idea to simply incorporate the Linux equivalent of wildcards in the path. I will release a new version soon with your change and a subsequent extension from me (including further changes that I had not yet published on Github/Gallery).

@eizedev eizedev closed this Sep 5, 2025
@eizedev eizedev reopened this Sep 5, 2025
@eizedev eizedev self-assigned this Sep 5, 2025
@eizedev eizedev merged commit 7966dc0 into eizedev:master Sep 5, 2025
6 of 9 checks passed
@cdonnellytx cdonnellytx deleted the fix/relativePathHandling branch November 24, 2025 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants