From 16e75ecac097450b2abee7ea0644929b97e45bd7 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Wed, 21 Oct 2020 17:04:42 -0700 Subject: 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). --- share/spack/setup-env.sh | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'share') diff --git a/share/spack/setup-env.sh b/share/spack/setup-env.sh index 9d984ae199..5761d16362 100755 --- a/share/spack/setup-env.sh +++ b/share/spack/setup-env.sh @@ -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 # -- cgit v1.2.3-70-g09d2