Skip to content

Resolve-Path -RelativeBasePath is ignored unless -Relative is also specified, should imply the latter #20920

Closed

Description

Prerequisites

Steps to reproduce

The -RelativeBasePath string parameter qualifies the -Relative switch.

Currently, -RelativeBasePath is ignored if -Relative isn't also specified.

The better solution is for -RelativeBasePath to imply -Relative, analogous to how, with Get-ChildItem, -Depth implies -Recurse.

Update: Turns out that -RelativeBasePath without -Relative does have a use case, albeit only with relative input paths; see comments below.

Resolve-Path $Home -RelativeBasePath /  # -Relative not specified; should be implied

Expected behavior

E.g., on Windows:

.\Users\jdoe

Actual behavior

E.g., on Windows:

Path
----
C:\Users\jdoe

That is, -RelativeBasePath was quietly ignored.

Error details

No response

Environment data

PowerShell 7.4.0

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Dec 14, 2023
  2. changed the title [-]Resolve-Path -RelativeBasePath is ignored unless -Relative is also specified, should imply the latter[/-] [+]`Resolve-Path -RelativeBasePath` is ignored unless `-Relative` is also specified, should imply the latter[/+] on Dec 14, 2023
  3. MartinGC94 commented on Dec 14, 2023

    @MartinGC94
    Contributor

    That was the initial implementation but I changed it here: #19755 so people could resolve a relative path to a full path like this:

    PS C:\Users\Martin> Resolve-Path -Path .\System32 -RelativeBasePath C:\Windows
    
    Drive Provider                             ProviderPath        Path
    ----- --------                             ------------        ----
    C     Microsoft.PowerShell.Core\FileSystem C:\Windows\System32 C:\Windows\System32
    

    Of course it's not very useful when you are specifying a full path as the input but we can't do anything about that.

  4. mklement0 commented on Dec 14, 2023

    @mklement0
    ContributorAuthor

    Thanks, MartinGC94, I wasn't aware of this feature.

    Let me try to summarize, as the basis for discussing potential changes / amending the documentation:

    -RelativeBasePath has two distinct use cases - one with and the other without also specifying -Relative.
    In both cases, -RelativeBasePath acts as a reference path for the resolution.

    • With -Relative:

      • Returns relative paths, as strings, which describes how to get from the input paths to the reference path (which defaults to the current location), using . and .. components as needed.
      • This is equally useful for both relative and absolute input paths, but it is important to note that a relative input path is interpreted as relative to -RelativeBasePath.
      • Examples (using literal paths, but wildcards are supported, via -Path):
    # ABSOLUTE input path:
    # Get the relative path from C:\Windows to, say, C:\Users\jdoe -> '..\Users\jdoe'
    Resolve-Path $HOME -Relative -RelativeBasePath C:\Windows
    
    # RELATIVE input path: 
    # Get the relative path from C:\Windows to C:\Windows\System32  -> '.\System32'
    Resolve-Path System32 -Relative -RelativeBasePath C:\Windows
    # This is basically the equivalent of:
    #    '.\' +  (Get-Item (Join-Path C:\Windows System32)).Name
    • Without -Relative:

      • Returns absolute paths, as [System.Management.Automation.PathInfo] instances.

      • This parameter combination It is therefore only useful with relative input paths, given that there'd be nothing to resolve for absolute ones.

      • Example:

    # Get the absolute paths of items named Prog* in C:\
    # -> [System.Management.Automation.PathInfo] instances for the following full paths:
    #   'C:\Program Files'
    #   'C:\Program Files (x86)'
    Resolve-Path Prog* -RelativeBasePath C:\
    # This is the equivalent of:
    #   Resolve-Path (Join-Path C:\ Prog*)

    Potential resolutions:

    • The docs definitely need amending, at the very least to modify the useless Resolve-Path -Path $ExecutablePath -RelativeBasePath $env:TEMP example, which uses an absolute input path.

    • Given that Resolve-Path Prog* -RelativeBasePath C:\ is essentially the same as Resolve-Path (Join-Path C:\ Prog*), maybe we could have done without the former, especially given the potential for confusion around -Relative - but I guess that ship has sailed.

    • To lessen the potential for confusion, I suggest renaming -RelativeBasePath to -BasePath (and keeping the former a as an alias of the latter, for backward compatibility):

      • This avoids the expectation that -RelativeBasePath use implies -Relative (which is what prompted creation of this issue).
      • By avoiding the word Relative in the name, users will be less tempted to think that the feature relateds exclusively to the -Relative switch.
  5. mklement0 commented on Dec 15, 2023

    @mklement0
    ContributorAuthor

    So that this issue doesn't die a slow death (which may still happen to the linked feature request), I'm closing this in favor of:

  6. microsoft-github-policy-service commented on Dec 15, 2023

    @microsoft-github-policy-service
    Contributor

    📣 Hey Michael Klement (@mklement0), how did we do? We would love to hear your feedback with the link below! 🗣️

    🔗 https://aka.ms/PSRepoFeedback

  7. removed
    Needs-Triage
    on Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions