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

Re: zcompile does not expand aliases defined in the compiled script



On 7/7/20, Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx> wrote:
> I naively expected that `zcompile x && source x` has the same effect
> as plain `source x` but apparently when using the former aliases
> defined within `x` aren't expanded during the evaluation of `x`.
>
> Is this expected behavior?
>
>   % zsh -f
>   % print 'alias x=:; x' >x
>   % zcompile x
>   % source x
>   x:1: command not found: x

It is expected, but you are not correct that it works when you didn't
compile the file first:
% source x
zsh: command not found: x

However, sourcing a non-compiled file multiple times will of course
work on subsequent runs because the alias is carried across in the
shell session. If you zcompile the file after sourcing it once, it
will also work subsequently (even in new sessions) as aliases, if any,
are expanded at compile time (unless inhibited by -U).

It is possible that you changed the example in the mail without trying
what you changed; if you have the statements
alias x=:
x
in the file x, then sourcing it (non-compiled) will work, since it is
parsed and executed line by line. This does not change anything for
the zcompile case from the above paragraph as the entire file is
parsed and nothing is executed.

Hopefully all of the above is true, I did my best to test it  :).

-- 
Mikael Magnusson



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