GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(complete)!: remove the vendored bash-completion copy (#1176) · jdx/usage@1e8c3d4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .github/workflows
    • test.yml
  • NOTICE.md
  • README.md
  • cli
    • assets
      • completions
        • usage.bash
      • fig.ts
      • usage.1
    • src/cli/generate
      • completion.rs
    • tests
      • shell_completions_integration.rs
    • usage.usage.kdl
  • docs/cli
    • completions.md
    • reference
      • commands.json
      • generate
        • completion.md
  • lib
    • Cargo.toml
    • bash-completion
      • COPYING
      • README.md
      • bash_completion
    • src/complete
      • bash.rs
      • fish.rs
      • mod.rs
      • nu.rs
      • powershell.rs
      • snapshots
        • usage__complete__bash__tests__complete_bash-2.snap
        • usage__complete__bash__tests__complete_bash-3.snap
        • usage__complete__bash__tests__complete_bash.snap
      • zsh.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.github/workflows/test.yml‎

Copy file name to clipboardExpand all lines: .github/workflows/test.yml
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,21 @@ jobs:
4343
# timeout: with one, apt gives up on the stalled mirror in seconds and moves to the
4444
# next entry in `/etc/apt/apt-mirrors.txt`, which is the archive.ubuntu.com that was
4545
# answering fine throughout.
46-
- name: Install shells for completion integration tests
46+
#
47+
# `bash-completion` is in the list for the same reason: usage no longer embeds a copy of
48+
# it, so the generated bash completion needs the system one loaded, and
49+
# `bash_completion_or_skip` panics under CI rather than skipping without it. The image
50+
# already ships it; naming it here is what keeps that true if a future one stops.
51+
#
52+
# Deliberately the distro package rather than a newer build from source: `ubuntu-latest`
53+
# is 24.04, whose 2.11 is the oldest version the generated script supports. Testing
54+
# against the floor is the point — 26.04 ships 2.16, which would pass either way.
55+
- name: Install shells and bash-completion for completion integration tests
4756
timeout-minutes: 5
4857
run: |
4958
sudo sed -i 's|http://azure.archive.ubuntu.com/ubuntu|https://archive.ubuntu.com/ubuntu|g' /etc/apt/apt-mirrors.txt
5059
sudo apt-get -o Acquire::Retries=3 -o Acquire::http::Timeout=15 -o Acquire::https::Timeout=15 update
51-
sudo apt-get -o Acquire::Retries=3 -o Acquire::http::Timeout=15 -o Acquire::https::Timeout=15 install -y zsh fish
60+
sudo apt-get -o Acquire::Retries=3 -o Acquire::http::Timeout=15 -o Acquire::https::Timeout=15 install -y zsh fish bash-completion
5261
# pwsh is pre-installed on GitHub ubuntu-latest images. Self-heal
5362
# if a future image drops it so the integration test still runs
5463
# (rather than panicking under CI=1).
Collapse file

‎NOTICE.md‎

Copy file name to clipboardExpand all lines: NOTICE.md
-20Lines changed: 0 additions & 20 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,6 @@ SOFTWARE.
4949
The Apache-2.0 option is available upstream at
5050
<https://github.com/clap-rs/clap/blob/master/LICENSE-APACHE>.
5151

52-
## bash-completion
53-
54-
[`lib/bash-completion/bash_completion`](lib/bash-completion/bash_completion) is a
55-
verbatim copy of the `bash_completion` script from
56-
[scop/bash-completion](https://github.com/scop/bash-completion) (version 2.15.0).
57-
`lib/src/complete/bash.rs` embeds it with `include_str!` and emits it when
58-
`usage generate completion bash --include-bash-completion-lib` is used, so the
59-
script is redistributed both in the `usage-lib` crate and in any binary built
60-
from it.
61-
62-
**bash-completion is licensed under the GNU General Public License, version 2 or
63-
later** — not MIT. The full license text ships alongside the script in
64-
[lib/bash-completion/COPYING](lib/bash-completion/COPYING), and the upstream
65-
copyright header is preserved at the top of the script itself:
66-
67-
```
68-
Copyright © 2006-2008, Ian Macdonald <[email protected]>
69-
© 2009-2020, Bash Completion Maintainers
70-
```
71-
7252
## clap adopter probes
7353

7454
`benches/shadows/external-*` and `conformance/tests/external_clap_adopters.rs`
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+1-4Lines changed: 1 addition & 4 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,4 @@ license is reproduced in [NOTICE.md](NOTICE.md).
2828

2929
## License
3030

31-
[MIT](LICENSE). Third-party notices — including the GPL-2.0-or-later
32-
[bash-completion](https://github.com/scop/bash-completion) script that
33-
`usage generate completion bash --include-bash-completion-lib` emits — are in
34-
[NOTICE.md](NOTICE.md).
31+
[MIT](LICENSE). Third-party notices are in [NOTICE.md](NOTICE.md).
Collapse file

‎cli/assets/completions/usage.bash‎

Copy file name to clipboardExpand all lines: cli/assets/completions/usage.bash
+12-4Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
# @generated by usage-cli from usage spec
2+
# Requires bash-completion (https://github.com/scop/bash-completion), which must be
3+
# sourced before this script.
24
_usage() {
35
if ! type -P usage &> /dev/null; then
46
echo >&2
57
echo "Error: usage CLI not found. This is required for completions to work in usage." >&2
68
echo "See https://usage.jdx.dev for more information." >&2
79
return 1
810
fi
11+
if ! declare -F _init_completion > /dev/null 2>&1; then
12+
echo >&2
13+
echo "Error: bash-completion is required for usage completions but was not loaded." >&2
14+
echo "Install it from your package manager and source it before this script." >&2
15+
return 1
16+
fi
917

10-
local cur prev words cword was_split comp_args
11-
_comp_initialize -n : -- "$@" || return
18+
local cur prev words cword comp_args
19+
_init_completion -n : -- "$@" || return
1220
local spec_dir="${XDG_CACHE_HOME:-$HOME/.cache}/usage"
1321
[[ -d "$spec_dir" ]] || mkdir -p -m 700 "$spec_dir"
1422
local spec_file="$spec_dir/usage__usage_spec_usage.spec"
1523
command usage --usage-spec >| "$spec_file"
1624
# shellcheck disable=SC2207
17-
_comp_compgen -- -W "$(command usage complete-word --shell bash -f "$spec_file" --cword="$cword" -- "${words[@]}")"
18-
_comp_ltrim_colon_completions "$cur"
25+
COMPREPLY=($(compgen -W "$(command usage complete-word --shell bash -f "$spec_file" --cword="$cword" -- "${words[@]}")" -- "$cur"))
26+
__ltrim_colon_completions "$cur"
1927
# shellcheck disable=SC2181
2028
if [[ $? -ne 0 ]]; then
2129
unset COMPREPLY
Collapse file

‎cli/assets/fig.ts‎

Copy file name to clipboardExpand all lines: cli/assets/fig.ts
-5Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,6 @@ const completionSpec: Fig.Spec = {
250250
name: "cache_key",
251251
},
252252
},
253-
{
254-
name: "--include-bash-completion-lib",
255-
description: "Include https://github.com/scop/bash-completion",
256-
isRepeatable: false,
257-
},
258253
{
259254
name: "--usage-bin",
260255
description:
Collapse file

‎cli/assets/usage.1‎

Copy file name to clipboardExpand all lines: cli/assets/usage.1
-5Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,6 @@ A .usage.kdl spec file to use for generating completions, use "\-" to read from
216216
\fB\-\-cache\-key\fR \fI<CACHE_KEY>\fR
217217
A cache key to use for storing the results of calling the CLI with \-\-usage\-cmd
218218
.TP
219-
\fB\-\-include\-bash\-completion\-lib\fR
220-
Include https://github.com/scop/bash\-completion
221-
222-
This is required for usage completions to work in bash, but the user may already provide it
223-
.TP
224219
\fB\-\-usage\-bin\fR \fI<USAGE_BIN>\fR
225220
Override the bin used for calling back to usage\-cli
226221

Collapse file

‎cli/src/cli/generate/completion.rs‎

Copy file name to clipboardExpand all lines: cli/src/cli/generate/completion.rs
-7Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ pub struct Completion {
2424
#[usage(long, requires = "--usage-cmd")]
2525
cache_key: Option<String>,
2626

27-
/// Include https://github.com/scop/bash-completion
28-
///
29-
/// This is required for usage completions to work in bash, but the user may already provide it
30-
#[usage(long, verbatim_doc_comment)]
31-
include_bash_completion_lib: bool,
32-
3327
/// Override the bin used for calling back to usage-cli
3428
///
3529
/// You may need to set this if you have a different bin named "usage"
@@ -61,7 +55,6 @@ impl Completion {
6155
cache_key: self.cache_key.clone(),
6256
spec,
6357
usage_cmd: self.usage_cmd.clone(),
64-
include_bash_completion_lib: self.include_bash_completion_lib,
6558
source_file: self.file.as_ref().map(|f| {
6659
if f.as_os_str() == "-" {
6760
"stdin".to_string()
Collapse file

‎cli/tests/shell_completions_integration.rs‎

Copy file name to clipboardExpand all lines: cli/tests/shell_completions_integration.rs
+143-7Lines changed: 143 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,82 @@ fn skip_if_shell_missing(shell: &str) -> bool {
100100
true
101101
}
102102

103+
/// The system bash-completion library, or `None` if it isn't installed.
104+
///
105+
/// usage no longer embeds a copy of bash-completion, so the generated bash completion needs
106+
/// the real one loaded before it — `_init_completion` and `__ltrim_colon_completions` come from
107+
/// there.
108+
///
109+
/// Candidates are the library itself, never the `profile.d/bash_completion.sh` snippet that
110+
/// Homebrew and others also install: that one is guarded on `$PS1` and so does nothing at all
111+
/// in a non-interactive shell, which is the only kind these tests run. Sourcing it would look
112+
/// like success and then fail the completion assertions. `BASH_COMPLETION_USER_DIR` is not
113+
/// consulted either — it holds per-command completions, not the library.
114+
///
115+
/// `USAGE_TEST_BASH_COMPLETION` names a library directly, for testing against a copy that is
116+
/// not installed system-wide.
117+
fn bash_completion_candidates() -> Vec<PathBuf> {
118+
let mut candidates: Vec<PathBuf> = Vec::new();
119+
if let Some(path) = env::var_os("USAGE_TEST_BASH_COMPLETION") {
120+
candidates.push(PathBuf::from(path));
121+
}
122+
// Whichever Homebrew is installed: `/opt/homebrew` on Apple Silicon, `/usr/local` on Intel,
123+
// and anywhere else if the user relocated it.
124+
if let Some(prefix) = env::var_os("HOMEBREW_PREFIX") {
125+
candidates.push(PathBuf::from(prefix).join("share/bash-completion/bash_completion"));
126+
}
127+
candidates.extend(
128+
[
129+
"/usr/share/bash-completion/bash_completion",
130+
"/usr/local/share/bash-completion/bash_completion",
131+
"/opt/homebrew/share/bash-completion/bash_completion",
132+
]
133+
.into_iter()
134+
.map(PathBuf::from),
135+
);
136+
candidates
137+
}
138+
139+
/// Returns the first candidate that a non-interactive bash can actually load `_init_completion`
140+
/// from.
141+
///
142+
/// Existing on disk is not the property the test needs — being sourceable into the shell the
143+
/// test spawns is. Probing for it means a wrapper, a version too old to define the function, or
144+
/// a path guess that turns out wrong is rejected here rather than surfacing as a puzzling
145+
/// completion assertion failure further down.
146+
fn system_bash_completion() -> Option<PathBuf> {
147+
bash_completion_candidates().into_iter().find(|candidate| {
148+
candidate.is_file()
149+
&& Command::new(shell_program("bash"))
150+
.args(["-c", "source \"$1\" && declare -F _init_completion", "--"])
151+
.arg(sh_path(candidate))
152+
.output()
153+
.is_ok_and(|out| out.status.success())
154+
})
155+
}
156+
157+
/// Returns `Some(path)` to the system bash-completion, or `None` if the test should be skipped.
158+
///
159+
/// Panics under `CI` for the same reason [`skip_if_shell_missing`] does: bash-completion is
160+
/// installed by the workflow, so a run that quietly skipped this test would be reporting a
161+
/// green bash suite that never exercised a completion.
162+
///
163+
/// What CI finds is `ubuntu-latest`'s package, which is 2.11 — deliberately the oldest version
164+
/// the generated script claims to support, so the claim is tested rather than asserted.
165+
fn bash_completion_or_skip() -> Option<PathBuf> {
166+
if let Some(path) = system_bash_completion() {
167+
return Some(path);
168+
}
169+
if env::var("CI").is_ok_and(|v| !v.is_empty()) {
170+
panic!(
171+
"no usable bash-completion but CI is set — refusing to skip. Tried: {:?}",
172+
bash_completion_candidates()
173+
);
174+
}
175+
eprintln!("Skipping bash completion test - no usable bash-completion library found");
176+
None
177+
}
178+
103179
fn shell_can_run_a_script(shell: &str) -> bool {
104180
static NEXT: AtomicUsize = AtomicUsize::new(0);
105181
let dir = env::temp_dir().join(format!(
@@ -369,6 +445,9 @@ fn test_bash_completion_integration() {
369445
if skip_if_shell_missing("bash") {
370446
return;
371447
}
448+
let Some(bash_completion) = bash_completion_or_skip() else {
449+
return;
450+
};
372451

373452
// Build the usage binary
374453
let usage_bin = build_usage_binary();
@@ -389,12 +468,10 @@ cmd "sub" help="Subcommand" {
389468
let spec_kdl_file = temp_dir.join("testcli.kdl");
390469
fs::write(&spec_kdl_file, spec).unwrap();
391470

392-
// Generate the completion with bash-completion library included
393471
let output = Command::new(&usage_bin)
394472
.args(["generate", "completion", "bash", "testcli"])
395473
.arg("-f")
396474
.arg(spec_kdl_file.to_str().unwrap())
397-
.arg("--include-bash-completion-lib")
398475
.output()
399476
.expect("Failed to generate bash completion");
400477

@@ -424,7 +501,8 @@ set +e
424501
# Add usage binary to PATH
425502
export PATH="{}:$PATH"
426503
427-
# Source our completion (which includes bash-completion library)
504+
# bash-completion first: the generated completion calls its functions.
505+
source {}
428506
source {}
429507
430508
echo "LOAD_SUCCESS"
@@ -519,6 +597,7 @@ fi
519597
echo "COMPLETION_TEST_DONE"
520598
"#,
521599
path_var_entry("bash", usage_bin.parent().unwrap()),
600+
sh_path(&bash_completion),
522601
sh_path(&comp_file),
523602
sh_path(&temp_dir)
524603
);
@@ -1576,6 +1655,64 @@ echo "GUARD_EXIT=$?"
15761655
let _ = fs::remove_dir_all(&temp_dir);
15771656
}
15781657

1658+
#[test]
1659+
fn test_bash_guard_reports_missing_bash_completion() {
1660+
if skip_if_shell_missing("bash") {
1661+
return;
1662+
}
1663+
1664+
// usage no longer ships a copy of bash-completion, so "it isn't installed" is a
1665+
// reachable state for anyone sourcing a generated completion. It has to say so
1666+
// rather than fail as `_init_completion: command not found`.
1667+
let usage_bin = build_usage_binary();
1668+
let temp_dir = env::temp_dir().join(format!("usage_bash_nobc_test_{}", std::process::id()));
1669+
fs::create_dir_all(&temp_dir).unwrap();
1670+
1671+
let output = Command::new(&usage_bin)
1672+
.args(["generate", "completion", "bash", "testcli"])
1673+
.args(["--usage-cmd", "command usage --usage-spec"])
1674+
.output()
1675+
.expect("Failed to generate bash completion");
1676+
let comp_file = temp_dir.join("testcli.bash");
1677+
fs::write(&comp_file, &output.stdout).unwrap();
1678+
1679+
// A non-interactive bash loads no bash-completion of its own, so sourcing only the
1680+
// generated script is exactly the state being tested.
1681+
let test_script = format!(
1682+
r#"#!/usr/bin/env bash
1683+
export PATH="{path}:$PATH"
1684+
source "{comp}"
1685+
_testcli testcli "" testcli 1
1686+
echo "GUARD_EXIT=$?"
1687+
"#,
1688+
path = path_var_entry("bash", usage_bin.parent().unwrap()),
1689+
comp = sh_path(&comp_file),
1690+
);
1691+
let script_file = temp_dir.join("test.sh");
1692+
fs::write(&script_file, &test_script).unwrap();
1693+
1694+
let result = script_command("bash", &script_file)
1695+
.output()
1696+
.expect("Failed to run bash test");
1697+
let stdout = String::from_utf8_lossy(&result.stdout);
1698+
let stderr = String::from_utf8_lossy(&result.stderr);
1699+
1700+
assert!(
1701+
stdout.contains("GUARD_EXIT=1"),
1702+
"guard should return 1 when bash-completion is not loaded.\nstdout:\n{stdout}\nstderr:\n{stderr}"
1703+
);
1704+
assert!(
1705+
stderr.contains("bash-completion is required"),
1706+
"guard should name bash-completion.\nstdout:\n{stdout}\nstderr:\n{stderr}"
1707+
);
1708+
assert!(
1709+
!stderr.contains("_init_completion: command not found"),
1710+
"the guard should fire before bash reports a missing function.\nstderr:\n{stderr}"
1711+
);
1712+
1713+
let _ = fs::remove_dir_all(&temp_dir);
1714+
}
1715+
15791716
#[test]
15801717
fn test_bash_self_completion_uses_executable_not_shadowing_function() {
15811718
if skip_if_shell_missing("bash") {
@@ -1588,16 +1725,15 @@ fn test_bash_self_completion_uses_executable_not_shadowing_function() {
15881725

15891726
// The real usage binary is on $PATH *and* a `usage` function is defined,
15901727
// so the guard passes legitimately — only the spec call can go wrong.
1591-
// bash-completion isn't loaded here; stub the three helpers so the spec
1728+
// bash-completion isn't loaded here; stub the helpers it provides so the spec
15921729
// write is the only thing under test.
15931730
let test_script = format!(
15941731
r#"#!/usr/bin/env bash
15951732
export PATH="{usage_dir}:$PATH"
15961733
export XDG_CACHE_HOME="{cache}"
15971734
usage() {{ echo "FUNCTION_MARKER"; }}
1598-
_comp_initialize() {{ return 0; }}
1599-
_comp_compgen() {{ return 0; }}
1600-
_comp_ltrim_colon_completions() {{ return 0; }}
1735+
_init_completion() {{ return 0; }}
1736+
__ltrim_colon_completions() {{ return 0; }}
16011737
source "{asset}"
16021738
_usage usage "" usage 1
16031739
echo "SPEC_BEGIN"
Collapse file

‎cli/usage.usage.kdl‎

Copy file name to clipboardExpand all lines: cli/usage.usage.kdl
-3Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ cmd generate help="Generate completions, documentation, and other artifacts from
6868
flag --cache-key help="A cache key to use for storing the results of calling the CLI with --usage-cmd" requires=--usage-cmd {
6969
arg <CACHE_KEY>
7070
}
71-
flag --include-bash-completion-lib help="Include https://github.com/scop/bash-completion" {
72-
long_help "Include https://github.com/scop/bash-completion\n\nThis is required for usage completions to work in bash, but the user may already provide it"
73-
}
7471
flag --usage-bin help="Override the bin used for calling back to usage-cli" env=JDX_USAGE_BIN default=usage {
7572
long_help "Override the bin used for calling back to usage-cli\n\nYou may need to set this if you have a different bin named \"usage\""
7673
arg <USAGE_BIN>
Collapse file

‎docs/cli/completions.md‎

Copy file name to clipboardExpand all lines: docs/cli/completions.md
+8Lines changed: 8 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ source ~/.bash_completions/mycli.bash
5353
mycli --<TAB>
5454
```
5555

56+
Generated bash completions call
57+
[bash-completion](https://github.com/scop/bash-completion)'s helper functions, so it
58+
must be installed and sourced before the generated script. Install it from your
59+
package manager (`apt install bash-completion`, `brew install bash-completion@2`, …);
60+
a generated completion that cannot find it says so instead of failing silently.
61+
Version 2.11 and newer are supported, which covers every current distribution
62+
release.
63+
5664
zsh:
5765

5866
```bash

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.