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:
2026-06-15 17:46:11 +02:00
co-authored by Claude Opus 4.8
parent ca08c2a17d
commit 03d053274e
13 changed files with 269 additions and 87 deletions
+2 -17
View File
@@ -130,23 +130,8 @@ void main() {
});
});
group('expandedPath', () {
test('non-macOS returns PATH unchanged', () {
expect(expandedPath('/a:/b', macOS: false, home: '/home/x'), '/a:/b');
});
test('macOS prepends missing user + homebrew bins', () {
final out = expandedPath('/usr/bin', macOS: true, home: '/home/x').split(':');
expect(out, contains('/home/x/.local/bin'));
expect(out, contains('/opt/homebrew/bin'));
expect(out.last, '/usr/bin');
});
test('macOS does not duplicate entries already on PATH', () {
final out = expandedPath('/opt/homebrew/bin:/usr/bin', macOS: true, home: '');
expect('/opt/homebrew/bin'.allMatches(out).length, 1);
});
});
// PATH expansion moved to the shared resolver (T-439); its logic is covered by
// expandToolPath in toolchain_paths_test + shell_env_test.
group('install', () {
test('fails clearly when no bundled client is present', () {
+7 -11
View File
@@ -23,20 +23,16 @@ void main() {
});
});
group('expandedPath', () {
test('returns a non-empty string on every platform', () {
group('expandedPath (delegates to the shared resolver, T-439)', () {
test('non-empty on every platform', () {
expect(expandedPath, isNotEmpty);
});
test('on Linux/Windows, equals Platform.environment[PATH]', () {
if (Platform.isMacOS) return; // macOS path-merge tested separately.
expect(expandedPath, Platform.environment['PATH']);
});
test('on macOS, includes the well-known extras', () {
if (!Platform.isMacOS) return;
// Homebrew is the canonical one; at least one of these should appear.
expect(expandedPath, anyOf(contains('/opt/homebrew/bin'), contains('/usr/local/bin')));
test('is a superset of the process PATH (never drops entries)', () {
final got = expandedPath.split(':').toSet();
for (final dir in (Platform.environment['PATH'] ?? '').split(':').where((e) => e.isNotEmpty)) {
expect(got, contains(dir));
}
});
});
+102
View File
@@ -0,0 +1,102 @@
/// Tests for the consolidated PATH resolver (T-439): the login-shell probe,
/// its graceful fallbacks, and the shared `expandToolPath` merge.
library;
import 'dart:io';
import 'package:clide/src/env/shell_env.dart';
import 'package:test/test.dart';
ProcessResult _ok(String path) => ProcessResult(1, 0, '__CLIDE_PATH__${path}__CLIDE_PATH__', '');
void main() {
setUp(debugResetLoginShellPath);
tearDown(debugResetLoginShellPath);
group('primeLoginShellPath', () {
test('caches the login shell PATH so currentSearchPath returns it', () async {
await primeLoginShellPath(shell: '/bin/zsh', run: (e, a) async => _ok('/opt/tool/bin:/usr/bin'));
expect(currentSearchPath(), '/opt/tool/bin:/usr/bin');
});
test('strips profile chatter around the sentinel-framed PATH', () async {
await primeLoginShellPath(shell: '/bin/bash', run: (e, a) async => ProcessResult(1, 0, 'MOTD: hi\n__CLIDE_PATH__/a:/b__CLIDE_PATH__', ''));
expect(currentSearchPath(), '/a:/b');
});
test('falls back to the process PATH on a non-zero exit', () async {
await primeLoginShellPath(shell: '/bin/bash', run: (e, a) async => ProcessResult(1, 1, '', 'boom'));
expect(currentSearchPath(), Platform.environment['PATH'] ?? '');
});
test('falls back when the probe throws (e.g. spawn failure)', () async {
await primeLoginShellPath(shell: '/bin/bash', run: (e, a) async => throw const ProcessException('sh', []));
expect(currentSearchPath(), Platform.environment['PATH'] ?? '');
});
test('falls back when the probe times out', () async {
await primeLoginShellPath(
shell: '/bin/bash',
timeout: const Duration(milliseconds: 20),
run: (e, a) => Future.delayed(const Duration(seconds: 5), () => _ok('/never')),
);
expect(currentSearchPath(), Platform.environment['PATH'] ?? '');
});
test('falls back when SHELL is empty', () async {
await primeLoginShellPath(shell: '', run: (e, a) async => _ok('/should/not/run'));
expect(currentSearchPath(), Platform.environment['PATH'] ?? '');
});
test('is idempotent — a second call does not re-probe', () async {
var calls = 0;
await primeLoginShellPath(
shell: '/bin/bash',
run: (e, a) async {
calls++;
return _ok('/first');
},
);
await primeLoginShellPath(
shell: '/bin/bash',
run: (e, a) async {
calls++;
return _ok('/second');
},
);
expect(calls, 1);
expect(currentSearchPath(), '/first');
});
});
group('resolvedToolPath', () {
test('unions the well-known dirs onto the resolved base', () async {
await primeLoginShellPath(shell: '/bin/bash', run: (e, a) async => _ok('/usr/bin'));
final got = resolvedToolPath();
// The resolved base is preserved; on macOS/Linux the user/local dirs are
// unioned in. (Windows passes through, so only assert the base is kept.)
expect(got.split(':'), contains('/usr/bin'));
if (Platform.isLinux || Platform.isMacOS) {
expect(got.split(':'), contains('/usr/local/bin'));
}
});
});
group('expandToolPath', () {
test('prepends missing user/local dirs (Linux), de-duplicated, base last', () {
final out = expandToolPath('/usr/bin', isMac: false, isLinux: true, home: '/home/u').split(':');
expect(out, contains('/home/u/.local/bin'));
expect(out, contains('/usr/local/bin'));
expect(out.last, '/usr/bin');
});
test('does not duplicate dirs already present', () {
final out = expandToolPath('/usr/local/bin:/usr/bin', isMac: false, isLinux: true, home: '');
expect('/usr/local/bin'.allMatches(out).length, 1);
});
test('passes the base through unchanged off macOS/Linux', () {
expect(expandToolPath('/a:/b', isMac: false, isLinux: false, home: '/home/u'), '/a:/b');
});
});
}