Skip to content

Commit 5a74ebe

Browse files
authored
fix(env): robust path handling and bash enforcement (#3224)
Ensure the script is sourced from bash and use project root for all path references instead of relying on $PWD. This improves reliability when sourcing from different locations.
1 parent 816b708 commit 5a74ebe

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

.source.dev

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
# Source this file from the .source script. You can also include additional environment
77
# variables such as DBSYNC_SCHEMA_DIR and GITHUB_TOKEN in that file, or change INSTANCE_NUM.
88
#
9+
10+
[ -n "${BASH_VERSION:-}" ] || {
11+
echo "This script must be sourced from bash." >&2
12+
return 1
13+
}
14+
915
# export DBSYNC_SCHEMA_DIR="$HOME/Source/repos/cardano-db-sync/schema"
1016
# export GITHUB_TOKEN=ghp_....
1117
INSTANCE_NUM="${INSTANCE_NUM:-0}"
1218

13-
# Ensure we are in the correct directory
14-
if [[ "${PWD##*/}" != "cardano-node-tests"* ]]; then
15-
echo "Must be in cardano-node-tests* directory" >&2
16-
return 1
17-
fi
19+
_ROOT_DIR_CNT="$(readlink -m "${BASH_SOURCE[0]}/..")"
1820

1921
# Activate poetry virtual environment
2022
if [ -z "${VIRTUAL_ENV:-}" ]; then
@@ -39,9 +41,9 @@ unset BOOTSTRAP_DIR
3941
mkdir -p "${CARDANO_NODE_SOCKET_PATH%/*}"
4042

4143
# Set temporary directory for this instance
42-
TMPDIR="$PWD/tmp"
44+
TMPDIR="$_ROOT_DIR_CNT/tmp"
4345
if [ "$INSTANCE_NUM" != 0 ]; then
44-
TMPDIR="$PWD/tmp${INSTANCE_NUM}"
46+
TMPDIR="$_ROOT_DIR_CNT/tmp${INSTANCE_NUM}"
4547
fi
4648
mkdir -p "$TMPDIR"
4749
export TMPDIR
@@ -51,13 +53,16 @@ PATH="$(echo "$PATH" | tr ":" "\n" | grep -v "ghcup" | tr "\n" ":" | sed 's/:*$/
5153
export PATH
5254

5355
# Prepend instance-specific .bin directory to PATH if it exists and if not already present
54-
if [ -e "$PWD/.bin${INSTANCE_NUM}" ] && [[ ":$PATH:" != *":$PWD/.bin${INSTANCE_NUM}:"* ]]; then
55-
export PATH="$PWD/.bin${INSTANCE_NUM}:$PATH"
56+
if [ -e "$_ROOT_DIR_CNT/.bin${INSTANCE_NUM}" ] \
57+
&& [[ ":$PATH:" != *":$_ROOT_DIR_CNT/.bin${INSTANCE_NUM}:"* ]]; then
58+
export PATH="$_ROOT_DIR_CNT/.bin${INSTANCE_NUM}:$PATH"
5659
fi
5760

5861
# Prepend default .bin directory to PATH for instance 0 if it exists and if not already present
59-
if [ "$INSTANCE_NUM" = 0 ] && [ -e "$PWD/.bin" ] && [[ ":$PATH:" != *":$PWD/.bin:"* ]]; then
60-
export PATH="$PWD/.bin:$PATH"
62+
if [ "$INSTANCE_NUM" = 0 ] \
63+
&& [ -e "$_ROOT_DIR_CNT/.bin" ] \
64+
&& [[ ":$PATH:" != *":$_ROOT_DIR_CNT/.bin:"* ]]; then
65+
export PATH="$_ROOT_DIR_CNT/.bin:$PATH"
6166
fi
6267

6368
# Set database connection environment variables for db-sync
@@ -68,10 +73,10 @@ if [ -n "${DBSYNC_SCHEMA_DIR:-}" ] && command -v cardano-smash-server >/dev/null
6873
export SMASH=true
6974
fi
7075

76+
unset INSTANCE_NUM _ROOT_DIR_CNT
77+
7178
# Enable cardano-cli bash completion
7279
if ! command -v _cardano-cli >/dev/null 2>&1 && command -v cardano-cli >/dev/null 2>&1; then
7380
# shellcheck disable=SC1090
7481
. <(cardano-cli --bash-completion-script cardano-cli)
7582
fi
76-
77-
unset INSTANCE_NUM

0 commit comments

Comments
 (0)