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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fix sourcing of completion path for Git Bash #801

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

svlandeg
Copy link
Collaborator

@svlandeg svlandeg commented Apr 19, 2024

My system

When running

typer --install-completion

on my Windows system with Git Bash, Typer fetches Path.home() which is C:\Users\Sofie. This is correct in itself, but Git Bash can't work directly with paths that contain backslashes. After installation, .bashrc contains

source C:\Users\Sofie\.bash_completions\typer.sh

and after rebooting the terminal, it will complain:

bash: C:UsersSofie.bash_completionstyper.sh: No such file or directory

To get this to work with Git Bash, .bashrc should instead contain

source 'C:\Users\Sofie\.bash_completions\typer.sh'

and then it works - no error upon starting the console, and the completion feature works.

TODO

This PR needs to be carefully checked on a "normal" Bash system to double check that the single quote doesn't harm any other environments. I wouldn't think so, but I'm not 100% sure.

@svlandeg svlandeg added bug Something isn't working autocompletion Autocompletion functionality os / windows Related to Windows repo / install Installation issues or improvements p2 labels Apr 19, 2024
@@ -100,13 +100,13 @@ def install_bash(*, prog_name: str, complete_var: str, shell: str) -> Path:
# It seems bash-completion is the official completion system for bash:
# Ref: https://www.gnu.org/software/bash/manual/html_node/A-Programmable-Completion-Example.html
# But installing in the locations from the docs doesn't seem to have effect
completion_path = Path.home() / f".bash_completions/{prog_name}.sh"
completion_path = Path.home() / ".bash_completions" / f"{prog_name}.sh"
Copy link
Collaborator Author

@svlandeg svlandeg Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this particular fix is not required to solve the issue on my end (in fact, it makes no difference). I just think it's safer to always use the Pathlib separator / instead of the literal "/".

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It really makes no difference because Pathlib perfectly understands / in paths 馃槉 it's designed to be cross-platform

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I think you're right - it makes no difference because Pathlib will deal with the literal string correctly, too. Honestly I'd still prefer the edit for esthetic reasons (part of the same path is using /, the other part is using "/") but I don't feel strongly. Will leave it up to Tiangolo - and either way as I said this is not actually related to the bug fix.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I also think it's more esthetic this way 馃槈 just pointing out it doesn't really make it any safer

rc_path = Path.home() / ".bashrc"
rc_path.parent.mkdir(parents=True, exist_ok=True)
rc_content = ""
if rc_path.is_file():
rc_content = rc_path.read_text()
completion_init_lines = [f"source {completion_path}"]
completion_init_lines = [f"source '{completion_path}'"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe to quote in posix, too.
In fact it's the right thing to do, even on posix, because the home directory might contain spaces

However, I recommend quoting using shlex.quote, which will also cover other corner cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autocompletion Autocompletion functionality bug Something isn't working os / windows Related to Windows p2 repo / install Installation issues or improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants