Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Completions don't work with ntl alias on zsh #6940

Closed
benhancock opened this issue Dec 1, 2024 · 5 comments · Fixed by #6946
Closed

Completions don't work with ntl alias on zsh #6940

benhancock opened this issue Dec 1, 2024 · 5 comments · Fixed by #6946
Labels
type: bug code to address defects in shipped code

Comments

@benhancock
Copy link
Contributor

Describe the bug

Completions work with the netlify command, but not with the ntl alias. Trying to use tab with ntl does not produce any completions.

Thanks to @alexschcom for pointing this issue out

Steps to reproduce

  1. Install completions with completion:install for zsh
  2. Type netlify bl and press tab. This should autocomplete to netlify blobs
  3. Type ntl bl and press tab. This does not autocomplete

Configuration

No response

Environment

System:
OS: macOS 15.0
CPU: (8) arm64 Apple M2
Memory: 91.16 MB / 8.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 21.6.2 - ~/.nvm/versions/node/v21.6.2/bin/node
Yarn: 1.22.21 - ~/.nvm/versions/node/v21.6.2/bin/yarn
npm: 10.2.4 - ~/.nvm/versions/node/v21.6.2/bin/npm
pnpm: 9.12.0 - ~/.nvm/versions/node/v21.6.2/bin/pnpm
npmGlobalPackages:
netlify-cli: 17.37.2

@benhancock benhancock added the type: bug code to address defects in shipped code label Dec 1, 2024
@benhancock
Copy link
Contributor Author

benhancock commented Dec 4, 2024

It appears that the reason for this is that shell completions are command-specific and do not automatically propagate to aliases without explicit configuration in either the completion script or the shell configuration.

Either of these fix the issue in zsh:

  1. Add ntl to the completion script itself (/Users/$user/.config/tabtab/netlify.zsh)
###-begin-netlify-completion-###
if type compdef &>/dev/null; then
  _netlify_completion () {
    local reply
    local si=$IFS

    IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" /Users/ben/.nvm/versions/node/v21.6.2/lib/node_modules/netlify-cli/dist/lib/completion/script.js completion -- "${words[@]}"))
    IFS=$si

    _describe 'values' reply
  }
  compdef _netlify_completion netlify ntl # Add `ntl` here <----------------
fi
###-end-netlify-completion-###
  1. Add compdef ntl=netlify to the user's shell config file:
# rest of zsh/bash/fish config ...

compdef ntl=netlify

@benhancock
Copy link
Contributor Author

benhancock commented Dec 4, 2024

It doesn't look like tabtab, the package the CLI uses for its completions, supports adding aliases for a completion script automatically.

However, we can write the completion script to add the ntl alias manually within src/commands/completion/completion.ts

This logic would work for zsh:

  // ... completion script installation

  const completionScriptPath = join(homedir(), `.config/tabtab/${parent.name()}.zsh`)

  if (fs.existsSync(completionScriptPath)) {
    let completionScript = fs.readFileSync(completionScriptPath, 'utf8')

    completionScript = completionScript.replace(
      /compdef _netlify_completion netlify/,
      'compdef _netlify_completion netlify ntl',
    )

    fs.writeFileSync(completionScriptPath, completionScript, 'utf8')
    log(`Added alias 'ntl' to completion script.`)
   
  }

 // ... rest of file

One problem with this approach is that it would require separate logic for zsh, bash, and fish, since they each have different mechanisms for defining completions

@benhancock benhancock changed the title Completions don't work with ntl alias Completions don't work with ntl alias on zsh Dec 5, 2024
@alexschcom
Copy link

  1. Add compdef ntl=netlify to the user's shell config file:

@benhancock It seems like this isn't done automatically? I reinstalled both completions as well as netlify-cli and the ntl alias would only work with completions after manually adding compdef ntl=netlify to my .zshrc file.

Sorry if I'm doing something wrong here, not super familiar with this stuff...

@benhancock
Copy link
Contributor Author

@alexschcom That’s interesting! The script isn’t supposed to add compdef ntl=netlify, since just adding ntl to the tabtab completion script (which is what this PR's script does) should do the trick. Can you show me the contents of your ~/.config/tabtab/netlify.zsh file (which is where the zsh alias is supposed to be added?)

###-begin-netlify-completion-###
if type compdef &>/dev/null; then
  _netlify_completion () {
    local reply
    local si=$IFS

    IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" /Users/ben/projects/nov-5/netlify-cli/dist/lib/completion/script.js completion -- "${words[@]}"))
    IFS=$si

    _describe 'values' reply
  }
  compdef _netlify_completion netlify ntl            #[<------------- ntl alias should be added here]
fi
###-end-netlify-completion-###

If it does contain ntl, does the normal stuff like resetting the terminal and source ~/.zshrc still not work?

If it doesn't contain ntl, can you tell me which version of the CLI you're currently using?

@alexschcom
Copy link

Nope, unfortunately not... ntl alias is present:

###-begin-netlify-completion-###
if type compdef &>/dev/null; then
  _netlify_completion () {
    local reply
    local si=$IFS

    IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" /opt/homebrew/lib/node_modules/netlify-cli/dist/lib/completion/script.js completion -- "${words[@]}"))
    IFS=$si

    _describe 'values' reply
  }
  compdef _netlify_completion netlify ntl
fi
###-end-netlify-completion-###

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants