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

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

Closed
IlyaVassyutovich opened this issue May 3, 2024 · 4 comments

Comments

@IlyaVassyutovich
Copy link

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
Copy link
Owner

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
Copy link

mrjackphil commented May 8, 2024

@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.env_change.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.
  1. ???
  2. 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
Copy link
Owner

@mrjackphil did you try upgrading zoxide?

@mrjackphil
Copy link

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.

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

No branches or pull requests

3 participants