From 9aa8061e831f7e25ebd8777959856af4d01b149c Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Mon, 17 Dec 2018 13:50:33 +0000 Subject: [PATCH] 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. --- do | 2 +- minimal/do | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/do b/do index 7d947ce..223b1ad 100755 --- a/do +++ b/do @@ -24,7 +24,7 @@ usage() { } mydir=$(dirname "$0") -cd "$mydir" || die "can't find self in dir: $mydir" +cd "$(/bin/pwd)" && cd "$mydir" || die "can't find self in dir: $mydir" args= while [ "$1" != "${1#-}" ]; do diff --git a/minimal/do b/minimal/do index d2ec992..4f634ee 100755 --- a/minimal/do +++ b/minimal/do @@ -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