Skip to content

Commit e1e3fde

Browse files
committed
chore: wip
1 parent fd12b8e commit e1e3fde

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

packages/launchpad/src/dev/shellcode.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,32 @@ __launchpad_update_path() {
162162
fi
163163
}
164164
165+
# Ensure the active environment's bin path stays first in PATH
166+
__launchpad_ensure_env_path_priority() {
167+
if [[ -n "$LAUNCHPAD_ENV_BIN_PATH" && -d "$LAUNCHPAD_ENV_BIN_PATH" ]]; then
168+
local OLD_IFS="$IFS"
169+
local rebuilt_path=""
170+
IFS=':'
171+
for seg in $PATH; do
172+
if [[ -z "$seg" ]]; then continue; fi
173+
if [[ "$seg" == "$LAUNCHPAD_ENV_BIN_PATH" ]]; then continue; fi
174+
if [[ -z "$rebuilt_path" ]]; then
175+
rebuilt_path="$seg"
176+
else
177+
rebuilt_path="$rebuilt_path:$seg"
178+
fi
179+
done
180+
IFS="$OLD_IFS"
181+
if [[ -n "$rebuilt_path" ]]; then
182+
export PATH="$LAUNCHPAD_ENV_BIN_PATH:$rebuilt_path"
183+
else
184+
export PATH="$LAUNCHPAD_ENV_BIN_PATH"
185+
fi
186+
# Refresh command hash tables for both bash and zsh
187+
hash -r 2>/dev/null || rehash 2>/dev/null || true
188+
fi
189+
}
190+
165191
# Fast library path update for quick activation (no expensive find operations)
166192
__launchpad_update_library_paths_fast() {
167193
local env_dir="$1"
@@ -793,10 +819,11 @@ __launchpad_chpwd() {
793819
if [[ -n "$__lp_cache_ok" ]]; then
794820
# Instant activation from persistent cache (no filesystem scans)
795821
export PATH="$env_dir/bin:$LAUNCHPAD_ORIGINAL_PATH"
822+
export LAUNCHPAD_ENV_BIN_PATH="$env_dir/bin"
796823
__launchpad_update_library_paths_fast "$env_dir"
797824
__launchpad_ensure_global_path_fast
798825
__launchpad_ensure_system_path
799-
hash -r 2>/dev/null || true
826+
__launchpad_ensure_env_path_priority
800827
# Verbose diagnostics for fast cached activation
801828
if [[ "$LAUNCHPAD_VERBOSE" == "true" ]]; then
802829
printf "🔍 Fast path (cache): env_dir=%s ready=%s\n" "$env_dir" "$([ -f "$env_dir/.launchpad_ready" ] && echo true || echo false)" >&2
@@ -820,10 +847,11 @@ __launchpad_chpwd() {
820847
# use glob expansion which is faster than ls
821848
if [[ -d "$env_dir/bin" ]] && [[ $__lp_markers_ready -eq 1 || $(echo "$env_dir/bin"/*) != "$env_dir/bin/*" ]]; then
822849
export PATH="$env_dir/bin:$LAUNCHPAD_ORIGINAL_PATH"
850+
export LAUNCHPAD_ENV_BIN_PATH="$env_dir/bin"
823851
__launchpad_update_library_paths_fast "$env_dir"
824852
__launchpad_ensure_global_path_fast
825853
__launchpad_ensure_system_path
826-
hash -r 2>/dev/null || true
854+
__launchpad_ensure_env_path_priority
827855
828856
# Update persistent cache for instant future activation and keep parity with dep file
829857
mkdir -p "$(dirname "$cache_file")" 2>/dev/null || true
@@ -906,9 +934,8 @@ __launchpad_chpwd() {
906934
# Ensure global dependencies are still in PATH after project setup
907935
__launchpad_ensure_global_path
908936
__launchpad_ensure_system_path
909-
910-
# Clear command hash table to ensure commands are found in new PATH
911-
hash -r 2>/dev/null || true
937+
export LAUNCHPAD_ENV_BIN_PATH="$env_dir/bin"
938+
__launchpad_ensure_env_path_priority
912939
913940
# Mark environment ready for instant future activation (both cache and marker)
914941
mkdir -p "$env_dir" 2>/dev/null || true
@@ -927,7 +954,8 @@ __launchpad_chpwd() {
927954
__launchpad_update_library_paths "$env_dir"
928955
__launchpad_ensure_global_path
929956
__launchpad_ensure_system_path
930-
hash -r 2>/dev/null || true
957+
export LAUNCHPAD_ENV_BIN_PATH="$env_dir/bin"
958+
__launchpad_ensure_env_path_priority
931959
932960
printf "${activationMessage}\n" >&2
933961
else
@@ -1090,6 +1118,8 @@ __launchpad_auto_refresh_check() {
10901118
__launchpad_refresh_global_paths
10911119
__launchpad_source_hooks_dir "$HOME/.config/launchpad/hooks/post-refresh.d"
10921120
fi
1121+
# Always reassert env path precedence on each prompt
1122+
__launchpad_ensure_env_path_priority
10931123
}
10941124
10951125
# Check for global refresh on every prompt (lightweight check)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { shellcode } from '../src/dev/shellcode'
3+
4+
describe('Shell Integration PATH Precedence', () => {
5+
it('includes env path precedence helper and sets LAUNCHPAD_ENV_BIN_PATH', () => {
6+
const code = shellcode(true)
7+
8+
expect(code).toContain('__launchpad_ensure_env_path_priority()')
9+
expect(code).toContain('export LAUNCHPAD_ENV_BIN_PATH="$env_dir/bin"')
10+
})
11+
12+
it('calls precedence helper after each activation path', () => {
13+
const code = shellcode(true)
14+
15+
// Cached fast path
16+
expect(code).toContain('export PATH="$env_dir/bin:$LAUNCHPAD_ORIGINAL_PATH"')
17+
expect(code).toContain('__launchpad_ensure_env_path_priority')
18+
19+
// Fast activation path when env exists
20+
expect(code).toContain('export LAUNCHPAD_ENV_BIN_PATH="$env_dir/bin"')
21+
22+
// Post-setup success path
23+
expect(code).toContain('export LAUNCHPAD_ENV_BIN_PATH="$env_dir/bin"')
24+
25+
// Fallback activation on setup failure
26+
expect(code).toContain('__launchpad_update_path "$env_dir/bin"')
27+
expect(code).toContain('__launchpad_ensure_env_path_priority')
28+
})
29+
30+
it('reasserts precedence on each prompt via precmd/PROMPT_COMMAND hook', () => {
31+
const code = shellcode(true)
32+
33+
// Hook registration present
34+
expect(code).toContain('add-zsh-hook precmd __launchpad_auto_refresh_check')
35+
// Auto-refresh function should invoke path precedence helper
36+
expect(code).toContain('__launchpad_auto_refresh_check()')
37+
expect(code).toContain('__launchpad_ensure_env_path_priority')
38+
})
39+
})

0 commit comments

Comments
 (0)