shell support: make which spack output intelligible (#19256)

Zsh and newer versions of bash have a builtin `which` function that will
show you if a command is actually an alias or a function. For functions,
the entire function is printed, and our `spack()` function is quite long.
Instead of printing out all that, make the `spack()` function a wrapper
around `_spack_shell_wrapper()`, and include some no-ops in the
definition so that users can see where it was created and where Spack is
installed.

Here's what the new output looks like in zsh:

```console
$ which spack
spack () {
	: this is a shell function from: /Users/gamblin2/src/spack/share/spack/setup-env.sh
	: the real spack script is here: /Users/gamblin2/src/spack/bin/spack
	_spack "$@"
	return $?
}
```

Note that `:` is a no-op in Bourne shell; it just discards anything after
it on the line. We use it here to embed paths in the function definition
(as comments are stripped).
This commit is contained in:
Todd Gamblin 2020-10-21 17:04:42 -07:00 committed by GitHub
parent 93e7267dcc
commit 16e75ecac0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,7 +45,8 @@ if [ -n "${_sp_initializing:-}" ]; then
fi
export _sp_initializing=true
spack() {
_spack_shell_wrapper() {
# Store LD_LIBRARY_PATH variables from spack shell function
# This is necessary because MacOS System Integrity Protection clears
# variables that affect dyld on process start.
@ -243,11 +244,6 @@ _spack_determine_shell() {
_sp_shell=$(_spack_determine_shell)
# Export spack function so it is available in subshells (only works with bash)
if [ "$_sp_shell" = bash ]; then
export -f spack
fi
alias spacktivate="spack env activate"
#
@ -315,6 +311,21 @@ if ! _spack_fn_exists use && ! _spack_fn_exists module; then
need_module="yes"
fi;
# Define the spack shell function with some informative no-ops, so when users
# run `which spack`, they see the path to spack and where the function is from.
eval "spack() {
: this is a shell function from: $_sp_share_dir/setup-env.sh
: the real spack script is here: $_sp_prefix/bin/spack
_spack_shell_wrapper \"\$@\"
return \$?
}"
# Export spack function so it is available in subshells (only works with bash)
if [ "$_sp_shell" = bash ]; then
export -f spack
export -f _spack_shell_wrapper
fi
#
# make available environment-modules
#