edc / bass

Make Bash utilities usable in Fish shell
MIT License
2.2k stars 70 forks source link

How does bass import function from bash and why nvm need sourced every time ? #101

Closed haoadoreorange closed 2 years ago

haoadoreorange commented 2 years ago

First I'm sorry if this isn't the right place to ask.

I'm trying to learn how to interact between bash and fish, and bass is an excellent example. I can't understand how bass import bash functions to fish when sourcing a file, I thought it should read and redefine the functions in fish but it's not the case, for example for nvm use case.

And then, providing that bass import the function to fish, why do we need to source everytime we run nvm ? Isn't sourcing on first session enough ?

edc commented 2 years ago

how bass import bash functions to fish when sourcing a file

Bass does not really import bash functions. When you source a bash script in bash, two things can happen:

  1. the script might "launch" other programs to do something;
  2. the script can alter the current bash process to change environment variables (which then get "inherited" by any programs launched by the bash process)

When you source a bash script (e.g. nvm.sh) through bass, bass creates a temporary wrapper bash script that roughly does the following when sourced:

Bass then launch a bash subprocess and source the above wrapper script.

Now let's look at the two things that original script (nvm.sh) wants to do:

  1. the script might "launch" other programs to do something: this is not impacted.
  2. the script can alter the current bash process to change environment variables: the change is only done to the bash subprocess. However, because the wrapper script monitors the change to the environment variables, the changes are then passed to Bass, and bass will replay the changes in the fish shell.

The reason you have to source nvm every time you use it is because you use nvm through its subcommands (like nvm use). Bass does not import nvm functionality to fish, so Bass still has to invoke the nvm with subcommands in a bash shell. Because Bass has to use a new bash shell subprocess to run the subcommand, it has to ask each subprocess to source nvm.sh to get the nvm functionality before running the subcommand.

Let me know if this makes sense.

haoadoreorange commented 2 years ago

Yes totally ! Thanks so much for the explanation ! I close the issue here (:+1: