From 7d2825f9d1bf5a185275620cf33530dd9cc797a3 Mon Sep 17 00:00:00 2001 From: Andrejus Date: Tue, 24 Feb 2026 16:10:12 +0000 Subject: [PATCH] profile: invalidate PATH cache when base PATH changes Store the base PATH alongside the cached result so the cache is only used when the pre-modification PATH still matches. This prevents stale entries when the shell inherits a different base PATH (e.g. inside tmux or after system updates). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- home/.profile | 18 +++++++++++------- install.d/23-stow.sh | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/home/.profile b/home/.profile index 1d28d35..debe143 100644 --- a/home/.profile +++ b/home/.profile @@ -15,16 +15,20 @@ export PAGER=less export HOMEBREW_NO_ANALYTICS=1 export HOMEBREW_NO_ENV_HINTS=1 -# PATH setup with caching +# PATH setup with caching (invalidates when base PATH or .profile changes) _dots_path_cache="${XDG_CACHE_HOME:-$HOME/.cache}/dots/path" +_dots_path_hit=false if [[ -f "$_dots_path_cache" && "$_dots_path_cache" -nt ~/.profile ]]; then - export PATH="$(cat "$_dots_path_cache")" -else + { IFS= read -r _k; IFS= read -r _v; } < "$_dots_path_cache" + [[ "$_k" == "$PATH" ]] && { export PATH="$_v"; _dots_path_hit=true; } + unset _k _v +fi +if [[ "$_dots_path_hit" != true ]]; then + _dots_base_path="$PATH" [[ ":$PATH:" != *":$HOME/.local/bin:"* ]] && export PATH="$HOME/.local/bin:$PATH" [[ -x "/opt/homebrew/bin/brew" && ":$PATH:" != *":/opt/homebrew/bin:"* ]] && export PATH="/opt/homebrew/bin:/opt/homebrew/sbin:$PATH" - - # Cache the result mkdir -p "$(dirname "$_dots_path_cache")" - echo "$PATH" > "$_dots_path_cache" + printf '%s\n%s\n' "$_dots_base_path" "$PATH" > "$_dots_path_cache" + unset _dots_base_path fi -unset _dots_path_cache +unset _dots_path_cache _dots_path_hit diff --git a/install.d/23-stow.sh b/install.d/23-stow.sh index 414513e..c25d673 100755 --- a/install.d/23-stow.sh +++ b/install.d/23-stow.sh @@ -40,6 +40,6 @@ mkdir -p "$HOME/.ssh" stow --dir="$root_dir" --target="$HOME" home -# Bust PATH cache to force regeneration with new profile +# Bust PATH cache to force rebuild with new profile rm -f "${XDG_CACHE_HOME:-$HOME/.cache}/dots/path"