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

[Powershell] zoxide Does Not Escape Paths #312

Open
ditsuke opened this issue Dec 12, 2021 · 3 comments · Fixed by #313
Open

[Powershell] zoxide Does Not Escape Paths #312

ditsuke opened this issue Dec 12, 2021 · 3 comments · Fixed by #313

Comments

@ditsuke
Copy link
Contributor

ditsuke commented Dec 12, 2021

Problem

Powershell requires escaping special characters even if they're enclosed in single quotes. So calling Set-Location on, say 'D:\test\[foo] bar\' will fail like:

Set-Location : Cannot find path 'test/[foo] bar' because it does not exist.
At line:1 char:1
+ Set-Location 'test/[foo] bar'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (test/[foo] bar:String) [Set-Location], ItemNotFoundExcep
   tion
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

image

Solution

Path passed to Set-Location should be escaped:

image

ditsuke added a commit to ditsuke/zoxide that referenced this issue Dec 12, 2021
Adds escapes to paths used in Set-Location calls so they work as
expected even with special characters.
ditsuke added a commit to ditsuke/zoxide that referenced this issue Dec 12, 2021
Adds escapes to paths used in Set-Location calls so they work as
expected even with special characters.
@TeamDman
Copy link

TeamDman commented Jan 8, 2025

I fixed this by adjusting my profile to override the __zoxide_cd

function cd {
    z @args
}

Invoke-Expression (& { (zoxide init powershell | Out-String) })

function global:__zoxide_cd($dir, $literal) {
    # we need to override this so it doesn't pass -LiteralPath
    # https://github.com/ajeetdsouza/zoxide/blob/d99d82f141106c5c9e32fe75ec7695a43b3fcadc/templates/powershell.txt#L29C1-L46C2
    $dir = Set-Location $dir -Passthru -ErrorAction Stop
}

Without overriding the function, I got

❯ zoxide --version
zoxide 0.9.6
❯ ls
a[]b
❯ cd '.\a`[`]b\'
Set-Location:
Line |
  29 |          Set-Location -LiteralPath $dir -Passthru -ErrorAction Stop
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot find path '.\a`[`]b\' because it does not exist.

Looks like it from here at least is always passed $true so idk the consequences of changing that but it's working for now

function global:__zoxide_z {
if ($args.Length -eq 0) {
__zoxide_cd ~ $true
}
elseif ($args.Length -eq 1 -and ($args[0] -eq '-' -or $args[0] -eq '+')) {
__zoxide_cd $args[0] $false
}
elseif ($args.Length -eq 1 -and (Test-Path $args[0] -PathType Container)) {
__zoxide_cd $args[0] $true
}
else {
$result = __zoxide_pwd
if ($null -ne $result) {
$result = __zoxide_bin query --exclude $result "--" @args
}
else {
$result = __zoxide_bin query "--" @args
}
if ($LASTEXITCODE -eq 0) {
__zoxide_cd $result $true
}
}
}

@ajeetdsouza
Copy link
Owner

What is the actual name of your directory?

@TeamDman
Copy link

the problem reproduced for me using a dir named a[]b

@ajeetdsouza ajeetdsouza reopened this Jan 14, 2025
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 a pull request may close this issue.

3 participants