Skip to content

Commit 46ebbca

Browse files
committed
refactor: eliminate cd usage in bun wrapper script
Replace explicit directory changes with subshell execution to avoid changing the main shell's working directory. Infisical now runs from project root in a subprocess while preserving the original shell location. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 31f2a6a commit 46ebbca

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

.bin/bun

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,21 @@ if [ -z "$REAL_BUN" ]; then
2222
REAL_BUN=$(PATH=$(echo "$PATH" | tr ':' '\n' | grep -v "^$SCRIPT_DIR$" | tr '\n' ':') which bun)
2323
fi
2424

25+
# Function to find project root using git
26+
find_project_root() {
27+
# Use git to find the repository root
28+
local git_root=$(git rev-parse --show-toplevel 2>/dev/null)
29+
if [ -n "$git_root" ]; then
30+
echo "$git_root"
31+
else
32+
# Fallback to current directory if not in a git repo
33+
echo "$(pwd)"
34+
fi
35+
}
36+
2537
# Infisical cache configuration
26-
CACHE_FILE=".infisical-cache"
38+
PROJECT_ROOT="$(find_project_root)"
39+
CACHE_FILE="$PROJECT_ROOT/.infisical-cache"
2740
CACHE_TTL_SECONDS=${INFISICAL_CACHE_TTL:-900} # Default 15 minutes, configurable via env var
2841

2942
# Clean up any leftover temporary files
@@ -45,8 +58,8 @@ is_cache_valid() {
4558
fi
4659

4760
# Check if .infisical.json has been modified since cache was created
48-
if [ -f ".infisical.json" ]; then
49-
local infisical_time=$(stat -f "%m" ".infisical.json" 2>/dev/null || stat -c "%Y" ".infisical.json" 2>/dev/null)
61+
if [ -f "$PROJECT_ROOT/.infisical.json" ]; then
62+
local infisical_time=$(stat -f "%m" "$PROJECT_ROOT/.infisical.json" 2>/dev/null || stat -c "%Y" "$PROJECT_ROOT/.infisical.json" 2>/dev/null)
5063
if [ $infisical_time -gt $cache_time ]; then
5164
return 1
5265
fi
@@ -82,12 +95,12 @@ create_cache() {
8295
# Set performance optimizations for Infisical
8396
export INFISICAL_DISABLE_UPDATE_CHECK=true
8497

85-
# Try to get secrets using infisical export with timeout (fast path)
98+
# Run infisical export in a subshell from project root to avoid cd in main shell
8699
local output
87100
local temp_file=$(mktemp)
88101

89-
# Run infisical export in background, suppress stderr to avoid cluttered output
90-
(infisical export > "$temp_file" 2>/dev/null; echo $? > "$temp_file.exit") &
102+
# Run infisical export in background from project root, suppress stderr to avoid cluttered output
103+
(cd "$PROJECT_ROOT" && infisical export > "$temp_file" 2>/dev/null; echo $? > "$temp_file.exit") &
91104
local pid=$!
92105

93106
# Wait up to 10 seconds

knowledge.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ The `.bin/bun` script automatically wraps bun commands with infisical when secre
216216

217217
**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.
218218

219-
**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.
219+
**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.
220220

221221
**Session Validation**: The wrapper detects expired Infisical sessions using `infisical export` with a robust 10-second timeout implementation that works cross-platform (macOS and Linux). Uses background processes with polling to prevent hanging on interactive prompts. Valid sessions output environment variables in `KEY='value'` format, while expired sessions either output interactive prompts or timeout. Provides clear error messages directing users to run `infisical login`.
222222

0 commit comments

Comments
 (0)