Skip to content

Commit 74e8a13

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 f10be79 commit 74e8a13

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
@@ -38,6 +38,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3838
### Fixed
3939

4040
* Handle `tmutil` errors gracefully instead of crashing; paths that fail exclusion are skipped with a warning ([stevegrunwell/asimov#101], [stevegrunwell/asimov#86])
41+
* 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])
4142

4243
### Changed
4344

@@ -134,6 +135,7 @@ Initial public release.
134135
[#56]: https://github.com/stevegrunwell/asimov/pull/56
135136
[stevegrunwell/asimov#64]: https://github.com/stevegrunwell/asimov/pull/64
136137
[stevegrunwell/asimov#69]: https://github.com/stevegrunwell/asimov/pull/69
138+
[stevegrunwell/asimov#72]: https://github.com/stevegrunwell/asimov/issues/72
137139
[stevegrunwell/asimov#84]: https://github.com/stevegrunwell/asimov/issues/84
138140
[stevegrunwell/asimov#86]: https://github.com/stevegrunwell/asimov/issues/86
139141
[stevegrunwell/asimov#87]: https://github.com/stevegrunwell/asimov/pull/87

asimov

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ trap 'rm -f "$ASIMOV_SIZE_LOG"' EXIT
2121
# @author Steve Grunwell
2222
# @license MIT
2323

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

2638
# Paths to unconditionally skip over. This prevents Asimov from modifying the
2739
# 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)