minimal/do: fix a bug when $PWD != $(/bin/pwd).

This can happen when $PWD contains a symlink somewhere in the path.  In
that case, "cd ..; cat x" could mean something different from "cat ../x".

Notably, this error occurs when running "./do test" if your build
directory is through a symlink.  For example, on freebsd your home
directory is /home/$USER, but /home is a symlink to /usr/home, which
triggers this problem.

Not adding tests in this commit, because when I added some tests, I
found even more symlink-related bugs, but those ones are much more
unlikely to occur.  The additional fixes+tests are in a later commit.
This commit is contained in:
Avery Pennarun 2018-12-17 13:50:33 +00:00
commit 9aa8061e83
2 changed files with 9 additions and 3 deletions

View file

@ -44,7 +44,9 @@ qdirname()
)
_dirsplit "$0"
REDO=$(cd "${_dirsplit_dir:-.}" && echo "$PWD/$_dirsplit_base")
REDO=$(cd "$(/bin/pwd)" &&
cd "${_dirsplit_dir:-.}" &&
echo "$PWD/$_dirsplit_base")
export REDO
_cmd=$_dirsplit_base
@ -77,7 +79,11 @@ if [ -z "$DO_BUILT" -a "$_cmd" != "redo-whichdo" ]; then
if [ "$#" -eq 0 ] && [ "$_cmd" = "do" -o "$_cmd" = "redo" ]; then
set all # only toplevel redo has a default target
fi
export DO_STARTDIR="$PWD"
export DO_STARTDIR="$(/bin/pwd)"
# If starting /bin/pwd != $PWD, this will fix it.
# That can happen when $PWD contains symlinks that the shell is
# trying helpfully (but unsuccessfully) to hide from the user.
cd "$DO_STARTDIR" || exit 99
export DO_BUILT="$PWD/.do_built"
if [ -z "$_do_opt_clean" -a -e "$DO_BUILT" ]; then
echo "do: Incremental mode. Use -c for clean rebuild." >&2