Skip to content

Commit 844a4e2

Browse files
committed
haskell_cabal_library: No execroot in pkg-conf
Avoid absolute paths to execroot in the generated package configuration and package-db. These would leak into Bazel's cache keys and cause remote cache misses.
1 parent 4f8719d commit 844a4e2

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

haskell/private/cabal_wrapper.sh.tpl

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ function canonicalize_path()
3939
echo $new_path
4040
}
4141

42+
# relative_to ORIGIN PATH
43+
# Compute the relative path from ORIGIN to PATH.
44+
function relative_to() {
45+
local out=
46+
# Split path into components
47+
local -a relto; IFS="/\\" read -ra relto <<<"$1"
48+
local -a path; IFS="/\\" read -ra path <<<"$2"
49+
local off=0
50+
while [[ "${relto[$off]}" == "${path[$off]}" ]]; do
51+
if [[ $off -eq ${#relto[@]} || $off -eq ${#path[@]} ]]; then
52+
break
53+
fi
54+
: $((off++))
55+
done
56+
for ((i=$off; i < ${#relto[@]}; i++)); do
57+
out="$out${out:+/}.."
58+
done
59+
for ((i=$off; i < ${#path[@]}; i++)); do
60+
out="$out${out:+/}${path[$i]}"
61+
done
62+
echo "$out"
63+
}
64+
4265
# Remove any relative entries, because we'll be changing CWD shortly.
4366
LD_LIBRARY_PATH=$(canonicalize_path $LD_LIBRARY_PATH)
4467
LIBRARY_PATH=$(canonicalize_path $LIBRARY_PATH)
@@ -48,7 +71,7 @@ name=$1
4871
execroot="$(pwd)"
4972
setup=$execroot/$2
5073
srcdir=$execroot/$3
51-
pkgroot="$(realpath $execroot/$4/..)" # By definition (see ghc-pkg source code).
74+
pkgroot="$(realpath $execroot/$(dirname $4))" # By definition (see ghc-pkg source code).
5275
shift 4
5376

5477
declare -a extra_args
@@ -113,8 +136,23 @@ library=($libdir/libHS*.a)
113136
if [[ -n ${library+x} && -f $package_database/$name.conf ]]
114137
then
115138
mv $libdir/libHS*.a $dynlibdir
116-
sed 's,library-dirs:.*,library-dirs: ${pkgroot}/lib,' \
117-
$package_database/$name.conf > $package_database/$name.conf.tmp
139+
# The $execroot is an absolute path and should not leak into the output.
140+
# Replace each ocurrence of execroot by a path relative to ${pkgroot}.
141+
function replace_execroot() {
142+
local line
143+
local relpath
144+
while IFS="" read -r line; do
145+
while [[ $line =~ ("$execroot"[^[:space:]]*) ]]; do
146+
relpath="$(relative_to "$pkgroot" "${BASH_REMATCH[1]}")"
147+
line="${line/${BASH_REMATCH[1]}/\$\{pkgroot\}\/$relpath/}"
148+
done
149+
echo "$line"
150+
done
151+
}
152+
sed -e 's,library-dirs:.*,library-dirs: ${pkgroot}/lib,' \
153+
$package_database/$name.conf \
154+
| replace_execroot \
155+
> $package_database/$name.conf.tmp
118156
mv $package_database/$name.conf.tmp $package_database/$name.conf
119157
%{ghc_pkg} recache --package-db=$package_database
120158
fi

0 commit comments

Comments
 (0)