Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Surprising effect of fun() { local FPATH=...; autoload -X }, and a bug

On Sep 27, 12:07am, Sebastian Gniazdowski wrote:
}             eval "function $func {
}                 local FPATH="$PLUGIN_DIR":"${FPATH}"
}                 builtin autoload -X ${opts[*]}
}             }"
} What I astonishingly realized today is that functions autoloaded this
} way can further use autoload builtin purely normally. FPATH visible
} from the specially-autoloaded function is modified, has the required
} added component, and builtin autoload works as expected (even with
} <5.1 Zsh, it has a more specific manual autoload stub).

This will be the case as long as the autoload command AND the first
run of the normally-autoloaded function BOTH happen during the FIRST
run of the "specially-loaded" function.

E.g. suppose we have this silly example:

  --8<-- file "outer" --8<--
    if (( $+functions[inner2] )); then
      autoload inner1 inner2
  -->8-- file "outer" -->8--

and we send func=outer through your eval above.  When "outer" is first
run, the call stack will look like

    outer # scope of local FPATH is here
     autoload -X
      outer # "normal autoloads" are here
       inner1 # modified FPATH is still in scope

In this case inner1 will load with the modified FPATH.  On the second
and subsequent runs of "outer", the call stack will look like:

    outer # no FPATH declared
     inner2 # no modified FPATH in scope

thus inner2 will load from the normal fpath.  It's the fpath at time of
function call that matters, not the fpath at time of autoload command.

Messages sorted by: Reverse Date, Date, Thread, Author