From a32157ef7cf41a8b3bd9676d1fa5e5d1c2072211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sun, 22 Jan 2023 12:57:30 +0200 Subject: [PATCH 1/4] Point to /usr/share/bash-completion/completions for system bash completions /etc/bash_completion.d works too, but the downside is that everything there is loaded eagerly at bash_completion load time, slowing down shell startup. From /usr/share/bash-completion/completions completion files are loaded on demand. This mechanism is available as of bash-completion version 2 and later, just like the ~/.local/share/bash-completion/completions one that is already suggested for user-specific completions. --- src/cli/help.rs | 3 ++- .../cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cli/help.rs b/src/cli/help.rs index 683249629c..7cf7211044 100644 --- a/src/cli/help.rs +++ b/src/cli/help.rs @@ -169,7 +169,8 @@ pub(crate) static COMPLETIONS_HELP: &str = r"Discussion: Bash: - Completion files are commonly stored in `/etc/bash_completion.d/` for + Completion files are commonly stored in + `/usr/share/bash-completion/completions` for system-wide commands, but can be stored in `~/.local/share/bash-completion/completions` for user-specific commands. Run the command: diff --git a/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml b/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml index 63ac522164..6e08f57b5c 100644 --- a/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml @@ -26,7 +26,8 @@ Discussion: Bash: - Completion files are commonly stored in `/etc/bash_completion.d/` for + Completion files are commonly stored in + `/usr/share/bash-completion/completions` for system-wide commands, but can be stored in `~/.local/share/bash-completion/completions` for user-specific commands. Run the command: From 30f5e62144d538e3fc701fed49bdc53989281b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sun, 22 Jan 2023 13:03:22 +0200 Subject: [PATCH 2/4] Rephrase system-wide vs user-specific bash completion file dir note It's not that much about whether a command is system-wide or user-specific, but more about whether the completion is. In particular, the user-specific completions file dir is used to override system-wide completion, or to add completion for a command that does not have a system-wide completion installed. This is orthogonal to whether the command is installed system-wide or user-specific. --- src/cli/help.rs | 7 +++---- .../rustup/rustup_completions_cmd_help_flag_stdout.toml | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/cli/help.rs b/src/cli/help.rs index 7cf7211044..d35c7df438 100644 --- a/src/cli/help.rs +++ b/src/cli/help.rs @@ -169,10 +169,9 @@ pub(crate) static COMPLETIONS_HELP: &str = r"Discussion: Bash: - Completion files are commonly stored in - `/usr/share/bash-completion/completions` for - system-wide commands, but can be stored in - `~/.local/share/bash-completion/completions` for user-specific commands. + System wide completion files are commonly stored in + `/usr/share/bash-completion/completions`, and user-specific ones + can be stored in `~/.local/share/bash-completion/completions`. Run the command: $ mkdir -p ~/.local/share/bash-completion/completions diff --git a/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml b/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml index 6e08f57b5c..37c9f99e35 100644 --- a/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml @@ -26,10 +26,9 @@ Discussion: Bash: - Completion files are commonly stored in - `/usr/share/bash-completion/completions` for - system-wide commands, but can be stored in - `~/.local/share/bash-completion/completions` for user-specific commands. + System wide completion files are commonly stored in + `/usr/share/bash-completion/completions`, and user-specific ones + can be stored in `~/.local/share/bash-completion/completions`. Run the command: $ mkdir -p ~/.local/share/bash-completion/completions From ece7d09c7f845f5fb502b7a2a3e17e4f07f82236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sun, 22 Jan 2023 13:07:46 +0200 Subject: [PATCH 3/4] Advise overwrite rather than append to existing bash completion files There should be no reason to append to existing ones. Other similar completion file install recipes overwrite, too. --- src/cli/help.rs | 4 ++-- .../rustup/rustup_completions_cmd_help_flag_stdout.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cli/help.rs b/src/cli/help.rs index d35c7df438..cfade1afb7 100644 --- a/src/cli/help.rs +++ b/src/cli/help.rs @@ -175,7 +175,7 @@ pub(crate) static COMPLETIONS_HELP: &str = r"Discussion: Run the command: $ mkdir -p ~/.local/share/bash-completion/completions - $ rustup completions bash >> ~/.local/share/bash-completion/completions/rustup + $ rustup completions bash > ~/.local/share/bash-completion/completions/rustup This installs the completion script. You may have to log out and log back in to your shell session for the changes to take effect. @@ -270,7 +270,7 @@ pub(crate) static COMPLETIONS_HELP: &str = r"Discussion: Bash: - $ rustup completions bash cargo >> ~/.local/share/bash-completion/completions/cargo + $ rustup completions bash cargo > ~/.local/share/bash-completion/completions/cargo Zsh: diff --git a/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml b/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml index 37c9f99e35..e172b3b6e0 100644 --- a/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml @@ -32,7 +32,7 @@ Discussion: Run the command: $ mkdir -p ~/.local/share/bash-completion/completions - $ rustup completions bash >> ~/.local/share/bash-completion/completions/rustup + $ rustup completions bash > ~/.local/share/bash-completion/completions/rustup This installs the completion script. You may have to log out and log back in to your shell session for the changes to take effect. @@ -128,7 +128,7 @@ Discussion: Bash: - $ rustup completions bash cargo >> ~/.local/share/bash-completion/completions/cargo + $ rustup completions bash cargo > ~/.local/share/bash-completion/completions/cargo Zsh: From 65058ba1e20fa2f1d6c878622a37d379d494351d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sun, 22 Jan 2023 13:18:53 +0200 Subject: [PATCH 4/4] Make "modern" bash completion install recipes more dynamic The downside of installing completions in their expanded form is that they should be re-installed on respective tool updates to keep them in sync. Generating on demand fixes that, in exchange for a few milliseconds on first load. --- doc/user-guide/src/installation/index.md | 4 ++-- src/cli/help.rs | 13 ++++++++----- .../rustup_completions_cmd_help_flag_stdout.toml | 9 ++++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/doc/user-guide/src/installation/index.md b/doc/user-guide/src/installation/index.md index d97dd3f7b0..e2fccb3fdf 100644 --- a/doc/user-guide/src/installation/index.md +++ b/doc/user-guide/src/installation/index.md @@ -73,10 +73,10 @@ simple as using one of the following: ```console # Bash -$ rustup completions bash > ~/.local/share/bash-completion/completions/rustup +$ printf 'eval -- "$("$1" completions bash)"\n' > ~/.local/share/bash-completion/completions/rustup # Bash (macOS/Homebrew) -$ rustup completions bash > $(brew --prefix)/etc/bash_completion.d/rustup.bash-completion +$ printf 'eval -- "$(rustup completions bash)"\n' > $(brew --prefix)/etc/bash_completion.d/rustup.bash-completion # Fish $ mkdir -p ~/.config/fish/completions diff --git a/src/cli/help.rs b/src/cli/help.rs index cfade1afb7..c3038800c1 100644 --- a/src/cli/help.rs +++ b/src/cli/help.rs @@ -156,7 +156,7 @@ pub(crate) static DOC_HELP: &str = r"Discussion: By default, it opens the documentation index. Use the various flags to open specific pieces of documentation."; -pub(crate) static COMPLETIONS_HELP: &str = r"Discussion: +pub(crate) static COMPLETIONS_HELP: &str = r#"Discussion: Enable tab completion for Bash, Fish, Zsh, or PowerShell The script is output on `stdout`, allowing one to re-direct the output to the file of their choosing. Where you place the file @@ -175,7 +175,8 @@ pub(crate) static COMPLETIONS_HELP: &str = r"Discussion: Run the command: $ mkdir -p ~/.local/share/bash-completion/completions - $ rustup completions bash > ~/.local/share/bash-completion/completions/rustup + $ printf 'eval -- "$("$1" completions bash)"\n' \ + > ~/.local/share/bash-completion/completions/rustup This installs the completion script. You may have to log out and log back in to your shell session for the changes to take effect. @@ -186,7 +187,8 @@ pub(crate) static COMPLETIONS_HELP: &str = r"Discussion: With the `bash-completion` brew formula installed, run the command: $ mkdir -p $(brew --prefix)/etc/bash_completion.d - $ rustup completions bash > $(brew --prefix)/etc/bash_completion.d/rustup.bash-completion + $ printf 'eval -- "$(rustup completions bash)"\n' \ + > $(brew --prefix)/etc/bash_completion.d/rustup.bash-completion Fish: @@ -270,11 +272,12 @@ pub(crate) static COMPLETIONS_HELP: &str = r"Discussion: Bash: - $ rustup completions bash cargo > ~/.local/share/bash-completion/completions/cargo + $ printf 'eval -- "$(rustup completions bash cargo)"\n' \ + > ~/.local/share/bash-completion/completions/cargo Zsh: - $ rustup completions zsh cargo > ~/.zfunc/_cargo"; + $ rustup completions zsh cargo > ~/.zfunc/_cargo"#; pub(crate) static OFFICIAL_TOOLCHAIN_ARG_HELP: &str = "Toolchain name, such as 'stable', 'nightly', \ diff --git a/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml b/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml index e172b3b6e0..cd1597010e 100644 --- a/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup/rustup_completions_cmd_help_flag_stdout.toml @@ -32,7 +32,8 @@ Discussion: Run the command: $ mkdir -p ~/.local/share/bash-completion/completions - $ rustup completions bash > ~/.local/share/bash-completion/completions/rustup + $ printf 'eval -- "$("$1" completions bash)"\\n' \\ + > ~/.local/share/bash-completion/completions/rustup This installs the completion script. You may have to log out and log back in to your shell session for the changes to take effect. @@ -43,7 +44,8 @@ Discussion: With the `bash-completion` brew formula installed, run the command: $ mkdir -p $(brew --prefix)/etc/bash_completion.d - $ rustup completions bash > $(brew --prefix)/etc/bash_completion.d/rustup.bash-completion + $ printf 'eval -- "$(rustup completions bash)"\\n' \\ + > $(brew --prefix)/etc/bash_completion.d/rustup.bash-completion Fish: @@ -128,7 +130,8 @@ Discussion: Bash: - $ rustup completions bash cargo > ~/.local/share/bash-completion/completions/cargo + $ printf 'eval -- "$(rustup completions bash cargo)"\\n' \\ + > ~/.local/share/bash-completion/completions/cargo Zsh: