Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/host/agent-adapter/src/__tests__/shell-env.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { parseShellEnvironment } from '../shell-env';
import { parseShellEnvironment, shellProbeCommand } from '../shell-env';

describe('project shell environment', () => {
it('ignores shell output and parses the marked JSON environment', () => {
Expand All @@ -22,3 +22,15 @@ describe('project shell environment', () => {
);
});
});

describe('shell probe command', () => {
it('emits fish syntax for fish and POSIX syntax otherwise', () => {
expect(shellProbeCommand('/opt/homebrew/bin/fish', 'PRINT_ENV')).toBe(
'if command -v direnv >/dev/null 2>&1; exec direnv exec "$PWD" PRINT_ENV; else; exec PRINT_ENV; end',
);
expect(shellProbeCommand('/bin/zsh', 'PRINT_ENV')).toBe(
'if command -v direnv >/dev/null 2>&1; then exec direnv exec "$PWD" PRINT_ENV; else exec PRINT_ENV; fi',
);
expect(shellProbeCommand('/bin/bash', 'PRINT_ENV')).toContain('then exec');
});
});
12 changes: 11 additions & 1 deletion packages/host/agent-adapter/src/shell-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const CAPTURE_ENV = {
ELECTRON_NO_ATTACH_CONSOLE: '1',
LINKCODE_RESOLVING_ENVIRONMENT: '1',
};
const FISH_SHELL_PATTERN = /(?:^|\/)fish$/;

const ShellEnvironmentSchema = z
.record(z.string(), z.string())
.refine((value) => Boolean(value.PATH), 'PATH is required');
Expand All @@ -28,7 +30,7 @@ export async function resolveShellEnvironment(
const marker = randomUUID().replaceAll('-', '');
const expression = `"${marker}" + JSON.stringify(process.env) + "${marker}"`;
const printEnv = `${quoteShellArg(process.execPath)} -p ${quoteShellArg(expression)}`;
const command = `if command -v direnv >/dev/null 2>&1; then exec direnv exec "$PWD" ${printEnv}; else exec ${printEnv}; fi`;
const command = shellProbeCommand(shell, printEnv);
let stdout: string;
try {
({ stdout } = await execFileAsync(shell, ['-i', '-l', '-c', command], {
Expand All @@ -54,6 +56,14 @@ export async function resolveShellEnvironment(
return resolved;
}

/** fish rejects POSIX if/then/else — it alone gets its own syntax; every other login shell is POSIX. */
Comment thread
adminliu-main marked this conversation as resolved.
export function shellProbeCommand(shell: string, printEnv: string): string {
if (FISH_SHELL_PATTERN.test(shell)) {
return `if command -v direnv >/dev/null 2>&1; exec direnv exec "$PWD" ${printEnv}; else; exec ${printEnv}; end`;
}
return `if command -v direnv >/dev/null 2>&1; then exec direnv exec "$PWD" ${printEnv}; else exec ${printEnv}; fi`;
}

export function parseShellEnvironment(
stdout: string,
marker: string,
Expand Down
Loading