Skip to content

Commit 0ca062e

Browse files
committed
fix: detect logged-in user's home directory when running as root
When asimov runs as root (via brew services or sudo), ~ expands to /var/root. Now detects the console user via stat/dscl and uses their home directory instead. Addresses stevegrunwell#72.
1 parent 5d4263d commit 0ca062e

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4444

4545
### Fixed
4646

47+
* Detect the logged-in user's home directory when running as root, fixing `brew services` and `sudo` invocations that would search `/var/root` instead ([stevegrunwell/asimov#72])
4748
* Fixed duplicate Gradle sentinel entries in the sentinels list
4849
* Fixed typo in comment ("decendents" → "descendants")
4950

@@ -129,5 +130,6 @@ Initial public release.
129130
[#56]: https://github.com/stevegrunwell/asimov/pull/56
130131
[stevegrunwell/asimov#64]: https://github.com/stevegrunwell/asimov/pull/64
131132
[stevegrunwell/asimov#69]: https://github.com/stevegrunwell/asimov/pull/69
133+
[stevegrunwell/asimov#72]: https://github.com/stevegrunwell/asimov/issues/72
132134
[stevegrunwell/asimov#87]: https://github.com/stevegrunwell/asimov/pull/87
133135
[stevegrunwell/asimov#97]: https://github.com/stevegrunwell/asimov/pull/97

asimov

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@ set -Eeu -o pipefail
1717
# @author Steve Grunwell
1818
# @license MIT
1919

20-
readonly ASIMOV_ROOT=~
20+
# When running as root (e.g. via launchd or sudo), determine the logged-in
21+
# user's home directory instead of using /var/root.
22+
if [[ "${EUID:-$(id -u)}" -eq 0 ]]; then
23+
_console_user="$(stat -f '%Su' /dev/console 2>/dev/null || echo '')"
24+
if [[ -n "$_console_user" && "$_console_user" != "root" ]]; then
25+
_root_dir="$(dscl . -read "/Users/${_console_user}" NFSHomeDirectory 2>/dev/null | awk '{print $2}')"
26+
readonly ASIMOV_ROOT="$_root_dir"
27+
else
28+
readonly ASIMOV_ROOT=~
29+
fi
30+
else
31+
readonly ASIMOV_ROOT=~
32+
fi
2133

2234
# Paths to unconditionally skip over. This prevents Asimov from modifying the
2335
# Time Machine exclusions for these paths (and descendants). It has an important

tests/behavior.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ load test_helper
131131
[[ "$(count_exclusions)" -eq 1 ]]
132132
}
133133

134+
# =============================================================================
135+
# ASIMOV_ROOT detection
136+
# =============================================================================
137+
138+
@test "uses HOME as root directory when not running as root" {
139+
create_project "Code/My-Project" "package.json" "node_modules"
140+
run_asimov
141+
assert_excluded "${HOME}/Code/My-Project/node_modules"
142+
[[ "$(count_exclusions)" -eq 1 ]]
143+
}
144+
134145
@test "does not descend into excluded dependency directories" {
135146
# A node_modules inside another node_modules should not be separately excluded
136147
create_project "Code/My-Project" "package.json" "node_modules"

0 commit comments

Comments
 (0)