ast icon indicating copy to clipboard operation
ast copied to clipboard

typeset -fu $(basename x.sh) interacts badly with .paths mechanism

Open krader1961 opened this issue 4 years ago • 0 comments

While working on PR #1389 (Issue #570) I found there was one test that failed with that pull-request for no discernible reason:

https://github.com/att/ast/blob/a9d4bb0064df247b20f604626b5b58698079bd4d/src/cmd/ksh93/tests/path.sh#L380-L391

The PATH var has to be modified after the offending typeset -fu $(basename x.sh) before autoloaded functions defined by bin/.paths are usable. At this time I have no idea what the problem is. But it exists in ksh93u+. It is not a problem we have introduced. My change to support a man function that knows about ksh "internal" man pages simply brought this bug to light. Also, this unexpected interaction between the various methods to enable autoloaded functions shows yet another reason why the current mechanisms are suboptimal. Behavior more like the fish shell would be preferable.

Save the following script to /tmp/k and run via ksh /tmp/k. There should be zero myfun: not found errors.

SHELL=/bin/ksh
#SHELL=/usr/local/bin/ksh
rm -rf /tmp/ksh
mkdir /tmp/ksh
mkdir /tmp/ksh/bin
mkdir /tmp/ksh/fun
print FPATH=../fun > /tmp/ksh/bin/.paths
print 'function myfun { print myfun $*; }' > /tmp/ksh/fun/myfun

unset FPATH
#
# Of the next two statements The first one is "okay" in that it doesn't cause
# any failures to find `myfun`. The second one is "bad" in that it causes some
# of the following statements to fail which should not fail.
#
# print 'typeset -fu whatever' > /tmp/ksh/.kshrc
print 'typeset -fu $(basename whatever.x)' > /tmp/ksh/.kshrc

HOME=/tmp/ksh PATH="/tmp/ksh/bin:$PATH" $SHELL -Ec "myfun $LINENO"
HOME=/tmp/ksh PATH="/tmp/ksh/bin:$PATH" $SHELL -Ec "typeset -fu myfun; myfun $LINENO"
HOME=/tmp/ksh FPATH=/tmp/ksh/fun $SHELL -Ec "myfun $LINENO"
HOME=/tmp/ksh FPATH=/tmp/ksh/fun $SHELL -Ec "typeset -fu myfun2; myfun $LINENO"
HOME=/tmp/ksh FPATH=/tmp/ksh/fun PATH="/tmp/ksh/bin:$PATH" $SHELL -Ec "typeset -fu myfun; myfun $LINENO"

HOME=/tmp/ksh PATH="/tmp/ksh/bin:$PATH" $SHELL -Ec 'myfun '"$LINENO"

print 'PATH="/tmp/ksh/bin:$PATH"' >> /tmp/ksh/.kshrc
HOME=/tmp/ksh $SHELL -Ec 'myfun '"$LINENO"

The large number of variations is a consequence of how I explored possible options for figuring out what scenarios failed which should not have failed. Note the first two failures have slightly different wording which is surprising.

For the moment I'm going to modify the unit test to still run but workaround this longstanding bug.

krader1961 avatar Sep 07 '19 04:09 krader1961