ajeetdsouza / zoxide

A smarter cd command. Supports all major shells.
MIT License
20.29k stars 518 forks source link

Zoxide's init-script generated for nu-shell fails with error upon sourcing #808

Closed IlyaVassyutovich closed 2 months ago

IlyaVassyutovich commented 2 months ago

How to reproduce

Have a nu-shell

> nu --version
0.92.2

Build zoxide-init script

> zoxide init nushell | save -f ~/.zoxide.nu

Add zoxide-init script to nu.config:

# this is inside `$nu.config-path`
source ~/.zoxide.nu

Start new instance of the shell

Error: nu::shell::cannot_pass_list_to_external

  × Lists are not automatically spread when calling external commands
    ╭─[C:\Users\vassyutovich\.zoxide.nu:26:20]
 25 │ # Jump to a directory using only keywords.
 26 │ def-env __zoxide_z [...rest:string] {
    ·                    ────────┬───────
    ·                            ╰── Spread operator (...) is necessary to spread lists
 27 │   let arg0 = ($rest | append '~').0
    ╰────
  help: Either convert the list to a string or use the spread operator, like so: ...[...rest:string]

Misc

I'm not yet proficient with nu-language, but it seems like spread operator with "rest"-argumets does not work as intended with def-env.

ajeetdsouza commented 2 months ago

You may need to upgrade zoxide - Nushell has not reached v1.0 yet, and makes a lot of backward-incompatible changes every release.

Also, you appear to be generating the source file manually, unlike the instructions in the README. If you update zoxide, the Nushell script may change, causing your config to break.

mrjackphil commented 1 month ago

@IlyaVassyutovich I had the same issue. Zoxide generates init nu-script which doesn't work.

Tested in nushell 0.92.2

To make it work I:

  1. Commented the line zoxide init nushell | save -f ~/.zoxide.nu in nu-env file to prevent script regeneration.
  2. Changed ~/zoxide.nu to
    
    # Code generated by zoxide. DO NOT EDIT.

=============================================================================

#

Hook configuration for zoxide.

#

Initialize hook to add new entries to the database.

if (not ($env | default false zoxide_hooked | get __zoxide_hooked)) { $env.zoxide_hooked = true $env.config = ($env | default {} config).config $env.config = ($env.config | default {} hooks) $env.config = ($env.config | update hooks ($env.config.hooks | default {} env_change)) $env.config = ($env.config | update hooks.env_change ($env.config.hooks.env_change | default [] PWD)) $env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.envchange.PWD | append {|, dir| zoxide add -- $dir })) }

=============================================================================

#

When using zoxide with --no-cmd, alias these internal functions as desired.

#

Jump to a directory using only keywords.

def --env --wrapped __zoxide_z [...rest:string] { let arg0 = ($rest | append '~').0 let path = if (($rest | length) <= 1) and ($arg0 == '-' or ($arg0 | path expand | path type) == dir) { $arg0 } else { (zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n") } cd $path }

Jump to a directory using interactive search.

def --env --wrapped __zoxide_zi [...rest:string] { cd $'(zoxide query --interactive -- ...$rest | str trim -r -c "\n")' }

=============================================================================

#

Commands for zoxide. Disable these using --no-cmd.

#

alias cd = __zoxide_z alias cdi = __zoxide_zi

=============================================================================

#

Add this to your env file (find it by running $nu.env-path in Nushell):

#

zoxide init nushell | save -f ~/.zoxide.nu

#

Now, add this to the end of your config file (find it by running

$nu.config-path in Nushell):

#

source ~/.zoxide.nu

#

Note: zoxide only supports Nushell v0.73.0 and above.


3. ???
4. PROFIT

So, mainly changes were:
1. Changing `def-env` to `def --env --wrapped`
2. Adding spread to `$rest` variable
3. I've made alias to `cd` and `cdi` - you may don't want to do that.
ajeetdsouza commented 1 month ago

@mrjackphil did you try upgrading zoxide?

mrjackphil commented 1 month ago

I just today installed the zoxide and tried to run it. So I was sure that I'm using the latest version. But...

I installed it using chocolatey. Current version of zoxide in there is 0.9.2. Latest is 0.9.4. I've updated zoxide manually and it seems to resolve the problem.

So, thank you @ajeetdsouza for reply and sorry for oversight.