fix(env): one login-shell-derived PATH for every spawned tool (T-439)
Desktop/dock-launched clide inherits a sparse PATH (no ~/.local/bin, brew, nvm, …), so pql/git/claude and PTY tools went missing. T-347 fixed only the toolchain/pql path on Linux; env.dart's expander was still macOS-only and claude/PTY/git used the raw PATH — the breakage recurred per spawn site because there were three divergent expanders. Consolidate into one resolver (lib/src/env/shell_env.dart): - primeLoginShellPath(): probe the user's real login shell once at startup (`$SHELL -l -c`, sentinel-framed, bounded timeout, graceful fallback to the process PATH). Captures the user's actual PATH, not a hardcoded guess. - expandToolPath(): the canonical merge (moved from toolchain_paths, which re-exports it for its tests) — unions the well-known user/local bin dirs. - resolvedToolPath(): currentSearchPath() + expandToolPath, the single call every spawn site uses. Routed through it: PTY children (registry.dart now overrides PATH), git (env.dart → operations.dart), the toolchain probe (toolchain_paths), and hosted claude (agent_bootstrap). Primed in main.dart's !kIsWeb boot. Deleted the macOS-only env.dart copy and the cli_install copy. Tests: new shell_env_test (probe + every fallback + merge); env_test and cli_install_test updated to the consolidated surface. analyze clean, web wasm build still green, make test green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -23,6 +23,7 @@ library;
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:clide/src/env/shell_env.dart' show resolvedToolPath;
|
||||
import 'package:clide/src/ipc/paths.dart' show workspaceSocketPath;
|
||||
|
||||
// Web fence (T-438, D-100): `Abi.current()` (dart:ffi) is desktop-only; the web
|
||||
@@ -108,7 +109,10 @@ class AgentBootstrap {
|
||||
/// orchestrator merges both into one `--append-system-prompt`.
|
||||
AgentBootstrap agentBootstrap(String workspaceRoot, {Map<String, String>? base}) {
|
||||
final home = Platform.environment['HOME'];
|
||||
final currentPath = (base ?? Platform.environment)['PATH'] ?? Platform.environment['PATH'];
|
||||
// The login-shell-resolved PATH (T-439) so a hosted claude — and the tools it
|
||||
// shells out to — find user-installed components on a desktop launch, not just
|
||||
// the sparse GUI PATH. agentEnvDelta still prepends the clide-CLI dir.
|
||||
final currentPath = resolvedToolPath();
|
||||
final candidates = <String>[
|
||||
if (home != null && home.isNotEmpty) '$home/.local/bin',
|
||||
'$workspaceRoot/native/${currentNativeDirName()}',
|
||||
|
||||
@@ -14,6 +14,8 @@ library;
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:clide/src/env/shell_env.dart' show expandToolPath;
|
||||
|
||||
/// State of the `clide` shell command relative to the running GUI.
|
||||
enum CliInstallState {
|
||||
/// No `clide` resolves on PATH.
|
||||
@@ -209,7 +211,7 @@ class CliInstaller {
|
||||
|
||||
static String get _pathSep => Platform.isWindows ? ';' : ':';
|
||||
|
||||
String _expandedPath() => expandedPath(env['PATH'] ?? '', macOS: Platform.isMacOS, home: env['HOME'] ?? '');
|
||||
String _expandedPath() => expandToolPath(env['PATH'] ?? '', isMac: Platform.isMacOS, isLinux: Platform.isLinux, home: env['HOME'] ?? '');
|
||||
|
||||
/// `~/.local/bin` on every platform — on Windows that is
|
||||
/// `%USERPROFILE%\.local\bin`, the same convention the claude and
|
||||
@@ -234,16 +236,3 @@ final RegExp _devTreeClient = RegExp(r'(^|/)native/(linux|macos|windows)-(x64|ar
|
||||
/// PATH; it's a working client but not a packaged production install, so it's
|
||||
/// classified separately (T-256) rather than as a clean install.
|
||||
bool isDevTreeClient(String path) => _devTreeClient.hasMatch(path.replaceAll('\\', '/'));
|
||||
|
||||
/// Expand a `PATH` value. Mirrors `toolchain_paths.dart`: macOS GUI apps
|
||||
/// launch with a sparse PATH that omits the usual user/homebrew bins, so on
|
||||
/// macOS we prepend those (de-duplicated) before scanning. A top-level,
|
||||
/// platform-parameterized function so both branches are testable off-platform.
|
||||
String expandedPath(String base, {required bool macOS, String home = ''}) {
|
||||
if (!macOS) return base;
|
||||
final extras = <String>[if (home.isNotEmpty) '$home/.local/bin', '/opt/homebrew/bin', '/opt/homebrew/sbin', '/usr/local/bin'];
|
||||
final existing = base.split(':').toSet();
|
||||
final missing = extras.where((p) => !existing.contains(p));
|
||||
if (missing.isEmpty) return base;
|
||||
return [...missing, ...existing].join(':');
|
||||
}
|
||||
|
||||
@@ -10,6 +10,13 @@ library;
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:clide/src/env/shell_env.dart';
|
||||
|
||||
// The canonical PATH-expansion logic now lives in shell_env (T-439, the single
|
||||
// source of truth shared with git/pql/PTY/claude). Re-exported so existing
|
||||
// importers/tests keep resolving it from here.
|
||||
export 'package:clide/src/env/shell_env.dart' show expandToolPath;
|
||||
|
||||
/// Serializable result of tool resolution (crosses isolate boundary).
|
||||
class ResolvedPaths {
|
||||
const ResolvedPaths({this.git, this.pql, this.shell, this.gitEnv});
|
||||
@@ -144,25 +151,8 @@ String? _firstExisting(List<String> candidates) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Build expanded PATH inline — must be self-contained for isolate use.
|
||||
String _expandedPath() =>
|
||||
expandToolPath(Platform.environment['PATH'] ?? '', isMac: Platform.isMacOS, isLinux: Platform.isLinux, home: Platform.environment['HOME']);
|
||||
|
||||
/// Pure PATH-expansion logic, extracted so it's testable without touching the
|
||||
/// process environment.
|
||||
///
|
||||
/// A desktop-launched app (macOS or Linux) inherits a minimal PATH that lacks
|
||||
/// the user bin dirs where tools like `pql` install (`~/.local/bin`), so tool
|
||||
/// resolution fails even though a terminal launch would find them. Re-add the
|
||||
/// common user/local bin dirs — that any are missing means they're prepended,
|
||||
/// so they take precedence over a stale system copy (T-347). Homebrew dirs are
|
||||
/// macOS-only. On other platforms the base PATH passes through unchanged.
|
||||
String expandToolPath(String base, {required bool isMac, required bool isLinux, String? home}) {
|
||||
if (!isMac && !isLinux) return base;
|
||||
final h = home ?? '';
|
||||
final extras = <String>[if (h.isNotEmpty) '$h/.local/bin', if (isMac) '/opt/homebrew/bin', if (isMac) '/opt/homebrew/sbin', '/usr/local/bin'];
|
||||
final existing = base.split(':').toSet();
|
||||
final missing = extras.where((p) => !existing.contains(p));
|
||||
if (missing.isEmpty) return base;
|
||||
return [...missing, ...existing].join(':');
|
||||
}
|
||||
/// The full tool search PATH — the login-shell PATH (probed once at startup)
|
||||
/// unioned with the well-known user/local bin dirs, shared with every other
|
||||
/// spawn site via [shell_env] (T-439). In an isolate that never primed the
|
||||
/// probe it degrades to the process PATH + the well-known dirs (T-347).
|
||||
String _expandedPath() => resolvedToolPath();
|
||||
|
||||
@@ -51,6 +51,7 @@ import 'package:clide/src/daemon/search_commands.dart';
|
||||
import 'package:clide/src/editor/registry.dart' show EditorRegistry;
|
||||
import 'package:clide/src/git/client.dart';
|
||||
import 'package:clide/src/cli/argv_dispatch.dart';
|
||||
import 'package:clide/src/env/shell_env.dart' show primeLoginShellPath;
|
||||
import 'package:clide/src/ipc/envelope.dart';
|
||||
import 'package:clide/src/ipc/mcp_server.dart';
|
||||
import 'package:clide/src/ipc/paths.dart' show workspaceSocketPath, logDirectory;
|
||||
@@ -100,6 +101,11 @@ Future<void> main() async {
|
||||
LogLevel bootLogLevel = kReleaseMode ? LogLevel.warn : LogLevel.info;
|
||||
List<LogSink> bootLogSinks = const [];
|
||||
if (!kIsWeb) {
|
||||
// Resolve the user's real login-shell PATH once, before any tool resolution
|
||||
// or spawn — a desktop/dock launch inherits a sparse PATH that misses
|
||||
// ~/.local/bin, brew, nvm, etc. (T-439). Bounded + graceful: a slow/failed
|
||||
// probe just falls back to the process PATH + well-known dirs.
|
||||
await primeLoginShellPath();
|
||||
final bootSettings = SettingsStore(appDir: appDir);
|
||||
await bootSettings.load();
|
||||
startupWorkRoot = resolveStartupWorkspace(
|
||||
|
||||
Vendored
+93
@@ -0,0 +1,93 @@
|
||||
/// The single source of truth for the PATH clide hands to every tool it
|
||||
/// spawns — git, pql, the toolchain probe, PTY children, hosted claude (T-439).
|
||||
///
|
||||
/// A desktop/dock-launched GUI process inherits a minimal PATH (roughly
|
||||
/// `/usr/bin:/bin`): it never sources `~/.bashrc` / `~/.zprofile` /
|
||||
/// `/etc/profile.d` / brew shellenv, so `~/.local/bin`, Homebrew, and any
|
||||
/// user-customized dirs (nvm/pyenv/cargo/asdf/…) are absent and tool resolution
|
||||
/// fails even though a terminal launch would find them. Two layers, in order:
|
||||
///
|
||||
/// 1. [primeLoginShellPath] probes the user's actual login shell once at
|
||||
/// startup (`$SHELL -l -c …`) — the real PATH, not a guess — and caches it.
|
||||
/// 2. [expandToolPath] additionally unions in the well-known user/local bin
|
||||
/// dirs, so resolution still works when the probe is unavailable (Windows,
|
||||
/// timeout, spawn failure) or the shell's profile omits a dir we know.
|
||||
///
|
||||
/// Flutter-free (used by `GitClient` / `PqlClient` under `dart test`).
|
||||
library;
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
String? _loginShellPath;
|
||||
bool _primed = false;
|
||||
|
||||
/// Probe the user's login shell for its `PATH`, once, and cache it. Desktop-only
|
||||
/// — the caller guards on `!kIsWeb`. Idempotent. Graceful: on Windows (no
|
||||
/// login-shell convention), a missing `$SHELL`, a non-zero exit, a timeout, or a
|
||||
/// spawn failure, the cache stays null and [currentSearchPath] falls back to the
|
||||
/// process `PATH` (still hardcoded-merged by [expandToolPath]).
|
||||
///
|
||||
/// [run] is injectable for tests; [timeout] bounds the probe so a misbehaving
|
||||
/// profile can never hang startup.
|
||||
Future<void> primeLoginShellPath({
|
||||
Future<ProcessResult> Function(String executable, List<String> arguments)? run,
|
||||
String? shell,
|
||||
Duration timeout = const Duration(seconds: 4),
|
||||
}) async {
|
||||
if (_primed) return;
|
||||
_primed = true;
|
||||
if (Platform.isWindows) return; // PowerShell has no `-l -c` PATH convention.
|
||||
final sh = shell ?? Platform.environment['SHELL'];
|
||||
if (sh == null || sh.isEmpty) return;
|
||||
final runner = run ?? (e, a) => Process.run(e, a);
|
||||
try {
|
||||
// `-l -c`: a login shell (sources the profile files that set the real PATH)
|
||||
// but non-interactive (no prompt, no hang). Frame the value in sentinels so
|
||||
// any MOTD / profile chatter on stdout is stripped.
|
||||
final res = await runner(sh, ['-l', '-c', r'printf "__CLIDE_PATH__%s__CLIDE_PATH__" "$PATH"']).timeout(timeout);
|
||||
if (res.exitCode != 0) return;
|
||||
final out = res.stdout is String ? res.stdout as String : '';
|
||||
final m = RegExp(r'__CLIDE_PATH__(.*?)__CLIDE_PATH__', dotAll: true).firstMatch(out);
|
||||
final path = m?.group(1)?.trim();
|
||||
if (path != null && path.isNotEmpty) _loginShellPath = path;
|
||||
} catch (_) {
|
||||
// timeout / spawn failure → leave the cache null and fall back.
|
||||
}
|
||||
}
|
||||
|
||||
/// The base search PATH: the login-shell PATH if [primeLoginShellPath] resolved
|
||||
/// one, else the process `PATH`. Not yet merged with the well-known dirs — use
|
||||
/// [resolvedToolPath] for the full search path.
|
||||
String currentSearchPath() => _loginShellPath ?? Platform.environment['PATH'] ?? '';
|
||||
|
||||
/// The full PATH clide should hand to spawned tools: the login-shell/process
|
||||
/// PATH unioned with the well-known user/local bin dirs. The single resolver
|
||||
/// every spawn site calls.
|
||||
String resolvedToolPath() => expandToolPath(currentSearchPath(), isMac: Platform.isMacOS, isLinux: Platform.isLinux, home: Platform.environment['HOME']);
|
||||
|
||||
/// Pure PATH-expansion: prepend the well-known user/local bin dirs that a
|
||||
/// desktop launch drops, de-duplicated, so they take precedence over a stale
|
||||
/// system copy (T-347). Homebrew dirs are macOS-only. On platforms that aren't
|
||||
/// macOS/Linux the base passes through unchanged. Extracted so it's testable
|
||||
/// without touching the process environment.
|
||||
String expandToolPath(String base, {required bool isMac, required bool isLinux, String? home}) {
|
||||
if (!isMac && !isLinux) return base;
|
||||
final h = home ?? '';
|
||||
final extras = <String>[if (h.isNotEmpty) '$h/.local/bin', if (isMac) '/opt/homebrew/bin', if (isMac) '/opt/homebrew/sbin', '/usr/local/bin'];
|
||||
final existing = base.split(':').toSet();
|
||||
final missing = extras.where((p) => !existing.contains(p));
|
||||
if (missing.isEmpty) return base;
|
||||
return [...missing, ...existing].join(':');
|
||||
}
|
||||
|
||||
/// Test seam: force the cached login-shell PATH (and mark primed).
|
||||
void debugSetLoginShellPath(String? value) {
|
||||
_loginShellPath = value;
|
||||
_primed = true;
|
||||
}
|
||||
|
||||
/// Test seam: clear the cache so [primeLoginShellPath] probes again.
|
||||
void debugResetLoginShellPath() {
|
||||
_loginShellPath = null;
|
||||
_primed = false;
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import 'dart:convert';
|
||||
import 'dart:io' show Platform;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import '../env/shell_env.dart' show resolvedToolPath;
|
||||
import '../ipc/envelope.dart';
|
||||
import '../pty/pty_log.dart';
|
||||
import '../pty/pty_session.dart';
|
||||
@@ -53,6 +54,9 @@ class PaneRegistry {
|
||||
// Terminal defaults for the PTY child.
|
||||
final fullEnv = <String, String>{
|
||||
...Platform.environment,
|
||||
// The login-shell-resolved PATH so PTY children find user-installed tools
|
||||
// even on a desktop launch (T-439); an explicit caller PATH still wins.
|
||||
'PATH': resolvedToolPath(),
|
||||
'TERM': 'xterm-256color',
|
||||
'COLORTERM': 'truecolor',
|
||||
'LANG': 'en_US.UTF-8',
|
||||
|
||||
+7
-22
@@ -8,29 +8,14 @@
|
||||
/// the renderer can do true colour.
|
||||
library;
|
||||
|
||||
import 'dart:io';
|
||||
import 'package:clide/src/env/shell_env.dart' show resolvedToolPath;
|
||||
|
||||
/// On macOS, GUI apps inherit a minimal PATH that omits Homebrew,
|
||||
/// ~/.local/bin, and similar directories. This getter returns the
|
||||
/// platform PATH with those well-known directories merged in.
|
||||
/// On Linux/Windows it returns the PATH unchanged.
|
||||
String get expandedPath {
|
||||
_cachedPath ??= _buildExpandedPath();
|
||||
return _cachedPath!;
|
||||
}
|
||||
|
||||
String? _cachedPath;
|
||||
|
||||
String _buildExpandedPath() {
|
||||
final base = Platform.environment['PATH'] ?? '';
|
||||
if (!Platform.isMacOS) return base;
|
||||
final home = Platform.environment['HOME'] ?? '';
|
||||
final extras = <String>[if (home.isNotEmpty) '$home/.local/bin', '/opt/homebrew/bin', '/opt/homebrew/sbin', '/usr/local/bin'];
|
||||
final existing = base.split(':').toSet();
|
||||
final missing = extras.where((p) => !existing.contains(p));
|
||||
if (missing.isEmpty) return base;
|
||||
return [...missing, ...existing].join(':');
|
||||
}
|
||||
/// The full tool search PATH for PTY children and PATH-resolved subprocess
|
||||
/// lookup — delegates to the shared resolver ([resolvedToolPath], T-439) so
|
||||
/// every spawn site agrees: the login-shell PATH (probed once at startup)
|
||||
/// unioned with the well-known user/local bin dirs. Previously this was a
|
||||
/// macOS-only merge, so a Linux desktop launch left tools unresolvable.
|
||||
String get expandedPath => resolvedToolPath();
|
||||
|
||||
/// Base env clide builds for every PTY child. Callers merge with the
|
||||
/// user's environment — a child that needs user env like `HOME` /
|
||||
|
||||
Reference in New Issue
Block a user