Skip to content

Commit c61ed31

Browse files
committed
update .bin/bun and knowledge files about it
1 parent c110170 commit c61ed31

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

.bin/bun

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ load_from_cache() {
7878
set -a # automatically export all variables
7979
source "$CACHE_FILE" 2>/dev/null
8080
set +a
81-
81+
8282
# Verify that key variables are set to confirm success
8383
if [ -n "$PORT" ] || [ -n "$DATABASE_URL" ] || [ -n "$ANTHROPIC_API_KEY" ]; then
8484
return 0
@@ -88,7 +88,27 @@ load_from_cache() {
8888
fi
8989
}
9090

91+
# Function to load worktree env if it exists
92+
load_worktree_env() {
93+
if [ -f "$PROJECT_ROOT/.env.worktree" ]; then
94+
if [ "$1" = "override" ]; then
95+
echo "🔧 Loading worktree environment variables from .env.worktree (overriding Infisical)"
96+
else
97+
echo "🔧 Loading worktree environment variables from .env.worktree"
98+
fi
99+
set -a
100+
source "$PROJECT_ROOT/.env.worktree"
101+
set +a
102+
fi
103+
}
91104

105+
# Function to finalize and exec bun with secrets loaded
106+
finalize_and_exec() {
107+
export NEXT_PUBLIC_INFISICAL_UP=true
108+
# Load worktree overrides AFTER Infisical cache to ensure they take precedence
109+
load_worktree_env "override"
110+
exec "$REAL_BUN" "$@"
111+
}
92112

93113
# Function to create cache from infisical
94114
create_cache() {
@@ -98,11 +118,11 @@ create_cache() {
98118
# Run infisical export in a subshell from project root to avoid cd in main shell
99119
local output
100120
local temp_file=$(mktemp)
101-
121+
102122
# Run infisical export in background from project root, suppress stderr to avoid cluttered output
103123
(cd "$PROJECT_ROOT" && infisical export > "$temp_file" 2>/dev/null; echo $? > "$temp_file.exit") &
104124
local pid=$!
105-
125+
106126
# Wait up to 10 seconds
107127
local count=0
108128
while [ $count -lt 100 ]; do # 100 * 0.1s = 10s
@@ -117,7 +137,7 @@ create_cache() {
117137
sleep 0.1
118138
count=$((count + 1))
119139
done
120-
140+
121141
# If still running, kill it (timeout)
122142
if kill -0 $pid 2>/dev/null; then
123143
kill $pid 2>/dev/null
@@ -235,32 +255,24 @@ doesnt_need_secrets() {
235255
esac
236256
}
237257

238-
# Check if we're in a worktree and source worktree-specific env vars
239-
if [ -f ".env.worktree" ]; then
240-
echo "🔧 Loading worktree environment variables from .env.worktree"
241-
set -a # automatically export all variables
242-
source .env.worktree
243-
set +a
244-
fi
245-
246258
# Main logic - determine execution path based on secrets requirement
247259
if doesnt_need_secrets "$@"; then
248260
# Path 1: Command doesn't need secrets - run directly
261+
# But still load worktree overrides for commands that don't need secrets
262+
load_worktree_env
249263
exec "$REAL_BUN" "$@"
250264
fi
251265

252266
# Path 2: Command needs secrets - try cache first, then create if needed
253267
if is_cache_valid && load_from_cache; then
254-
# Path 2a: Valid cache exists - use it
255-
export NEXT_PUBLIC_INFISICAL_UP=true
256-
exec "$REAL_BUN" "$@"
268+
# Path 2a: Valid cache exists - use it, then apply worktree overrides
269+
finalize_and_exec "$@"
257270
fi
258271

259272
# Path 2b: No valid cache - create new one
260273
if create_cache; then
261274
load_from_cache
262-
export NEXT_PUBLIC_INFISICAL_UP=true
263-
exec "$REAL_BUN" "$@"
275+
finalize_and_exec "$@"
264276
fi
265277

266278
# Path 2c: Cache creation failed - exit with error

knowledge.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ The `.bin/bun` script automatically wraps bun commands with infisical when secre
269269

270270
**Worktree Support**: The wrapper automatically detects and loads `.env.worktree` files when present, allowing worktrees to override Infisical environment variables (like ports) for local development. This enables multiple worktrees to run simultaneously on different ports without conflicts.
271271

272+
The wrapper also loads environment variables in the correct precedence order:
273+
1. Infisical secrets are loaded first (if needed)
274+
2. `.env.worktree` is loaded second to override any conflicting variables
275+
3. This ensures worktree-specific overrides (like custom ports) always take precedence over cached Infisical defaults
276+
277+
The wrapper looks for `.env.worktree` in the project root directory, making it work consistently regardless of the current working directory when bun commands are executed.
278+
272279
**Performance Optimizations**: The wrapper uses `--silent` flag with Infisical to reduce CLI output overhead and sets `INFISICAL_DISABLE_UPDATE_CHECK=true` to skip version checks for faster startup times.
273280

274281
**Infisical Caching**: The wrapper implements robust caching of environment variables in `.infisical-cache` with a 15-minute TTL (configurable via `INFISICAL_CACHE_TTL`). This reduces startup time from ~1.2s to ~0.16s (87% improvement). The cache uses `infisical export` which outputs secrets directly in `KEY='value'` format, ensuring ONLY Infisical-managed secrets are cached (no system environment variables). Multi-line secrets like RSA private keys are handled correctly using `source` command. Cache automatically invalidates when `.infisical.json` is modified or after TTL expires. Uses subshell execution to avoid changing the main shell's working directory.

0 commit comments

Comments
 (0)