Repository navigation
fix(shell-ready): honor user's ZDOTDIR (XDG-layout fix) - #1737
Conversation
…e right rcfiles The wrapper .zshenv used to capture ORCA_ORIG_ZDOTDIR before sourcing the user's ~/.zshenv, so users who set ZDOTDIR=$XDG_CONFIG_HOME/zsh in .zshenv ended up with the wrapper sourcing ~/.zshrc instead of ~/.config/zsh/.zshrc. Functions, aliases, and PATH edits silently disappeared inside every shell-ready PTY (startup-command, attribution shim, SSH). Reorder: clear ZDOTDIR, source user .zshenv inside a function (so an early `return` doesn't abort our wrapper), recapture from user-resolved → spawn-env → HOME, then re-pin ZDOTDIR to the wrapper dir baked into the template. Trailing-slash normalization now matches the Node-side normalizer. Same change in the renderer- and daemon-side wrappers. New zsh-subprocess tests (gated on `which zsh`) cover the XDG case, early-return survival in user .zshenv, spawn-env preservation, HOME fallback, and self-loop normalization including pathological multi- trailing-slash paths. Co-Authored-By: Claude Opus 4.7 <[email protected]>
Reverts #1737 because sourcing user .zshenv inside a wrapper function can change zsh scoping behavior for common startup-file patterns. We will revisit with a safer targeted fix.
|
I reverted this in #1864. After a more careful re-review, I think I merged #1737 too early. The concern is not the goal of honoring I filed #1866 to revisit this with a safer implementation that preserves normal zsh startup semantics while still supporting XDG-style |
|
@nwparker thanks for catching this one! You were spot on — top-level zsh constructs in user Came back with a subshell-based fix in #1947. The subshell forks a child process so user Also went much deeper on test coverage this time — 64 tests covering edge cases the original missed (directory existence, trailing slash variants, whitespace-only values, stderr noise from broken configs). Built a small snapshot framework along the way to keep the tests readable, which was kind of fun to write. Take a look whenever you get a chance! |
|
also now in the latest release. yayy. thx a lot! |
Fixes stablyai#1866 This reimplements PR stablyai#1737 (reverted in stablyai#1864) with a safer approach that preserves normal zsh startup semantics. **Core fix**: Discover ZDOTDIR by sourcing user ~/.zshenv in a subshell instead of inside a wrapper function. This preserves top-level zsh scoping for common patterns like `typeset -U path` that broke in the original implementation. **Shell safety improvements**: - Use `printf '%s\n'` instead of `echo` for capturing ZDOTDIR (handles special characters in paths safely) - Subshell isolates early returns and side effects from wrapper **Code quality**: - Extract duplicated zsh wrapper template to `src/main/shell-templates.ts` - Both local-pty and daemon paths now share identical wrapper logic **Test coverage**: - Add live zsh subprocess tests that spawn real zsh to verify: - XDG ZDOTDIR discovery works - `typeset -U path` in .zshrc preserves top-level scoping - Early returns in .zshenv don't crash the wrapper - Vanilla (non-XDG) configs fall back to HOME correctly - Template structure tests validate subshell discovery logic Before (broken): ```zsh __orca_source_user_zshenv() { source "$HOME/.zshenv" # typeset becomes function-scoped } ``` After (fixed): ```zsh _orca_discovered_zdotdir=$( unset ZDOTDIR [[ -f "$HOME/.zshenv" ]] && source "$HOME/.zshenv" 2>/dev/null printf '%s\n' "${ZDOTDIR}" ) export ORCA_ORIG_ZDOTDIR="${_orca_discovered_zdotdir:-${_orca_spawn_orig_zdotdir:-$HOME}}" ``` The subshell sources .zshenv at top-level (preserving normal scoping), captures only the ZDOTDIR value, then exits. User rcfiles (.zshrc, etc.) are still sourced at the wrapper's top level, so all scoping works normally. Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
#1947) * fix(shell-ready): honor ZDOTDIR without breaking zsh scoping Fixes #1866 This reimplements PR #1737 (reverted in #1864) with a safer approach that preserves normal zsh startup semantics. **Core fix**: Discover ZDOTDIR by sourcing user ~/.zshenv in a subshell instead of inside a wrapper function. This preserves top-level zsh scoping for common patterns like `typeset -U path` that broke in the original implementation. **Shell safety improvements**: - Use `printf '%s\n'` instead of `echo` for capturing ZDOTDIR (handles special characters in paths safely) - Subshell isolates early returns and side effects from wrapper **Code quality**: - Extract duplicated zsh wrapper template to `src/main/shell-templates.ts` - Both local-pty and daemon paths now share identical wrapper logic **Test coverage**: - Add live zsh subprocess tests that spawn real zsh to verify: - XDG ZDOTDIR discovery works - `typeset -U path` in .zshrc preserves top-level scoping - Early returns in .zshenv don't crash the wrapper - Vanilla (non-XDG) configs fall back to HOME correctly - Template structure tests validate subshell discovery logic Before (broken): ```zsh __orca_source_user_zshenv() { source "$HOME/.zshenv" # typeset becomes function-scoped } ``` After (fixed): ```zsh _orca_discovered_zdotdir=$( unset ZDOTDIR [[ -f "$HOME/.zshenv" ]] && source "$HOME/.zshenv" 2>/dev/null printf '%s\n' "${ZDOTDIR}" ) export ORCA_ORIG_ZDOTDIR="${_orca_discovered_zdotdir:-${_orca_spawn_orig_zdotdir:-$HOME}}" ``` The subshell sources .zshenv at top-level (preserving normal scoping), captures only the ZDOTDIR value, then exits. User rcfiles (.zshrc, etc.) are still sourced at the wrapper's top level, so all scoping works normally. Co-Authored-By: Claude Sonnet 4.5 <[email protected]> * Add shell-script-literal test framework and improvements Adds a new declarative test framework for shell-ready tests that uses literal shell scripts (copy-pastable into terminals) with inline snapshots. Framework features: - Shell scripts as string literals with # Run: marker to split setup/test - Direct script execution (no brittle parsing) via temp files - Path normalization for reproducible snapshots (<HOME>, <WRAPPER_DIR>) - Auto-detects shell from command, supports bash/zsh/sh - Inline snapshot testing with vitest toMatchInlineSnapshot() Code quality improvements: - Extract escapeRegex to shared string-utils.ts (deduplicates 2 copies) - Refactor shell-templates.ts for readability (condense comments, add structure) - Pre-compile regex patterns to avoid hot-path allocation - Fix path normalization to sort by length (prevent nested path corruption) - Fix actualUserHome handling to skip empty values All tests passing (64/64 shell-ready tests, 55/55 affected tests). Files added: - src/main/providers/__tests__/shell-ready-framework/shell-script-test.ts - src/main/providers/__tests__/shell-ready-framework/README.md - src/main/providers/__tests__/shell-ready-framework-example.test.ts - src/shared/string-utils.ts Files modified: - src/main/shell-templates.ts (readability cleanup) - src/main/codex/config-toml-trust.ts (use shared escapeRegex) - src/main/daemon/shell-ready.test.ts (updated for new framework) - src/main/providers/local-pty-shell-ready.test.ts (updated for new framework) Co-Authored-By: Claude Sonnet 4.5 <[email protected]> * fix(shell-ready): preserve zshenv semantics --------- Co-authored-by: Claude Sonnet 4.5 <[email protected]> Co-authored-by: Neil <[email protected]>
…e right rcfiles (stablyai#1737) The wrapper .zshenv used to capture ORCA_ORIG_ZDOTDIR before sourcing the user's ~/.zshenv, so users who set ZDOTDIR=$XDG_CONFIG_HOME/zsh in .zshenv ended up with the wrapper sourcing ~/.zshrc instead of ~/.config/zsh/.zshrc. Functions, aliases, and PATH edits silently disappeared inside every shell-ready PTY (startup-command, attribution shim, SSH). Reorder: clear ZDOTDIR, source user .zshenv inside a function (so an early `return` doesn't abort our wrapper), recapture from user-resolved → spawn-env → HOME, then re-pin ZDOTDIR to the wrapper dir baked into the template. Trailing-slash normalization now matches the Node-side normalizer. Same change in the renderer- and daemon-side wrappers. New zsh-subprocess tests (gated on `which zsh`) cover the XDG case, early-return survival in user .zshenv, spawn-env preservation, HOME fallback, and self-loop normalization including pathological multi- trailing-slash paths. Co-authored-by: Claude Opus 4.7 <[email protected]>
Reverts stablyai#1737 because sourcing user .zshenv inside a wrapper function can change zsh scoping behavior for common startup-file patterns. We will revisit with a safer targeted fix.
…yai#1737) (stablyai#1947) * fix(shell-ready): honor ZDOTDIR without breaking zsh scoping Fixes stablyai#1866 This reimplements PR stablyai#1737 (reverted in stablyai#1864) with a safer approach that preserves normal zsh startup semantics. **Core fix**: Discover ZDOTDIR by sourcing user ~/.zshenv in a subshell instead of inside a wrapper function. This preserves top-level zsh scoping for common patterns like `typeset -U path` that broke in the original implementation. **Shell safety improvements**: - Use `printf '%s\n'` instead of `echo` for capturing ZDOTDIR (handles special characters in paths safely) - Subshell isolates early returns and side effects from wrapper **Code quality**: - Extract duplicated zsh wrapper template to `src/main/shell-templates.ts` - Both local-pty and daemon paths now share identical wrapper logic **Test coverage**: - Add live zsh subprocess tests that spawn real zsh to verify: - XDG ZDOTDIR discovery works - `typeset -U path` in .zshrc preserves top-level scoping - Early returns in .zshenv don't crash the wrapper - Vanilla (non-XDG) configs fall back to HOME correctly - Template structure tests validate subshell discovery logic Before (broken): ```zsh __orca_source_user_zshenv() { source "$HOME/.zshenv" # typeset becomes function-scoped } ``` After (fixed): ```zsh _orca_discovered_zdotdir=$( unset ZDOTDIR [[ -f "$HOME/.zshenv" ]] && source "$HOME/.zshenv" 2>/dev/null printf '%s\n' "${ZDOTDIR}" ) export ORCA_ORIG_ZDOTDIR="${_orca_discovered_zdotdir:-${_orca_spawn_orig_zdotdir:-$HOME}}" ``` The subshell sources .zshenv at top-level (preserving normal scoping), captures only the ZDOTDIR value, then exits. User rcfiles (.zshrc, etc.) are still sourced at the wrapper's top level, so all scoping works normally. Co-Authored-By: Claude Sonnet 4.5 <[email protected]> * Add shell-script-literal test framework and improvements Adds a new declarative test framework for shell-ready tests that uses literal shell scripts (copy-pastable into terminals) with inline snapshots. Framework features: - Shell scripts as string literals with # Run: marker to split setup/test - Direct script execution (no brittle parsing) via temp files - Path normalization for reproducible snapshots (<HOME>, <WRAPPER_DIR>) - Auto-detects shell from command, supports bash/zsh/sh - Inline snapshot testing with vitest toMatchInlineSnapshot() Code quality improvements: - Extract escapeRegex to shared string-utils.ts (deduplicates 2 copies) - Refactor shell-templates.ts for readability (condense comments, add structure) - Pre-compile regex patterns to avoid hot-path allocation - Fix path normalization to sort by length (prevent nested path corruption) - Fix actualUserHome handling to skip empty values All tests passing (64/64 shell-ready tests, 55/55 affected tests). Files added: - src/main/providers/__tests__/shell-ready-framework/shell-script-test.ts - src/main/providers/__tests__/shell-ready-framework/README.md - src/main/providers/__tests__/shell-ready-framework-example.test.ts - src/shared/string-utils.ts Files modified: - src/main/shell-templates.ts (readability cleanup) - src/main/codex/config-toml-trust.ts (use shared escapeRegex) - src/main/daemon/shell-ready.test.ts (updated for new framework) - src/main/providers/local-pty-shell-ready.test.ts (updated for new framework) Co-Authored-By: Claude Sonnet 4.5 <[email protected]> * fix(shell-ready): preserve zshenv semantics --------- Co-authored-by: Claude Sonnet 4.5 <[email protected]> Co-authored-by: Neil <[email protected]>
Fixes #1739
Summary
The shell-ready zsh wrapper sources
~/.zshenvbut throws away theZDOTDIRit computes, then sources~/.zshrcinstead of$ZDOTDIR/.zshrc. Users who follow the canonical XDG idiom (export ZDOTDIR="${ZDOTDIR:-$XDG_CONFIG_HOME/zsh}"in~/.zshenv) end up with an empty config inside any Orca PTY that flows throughgetShellReadyLaunchConfig(startup-command PTYs) orgetAttributionShellLaunchConfig(attribution-shim PTYs). Both routes share the same wrapper rcfiles, so both routes are fixed by the same template change. SSH PTYs get the fix via the daemon mirror.The fix:
ORCA_ORIG_ZDOTDIRfor fallback.unset ZDOTDIRso the user's.zshenvcan fire the${ZDOTDIR:-...}default expansion.~/.zshenvinside a function, so an earlyreturn(e.g.[[ -o interactive ]] || return, common in dotfiles) exits only the function — not our wrapper.ORCA_ORIG_ZDOTDIRfrom whatever the user's.zshenvresolvedZDOTDIRto, falling back to the saved spawn-env value, then$HOME.ZDOTDIRto the wrapper dir (baked into the template, not pulled from a captured variable, so the wrapper still works if a future caller forgets to setZDOTDIRin the spawn env).The renderer-side template (
src/main/providers/local-pty-shell-ready.ts) and the daemon mirror (src/main/daemon/shell-ready.ts) get the same treatment.Test plan
Automated (vitest, 38/38 passing locally):
unset ZDOTDIR, function wrapper, recapture, baked re-pin).getAttributionShellLaunchConfig: same wrapper bytes on disk, sameZDOTDIR/ORCA_ORIG_ZDOTDIRin the launch env,ORCA_SHELL_READY_MARKER=0,supportsReadyMarker=false.which zsh, skipped on Windows / missing zsh) source the generated.zshenvin a tempHOMEand assert the resultingZDOTDIR/ORCA_ORIG_ZDOTDIR:.zshenvexportsZDOTDIR=$XDG_CONFIG_HOME/zsh).zshenvdoesreturn 0before setting anything)ZDOTDIRset by user.zshenv, spawn-envORCA_ORIG_ZDOTDIRsurvives)ZDOTDIR).zshenvitself echoes a wrapper-shapedZDOTDIR), including pathological multi-trailing-slash variantsManual:
~/.zshenvexportingZDOTDIR=$XDG_CONFIG_HOME/zshand a function defined in~/.config/zsh/.zshrc. Spotlight-launch Orca, thenorca terminal create --worktree active --command "<that function>". Function resolves.~/.zshrc, no XDG layout. Same command still works (regression check).orca terminal create --command "...". Recursion guard still fires — norecursion limit exceededfrom zsh.Risk
~/.zshenvis sourced once per wrapper invocation, same as before. The change is when it runs (now before the wrapper has committed to aZDOTDIR), not how many times..zshenvdoesn't touchZDOTDIR), the recapture falls through to the saved spawn-env value, which is what the old code already used. No observable change.ORCA_ORIG_ZDOTDIRends up at their XDG dir, and the downstream.zprofile/.zshrc/.zloginsource the right files.typeset -gvariables set by user.zshenvpropagate out of the function unchanged (zsh's default scope is global). See the trade-offs section below for the barelocal/typesetcaveat..zshenv" to "after the recapture." Behavior is preserved: if user.zshenvsomehow exports a wrapper-shapedZDOTDIR, it gets normalized away before the downstream rcfiles run._orca_spawn_orig_zdotdiris a top-level shell variable (not function-scoped). It lives in the same scope as user.zshenv, so a.zshenvthat explicitly clobbers it (extremely unlikely given the underscore prefix) loses the spawn-env fallback path.Known function-scope trade-offs
Sourcing
~/.zshenvinside__orca_source_user_zshenvmeans a few zsh constructs at the top level of user.zshenvbehave function-scoped under the wrapper that they wouldn't under a vanilla zsh launch. None are silent breakages; all have a documented escape hatch:setopt LOCAL_OPTIONS/LOCAL_TRAPSrevert at function exit.TRAPEXIT/trap '...' EXITinside~/.zshenvfires on function return, not shell exit. Move such traps to~/.zprofileor~/.zshrc.local/typeset(without-g) in~/.zshenvbecomes function-local. Usetypeset -gorexportif the binding needs to outlive~/.zshenv.These trade-offs are inherent to early-return survival — sourcing
~/.zshenvdirectly at top level would let[[ -o interactive ]] || returnabort the entire wrapper, which is a worse failure mode (no ready marker, broken startup commands, broken attribution shim).Out of scope
ZDOTDIR-style indirection. Users with custom layouts already source from~/.bash_profile, which the wrapper sources directly. No analogous fix is needed in the bash wrapper.ensureShellReadyWrappers()already short-circuits onprocess.platform === 'win32'.resolveOriginalZdotdir()(Node side): unchanged. It still strips wrapper-shaped values out of the spawn env so we don't poison the recapture fallback.🤖 Generated with Claude Code