Every shell descended from the original Bourne shell searches $PATH when sourcing a plain file name instead of an absolute or relative path name.
The downside of that is that using the same search path for both conflates sourceable shell script libraries with executable programs. This is bad because
if a file is only executable or only sourceable, having it available as a candidate file when looking for the thing it is not is at best inefficient and at worst might cause errors, and
if a file is both, then you cannot reimplement that executable in a language that the shell cannot source without breaking its usability as a sourceable.
The upside is that this might be a good way to implement a proper library loading feature in the shell. Suppose we have a separate shell library search path. I'm going to call it SHPATH for now.
So ideally, when sourcing, the shell would search $SHPATH, not $PATH. So how can we implement that using what the shell gives us? Well, to start, before sourcing we would need to do this:
And then we source the file we want. But there's a problem: since that PATH value has to be set when the sourcing builtin is called, that is the PATH value the sourced script sees while it is being sourced. So we have to reset the PATH back to the right value at the very beginning of the sourced script, by doing:
Of course, a top-level script that is being executed instead of sourced should absolutely not do that, else it will lose its PATH. Good thing we have a very robust and portable mechanism which cooperating scripts can use to tell if they are being sourced.
Of course, if you don't mind the edge cases, you can just use the expansion of $_source_PATH or the comparison of $PATH to $SHPATH to decide if you are being sourced.