Resolve the repository from the invoking directory

A task included from the global mise config runs with the config root as its
working directory - $HOME - rather than the directory the user invoked it from.
Deriving the repository from the current directory therefore failed everywhere
except a project-level include, which defeats the point of installing the task
once and using it in every repository.

mise passes the real directory as MISE_ORIGINAL_CWD, so enter it before
resolving the repository, falling back to the current directory when the task
is run directly rather than through mise.
This commit is contained in:
2026-07-30 14:35:40 -05:00
parent b03fcd6dd7
commit d96f32d773
2 changed files with 60 additions and 0 deletions
+12
View File
@@ -210,7 +210,19 @@ sandbox_profile_name() {
printf '%s' "$mapped"
}
# A task included from the global mise config runs with the config root as its
# working directory ($HOME), not the directory the user invoked it from, so the
# repository would otherwise be undiscoverable from anywhere.
enter_invocation_directory() {
local invoked_from="${MISE_ORIGINAL_CWD:-$PWD}"
cd "$invoked_from" ||
die "Could not enter the invoking directory: $invoked_from"
}
repository_context() {
enter_invocation_directory
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)" ||
die "This command must be run inside a Git repository."
+48
View File
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
set -euo pipefail
# shellcheck source-path=SCRIPTDIR
# shellcheck source=tasks/ai/sbx
source "$(dirname "${BASH_SOURCE[0]}")/../tasks/ai/sbx"
failures=0
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
fail() {
printf 'FAIL: %s\n' "$1" >&2
failures=$((failures + 1))
}
mkdir -p "$work/invoked" "$work/elsewhere"
(
cd "$work/elsewhere"
MISE_ORIGINAL_CWD="$work/invoked"
export MISE_ORIGINAL_CWD
enter_invocation_directory
[[ "$PWD" == "$work/invoked" ]]
) || fail "did not enter MISE_ORIGINAL_CWD"
(
cd "$work/elsewhere"
unset MISE_ORIGINAL_CWD
enter_invocation_directory
[[ "$PWD" == "$work/elsewhere" ]]
) || fail "did not stay in PWD when MISE_ORIGINAL_CWD is unset"
if (
cd "$work/elsewhere"
MISE_ORIGINAL_CWD="$work/does-not-exist"
export MISE_ORIGINAL_CWD
enter_invocation_directory
) 2>/dev/null; then
fail "an unusable MISE_ORIGINAL_CWD was accepted"
fi
if ((failures)); then
printf '%d assertion(s) failed\n' "$failures" >&2
exit 1
fi
printf 'All invocation directory assertions passed.\n'