Replies: 7 comments
|
Bug still present in Typer 0.3.2 |
|
I also experienced this bug in Typer 0.15.2. It tried to install the completions into |
|
can send a fix — install_zsh writes to Path.home() / '.zshrc' unconditionally, but zsh reads $ZDOTDIR/.zshrc when ZDOTDIR is set. honoring that env var fixes it. opening a pr. |
Please, first double check whether there is already a PR open, which is extremely easy to do as it's literally linked from this page. |
|
The zdotdir bug in autocompletion is a common issue with shell completion generation. The problem is that the completion script doesn't properly escape special characters in the home directory path when ZDOTDIR contains non-ASCII characters. Fix: Always use import shlex
def generate_completion(home_dir: str) -> str:
quoted_home = shlex.quote(home_dir)
return f"export ZDOTDIR={quoted_home}\n..."Also check if the issue is in how typer reads import os
zdotdir = os.path.expanduser(os.environ.get('ZDOTDIR', '~'))
# Normalize to absolute path
zdotdir = os.path.abspath(zdotdir) |
|
For the zdotdir autocompletion bug: The issue is that zsh autocompletion reads ZDOTDIR to find the completion cache. If the path contains non-ASCII characters or special shell characters, the completion script generation breaks. Fix in typer's completion generation: import shlex
import os
def _escape_zdotdir(path: str) -> str:
# Escape special characters for safe shell use
return shlex.quote(os.path.expanduser(path))
# In completion generation:
completion_script = f"""
export ZDOTDIR={_escape_zdotdir(zdotdir_path)}
{completion_content}
"""Also normalize the path before use: zdotdir = os.path.normpath(os.path.expanduser(zdotdir or '~'))
# Verify it exists and is writable
if not os.path.isdir(zdotdir):
raise click.BadParameter(f"ZDOTDIR does not exist: {zdotdir}") |
|
For the zsh completion with special characters: The fix is to use import shlex
def _escape_for_zsh(s: str) -> str:
return shlex.quote(s)
# In completion script generation:
comp_file = f"${{ZDOTDIR:-$HOME}}/.zfunc/_{prog_name}"
# Should be:
comp_file = f"${{ZDOTDIR:-$HOME}}/.zfunc/{_escape_for_zsh(prog_name)}"And normalize paths before use: zdotdir = os.path.normpath(os.path.expanduser(os.environ.get('ZDOTDIR', '~'))) |
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
When autocompletion is created in typer, the necessary changes are made in
$HOME/.zshrc:https://github.com/tiangolo/typer/blob/a93e6b2f86a34fd82741ae837b9b188083a5e9c0/typer/completion.py#L225
However, the path to the directory where the
.zshrcfile is in, can be set by the environment variableZDOTDIR. Therefore, typer should check first if the environment variableZDOTDIRis set and valid and use it to find.zshrcand add the necessary line for auto-completion. If it is invalid or simply not set, the default path to$HOME/.zshrccan be used.To Reproduce
Expected behavior
typer should check for the
ZDOTDIRenv var and use it as base to locate.zshrcinstead of always usingHOME.All reactions