-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Makefile: add "shell-completion" target #5770
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #5770 +/- ##
=======================================
Coverage 59.42% 59.42%
=======================================
Files 347 347
Lines 29402 29402
=======================================
Hits 17472 17472
Misses 10958 10958
Partials 972 972 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's reason for this change instead of doing the build in completion
still (since it still depends on binary
? For docker/docker-ce-packaging#1150, couldn't we run the completion
target during the build too?
Overall LGTM :')
Yeah I was going back and forth a bit; so for docker/docker-ce-packaging#1150 we would need the "build, but don't install" bit, and the The alternative I was considering was to change Either that, or a Happy too hear your thoughts! I opened it as draft because I was still making my mind up 😅 |
Makefile
Outdated
/etc/bash_completion.d/docker: ## generate and install the bash-completion script | ||
mkdir -p /etc/bash_completion.d | ||
docker completion bash > /etc/bash_completion.d/docker | ||
install -D -p -m 0644 ./build/completion/bash/docker /etc/bash_completion.d/docker |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rabbit hole time!! I noticed here we're using /etc/bash_completion.d/
, but that our deb
packages use /usr/share/bash-completion/completions/
. StackOverflow had a nice summary; https://unix.stackexchange.com/a/605051/78656, but also linked to the docs;
In bash-completion >= 2.12, we search the data directory of bash-completion under the installation prefix where the target command is installed. When one can assume that the version of the target bash-completion is 2.12 or higher, the completion script can actually be installed to
$PREFIX/share/bash-completion/completions/
under the same installation prefix as the target program installed under$PREFIX/bin/
or$PREFIX/sbin/
. For the detailed search order, see also "Q. What is the search order for the completion file of each target command?" below.
But here's the fun bits; the new locations were "planning to use this in 2.12 and up" scop/bash-completion@530017d
But Debian 12 (bookworm);
Operating System: Debian GNU/Linux 12 (bookworm)
Has;
apt-cache madison bash-completion
bash-completion | 1:2.11-6 | mirror+file:/etc/apt/mirrors/debian.list bookworm/main amd64 Packages
bash-completion | 1:2.11-6 | mirror+file:/etc/apt/mirrors/debian.list bookworm/main Sources
But the deb packages (through dh_bash-completion
), install in the "new" location (recommended for 2.12 and up);
ls -l /usr/share/bash-completion/completions/docker
-rw-r--r-- 1 root root 114566 Jan 22 13:39 /usr/share/bash-completion/completions/docker
...not the old location;
ls -l /etc/bash_completion.d/
total 4
-rw-r--r-- 1 root root 439 Jun 16 2024 git-prompt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So for these we could consider this make target to either be;
- Install where the deb/rpm packages would install them
- User-override (
BASH_COMPLETION_USER_DIR
orXDG_DATA_DIRS
) to prevent overwriting existing ones installed from a deb/rpm package.
Q. What is the search order for the completion file of each target command?
A. The completion files of commands are looked up by the shell function
__load_completion
. Here, the search order in bash-completion >= 2.12 is
explained.
BASH_COMPLETION_USER_DIR
. The subdirectorycompletions
of each paths
inBASH_COMPLETION_USER_DIR
separated by colons is considered for a
completion directory.- The location of the main
bash_completion
file. The subdirectory
completions
in the same directory asbash_completion
is considered.- The location of the target command. When the real location of the command
is in the directory<prefix>/bin
or<prefix>/sbin
, the directory
<prefix>/share/bash-completion/completions
is considered.XDG_DATA_DIRS
(or the system directories/usr/local/share:/usr/share
if empty). The subdirectorybash-completion/completions
of each paths
inXDG_DATA_DIRS
separated by colons is considered.The completion files of the name
<cmd>
or<cmd>.bash
, where<cmd>
is
the name of the target command, are searched in the above completion
directories in order. The file that is found first is used. When no
completion file is found in any completion directories in this process, the
completion files of the name_<cmd>
is next searched in the completion
directories in order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/usr
is 100% arguably the "correct" location for these (per FHS), and supported since scop/bash-completion@c89dcbb, which is bash-completion
version 2.2 (hence why Debian uses it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
THANK YOU! I overlooked that it was just moving existing logic, and probably just the docs missing here.
I'll update, and add a note "for future self" to consider using user-specific override paths, but that's a whole other can of worms (XDG, other shells ...)
923aa63
to
485bc7d
Compare
@laurazard @tianon updated; I pushed an extra commit to split the "generating" from the "(dyn)binary" targets. I can squash the commits if this approach looks better to you. |
Makefile
Outdated
# requires either "binary" or "dynbinary" to be built. | ||
# but only build if it's not there, to prevent existing binaries | ||
# from being replaced. | ||
@ [ -f build/docker ] || $(MAKE) binary | ||
@ ./scripts/build/completion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slightly hacky here; the make binary
and make dynbinary
targets overwrite the build/binary
file; I'm trying to avoid cases where either a Dynamic or a Static binary was built, and generating the completion-scripts now replacing the binary.
Makefile
Outdated
.PHONY: shell-completion | ||
shell-completion: ## generate shell-completion scripts | ||
# requires either "binary" or "dynbinary" to be built. | ||
# but only build if it's not there, to prevent existing binaries | ||
# from being replaced. | ||
@ [ -f build/docker ] || $(MAKE) binary | ||
@ ./scripts/build/completion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about something like:
.PHONY: shell-completion | |
shell-completion: ## generate shell-completion scripts | |
# requires either "binary" or "dynbinary" to be built. | |
# but only build if it's not there, to prevent existing binaries | |
# from being replaced. | |
@ [ -f build/docker ] || $(MAKE) binary | |
@ ./scripts/build/completion | |
build/docker: | |
echo "Run 'make binary' or 'make dynbinary' first" && exit 1 | |
.PHONY: shell-completion | |
shell-completion: build/docker ## generate shell-completion scripts | |
# requires either "binary" or "dynbinary" to be built. | |
# but only build if it's not there, to prevent existing binaries | |
# from being replaced. | |
@ ./scripts/build/completion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh! Good suggestion; I think I tried that but something failed for some reason; let me actually try that again, because it was late, and maybe I tried the wrong thing.
6e49d17
to
4d7d513
Compare
4d7d513
to
4cb758e
Compare
I guess we should also add some check for macOS, which has a different (BSD flavor)
That's an existing issue though, and the |
@vvoland I took a variant of your suggestion; PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
4cb758e
to
fafbff6
Compare
Add a target to build the (cobra) generated completion and store them inside build/completions. Signed-off-by: Sebastiaan van Stijn <[email protected]>
fafbff6
to
6ab9b92
Compare
Makefile: add "shell-completion" target
Add a target to build the (cobra) generated completion and store
them inside build/completions.
- What I did
- How I did it
- How to verify it
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)