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

Autocomplete fails while using zsh. #534

Closed
evshary opened this issue Jun 13, 2020 · 30 comments
Closed

Autocomplete fails while using zsh. #534

evshary opened this issue Jun 13, 2020 · 30 comments
Assignees
Labels
bug Something isn't working duplicate This issue or pull request already exists

Comments

@evshary
Copy link
Contributor

evshary commented Jun 13, 2020

Bug report

Required Info:

  • Operating System:
    • Ubuntu 18.04
  • Installation type:
    • binaries
  • Version or commit hash:
    • Dashing / Eloquent
  • DDS implementation:
    • ALL
  • Client library (if applicable):
    • N/A

Steps to reproduce issue

I use zsh as my shell, but I find some problems while using <tab> to autocomplete.

  • While I type ros2 run <tab>, it works well.
  • However when I type ros2 run d<tab>, it can't show the packages which starts with 'd'.

Additional information

After some debugging, I find that the behavior is related to argcomplete library in Python 3.
If I remove the library rm -rf ~/.local/lib/python3.6/site-packages/argcomplete*, zsh works well again.
I'm not pretty sure what the root cause is.
Maybe argcomplete library does not support zsh well.

@dirk-thomas dirk-thomas added the question Further information is requested label Jun 13, 2020
@dirk-thomas
Copy link
Member

The fact that you have argcomplete under ~/.local/ indicates that you installed it via pip rather than (or in addition to) the Debian package.

Please share the version numbers of argcomplete reported by these two command:

  • pip3 freeze | grep arcomplete
  • dpkg -l | grep argcomplete

@evshary
Copy link
Contributor Author

evshary commented Jun 14, 2020

@dirk-thomas You're right. I've install argcomplete with the command python3 -m pip install -U argcomplete according to the tutorial before.

My result for the two commands:

$ pip3 freeze | grep arcomplete
# Nothing output
$ dpkg -l | grep argcomplete
ii  python3-argcomplete                                 1.8.1-1ubuntu1                                  all          bash tab completion for argparse (for Python 3)
ii  python3-colcon-argcomplete                          0.3.3-1                                         all          Completion for colcon command lines using argcomplete.

I guess you want to see the version of argcomplete under ~/.local/

$ python3 -m pip freeze | grep argcomplete
argcomplete==1.11.1
colcon-argcomplete==0.3.3

@dirk-thomas dirk-thomas added bug Something isn't working duplicate This issue or pull request already exists and removed question Further information is requested labels Jun 15, 2020
@dirk-thomas
Copy link
Member

I can reproduce the problem with Eloquent (using 5.4.2-3ubuntu3.1). In Foxy it is working though (using 5.8-3ubuntu1).

I think this is a duplicate of kislyuk/argcomplete#258. Either you use a newer version of zsh or you have to patch the function _bash_complete manually (zsh-users/zsh@e2f793e):

  • type which _bash_complete

  • copy-n-past the whole function, but insert the -1 to the following line:

    • Before: (( COMP_POINT = 1 + ${#${(j. .)words[1,CURRENT]}} + $#QIPREFIX + $#IPREFIX + $#PREFIX ))
    • After: (( COMP_POINT = 1 + ${#${(j. .)words[1,CURRENT-1]}} + $#QIPREFIX + $#IPREFIX + $#PREFIX ))

@dirk-thomas
Copy link
Member

@j-rivero to work on getting a patch upstream.

@evshary
Copy link
Contributor Author

evshary commented Jun 16, 2020

@dirk-thomas Thank you for the suggestion. The patch works perfectly!!!
It frustrated me a long time about the argcomplete.
You can close the issue if you think it's OK.

@dirk-thomas
Copy link
Member

dirk-thomas commented Jun 16, 2020

You can close the issue if you think it's OK.

We will keep it open until it is tracked upstream.

@ferrolho
Copy link

Hi! I have just installed ROS 2 (Galactic) for the first time on my machine (Ubuntu 20.04). I am also a user of ZSH, and for some reason the autocomplete isn't working; but it works on the Bash. Some more info:

~ zsh --version
zsh 5.8 (x86_64-ubuntu-linux-gnu)

➜  ~ which _bash_complete
_bash_complete () {
	local ret=1 
	local -a suf matches
	local -x COMP_POINT COMP_CWORD
	local -a COMP_WORDS COMPREPLY BASH_VERSINFO
	local -x COMP_LINE="$words" 
	local -A savejobstates savejobtexts
	(( COMP_POINT = 1 + ${#${(j. .)words[1,CURRENT-1]}} + $#QIPREFIX + $#IPREFIX + $#PREFIX ))
	(( COMP_CWORD = CURRENT - 1))
	COMP_WORDS=($words) 
	BASH_VERSINFO=(2 05b 0 1 release) 
	savejobstates=(${(kv)jobstates}) 
	savejobtexts=(${(kv)jobtexts}) 
	[[ ${argv[${argv[(I)nospace]:-0}-1]} = -o ]] && suf=(-S '') 
	matches=(${(f)"$(compgen $@ -- ${words[CURRENT]})"}) 
	if [[ -n $matches ]]
	then
		if [[ ${argv[${argv[(I)filenames]:-0}-1]} = -o ]]
		then
			compset -P '*/' && matches=(${matches##*/}) 
			compset -S '/*' && matches=(${matches%%/*}) 
			compadd -Q -f "${suf[@]}" -a matches && ret=0 
		else
			compadd -Q "${suf[@]}" -a matches && ret=0 
		fi
	fi
	if (( ret ))
	then
		if [[ ${argv[${argv[(I)default]:-0}-1]} = -o ]]
		then
			_default "${suf[@]}" && ret=0 
		elif [[ ${argv[${argv[(I)dirnames]:-0}-1]} = -o ]]
		then
			_directories "${suf[@]}" && ret=0 
		fi
	fi
	return ret
}

@evshary
Copy link
Contributor Author

evshary commented Sep 22, 2021

@ferrolho I've tried the same patch in Galactic, and autocomplete doesn't work.
I'm not really sure what happens, but maybe you can temporarily use Foxy instead.

@paolotherocker
Copy link

Hi, I'm having the same problem as @ferrolho with zsh version 5.8, and with both Foxy and Galaxy I'm unable to tab complete anything with the ros2 command.

I will add that zsh can tab complete parameters with any command except ros2.
For example apt se<tab> will produce apt search, but ros2 lau<tab> will not complete to ros2 launch

@CALMorACT
Copy link

I meet the problem also both Foxy and Galactic. The patch didn’t work either. also zsh is 5.8,

@RalphCodesTheInternet
Copy link

RalphCodesTheInternet commented Oct 26, 2021

Having the same issue with ZSH and ROS2 Galactic.

Previously used ROS Noetic and tab auto-complete worked perfectly

@chenjunnn
Copy link

chenjunnn commented Nov 2, 2021

Having the same issue with Zsh and ROS2 Galactic too.

But I found a solution in https://kislyuk.github.io/argcomplete/#zsh-support

By adding these lines to .zshrc the argcompletion is succesfully enabled.

# argcomplete for ros2 & colcon
eval "$(register-python-argcomplete3 ros2)"
eval "$(register-python-argcomplete3 colcon)"

(Thanks to #534 (comment), I removed the unnessecary autoload -U bashcompinit \ bashcompinit)

image

@Shiyu-Liu
Copy link

Shiyu-Liu commented Nov 2, 2021

I have also been struggling with this argcomplete issue with ZSH and ROS2 Galactic for a long while. Thanks for the constructive suggestions in sink0918's comment. Actually I found that it's working by adding simply this in .zshrc:
eval "$(register-python-argcomplete3 ros2)"

The arg completion for colcon was previously solved by sourcing colcon-argcomplete in .zshrc as described in colcon-Enable completion:
source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.zsh

@paolotherocker
Copy link

Having the same issue with Zsh and ROS2 Galactic too.

But I found a solution in https://kislyuk.github.io/argcomplete/#zsh-support

By adding these lines to .zshrc the argcompletion is succesfully enabled.

# argcomplete for ros2 & colcon
autoload -U bashcompinit
bashcompinit
eval "$(register-python-argcomplete3 ros2)"
eval "$(register-python-argcomplete3 colcon)"

image

That worked for me, thanks for sharing!

@clalancette
Copy link
Contributor

Huh, interesting. So it turns out that we are already calling eval "$(register-python-argcomplete ros2)" when sourcing for the zsh shell: https://github.com/ros2/ros2cli/blob/master/ros2cli/completion/ros2-argcomplete.zsh . And I verified locally that those files do indeed get sourced when source /opt/ros/rolling/setup.zsh. However, completion doesn't work anyway; you have to re-run the eval, as everyone above found out.

Actually, I found out that after sourcing /opt/ros/rolling/setup.zsh, it is enough to do:

complete -o nospace -o default -F _python_argcomplete "ros2"

That suggests to me that something else later in the sourcing process is unregistering what was already registered. Someone would need to look deeper into it to find out exactly why this isn't working as expected.

@CALMorACT
Copy link

Having the same issue with Zsh and ROS2 Galactic too.

But I found a solution in https://kislyuk.github.io/argcomplete/#zsh-support

By adding these lines to .zshrc the argcompletion is succesfully enabled.

# argcomplete for ros2 & colcon
eval "$(register-python-argcomplete3 ros2)"
eval "$(register-python-argcomplete3 colcon)"

(Thanks to #534 (comment), I removed the unnessecary autoload -U bashcompinit \ bashcompinit

image

Thank you a lot, I solve my problem by your sharing

@frankkusters
Copy link

frankkusters commented Dec 8, 2021

Instead of running eval "$(register-python-argcomplete3 ros2)" every time after sourcing setup.zsh, I made a modification in /opt/ros/galactic/share/rosidl_cli/environment/rosidl-argcomplete.zsh. I removed/commented the line:

autoload -U +X compinit && compinit

Then ros2 tab completion works. This has to do with compinit being called too many times (see also https://stackoverflow.com/questions/67136714/how-to-properly-call-compinit-and-bashcompinit-in-zsh ).

I'm not sure what a final fix should be, especially since tab completion for colcon still doesn't work with this change (the eval "$(register-python-argcomplete3 colcon)" is still needed for that).

@hardesh
Copy link

hardesh commented May 4, 2022

Hi,
Is it possible to fix this issue before the humble release? 🙈

@clalancette
Copy link
Contributor

Is it possible to fix this issue before the humble release? see_no_evil

Someone needs to debug it further, since we don't really understand the underlying cause. If you happen to find the problem and open a pull request, we're happy to review it.

@hardesh
Copy link

hardesh commented May 4, 2022

Aah got it. I don't have any previous experience with working on such an autocompletion tool. Are there any good resources to learn about this tool or any other autocompletion tool? I'd love to fix this issue. It's bugging me for a few days now
After a quick google search, I did find a few stack overflow links but they weren't useful.

TIA

Edit: Found a setup tool doc in the README. Will start from there

@frankkusters
Copy link

frankkusters commented May 4, 2022

@hardesh I think I was on to something with compinit being called too often. What I hadn't tried to figure out is why compinit is being called in rosidl-argcomplete.zsh, where else it's being called, and what would be the right (single) spot to do it.

@AndreasR30
Copy link

@frankkusters I observed the same. compinit seems to clear previous complete commands. For example in my ROS environement I get the following:

$ complete -p

complete -o nospace -o default -F _python_argcomplete rosidl
complete -o nospace -o default -F _python_argcomplete ros2
complete -o nospace -o default -F _python_argcomplete ament_index
complete _bash bash
complete -o nospace -o default -F _python_argcomplete colcon

$ compinit

$ complete -p

complete _bash bash

@AndreasR30
Copy link

For testing I removed the line autoload -U +X compinit && compinit from the following files:

/usr/share/colcon_argcomplete/hook/colcon-argcomplete.zsh
/opt/ros/humble/share/ros2cli/environment/ros2-argcomplete.zsh
/opt/ros/humble/share/ament_index_python/environment/ament_index-argcomplete.zsh
/opt/ros/humble/share/rosidl_cli/environment/rosidl-argcomplete.zsh

In order to make it work properly I have to add above line in my .zshrc before sourcing ROS workspace (and colcon-argcomplete). Whithout it it does not work. So it seems to be mandatory.

@frankkusters
Copy link

frankkusters commented Jun 22, 2022

Should autoload -U +X compinit && compinit be removed from the ROS/colcon *argcomplete.zsh files then? To me it makes sense for these commands to reside in .zshrc - it's up to the zsh user if they want completion or not. For most people it will be enabled by default anyway.

@mjbogusz
Copy link
Contributor

mjbogusz commented Jul 4, 2022

Maybe rather than removing the compinit call it should be replaced with a check and call? As far as I've tested, compinit sets the variable _comps, so it could be something like:

if (( ! ${+_comps} )); then
    autoload -U +X compinit && compinit
fi

This way neither completion script ordering nor the possible compinit call in .zshrc doesn't matter.

Edit: NVM had similar problem, but I'm not sure whether their solution works as intended - it calls compinit either way but with a flag (?), which doesn't change the fact that it clears previous completions.

@guzhaoyuan
Copy link

I am using Ubuntu 20.04 and Foxy, I have ZSH and oh-my-zsh installed.
I needed to do the following to fix this issue.

pip install argcomplete
eval "$(register-python-argcomplete ros2)"
eval "$(register-python-argcomplete colcon)"

@gbiggs
Copy link
Member

gbiggs commented Jan 13, 2023

Fixed by #750

@almeida-allan
Copy link

Having the same issue with Zsh and ROS2 Galactic too.

But I found a solution in https://kislyuk.github.io/argcomplete/#zsh-support

By adding these lines to .zshrc the argcompletion is succesfully enabled.

# argcomplete for ros2 & colcon
eval "$(register-python-argcomplete3 ros2)"
eval "$(register-python-argcomplete3 colcon)"

(Thanks to #534 (comment), I removed the unnessecary autoload -U bashcompinit \ bashcompinit)

image

Saved me! Thank you!!

jetsup added a commit to jetsup/SimpleHacks that referenced this issue Jul 4, 2024
@eduardo4jesus
Copy link

eduardo4jesus commented Sep 23, 2024

I am having an issue with this. I do have /usr/bin/register-python-argcomplete3 in my machine. However,

when using:

# argcomplete for ros2 & colcon
eval "$(/usr/bin/register-python-argcomplete3 ros2)"
eval "$(/usr/bin/register-python-argcomplete3 colcon)"

I am getting an error

/home/human/.zshrc:171: no such file or directory: /usr/bin/register-python-argcomplete3
/home/human/.zshrc:172: no such file or directory: /usr/bin/register-python-argcomplete3

Any suggestion?

@ahamboeck
Copy link

I am having an issue with this. I do have /usr/bin/register-python-argcomplete3 in my machine. However,

when using:

# argcomplete for ros2 & colcon
eval "$(/usr/bin/register-python-argcomplete3 ros2)"
eval "$(/usr/bin/register-python-argcomplete3 colcon)"

I am getting an error

/home/human/.zshrc:171: no such file or directory: /usr/bin/register-python-argcomplete3
/home/human/.zshrc:172: no such file or directory: /usr/bin/register-python-argcomplete3

Any suggestion?

This works for jazzy / ubuntu 24.04 with zsh

argcomplete for ros2 & colcon in zsh

eval "$(register-python-argcomplete ros2)"
eval "$(register-python-argcomplete colcon)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests