The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 16:00 UTC
fix(shared): capture shell values after profiles replace PATH (#35617)
Co-authored-by: Simone <185146821+Lucenx9@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Simone committed
Nov 14, 2026 at 08:04 UTC
388424db26888211304931461e947de1b6091c8d
2 files changed
+37
-7
packages/shared/src/shell.test.ts
+15
-4
@@ -119,16 +119,26 @@ describe("readEnvironmentFromLoginShell", () => {
119
);
120
121
it.runIf(NodeFS.existsSync("/bin/bash"))(
122
- "ignores probe markers a bash DEBUG trap echoes into the terminal title",
122
+ "captures values when the profile replaces PATH and a DEBUG trap echoes commands",
123
() => {
124
const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-bash-env-"));
125
try {
126
- const shellPath = `${NodePath.join(home, "bin")}:/usr/bin:/bin`;
126
+ const tools = NodePath.join(home, "tools 'quoted $dir");
127
+ NodeFS.mkdirSync(tools);
128
+ const printenv = NodeChildProcess.execFileSync("/bin/bash", ["-c", "type -P printenv"], {
129
+ encoding: "utf8",
130
+ env: { PATH: process.env.PATH },
131
+ }).trim();
132
+ NodeFS.copyFileSync(printenv, NodePath.join(tools, "printenv"));
133
+ vi.stubEnv("PATH", tools);
134
+ const shellPath = NodePath.join(home, "bin");
135
const bashEnv = NodePath.join(home, "env.bash");
136
NodeFS.writeFileSync(
137
bashEnv,
138
[
131
- `export PATH=${JSON.stringify(shellPath)}`,
139
+ `export PATH='${shellPath.replaceAll("'", "'\\''")}'`,
140
+ // Simulate a non-FHS host while retaining the real executable in its private prefix.
141
+ "function /usr/bin/printenv { return 127; }",
142
`trap 'echo -ne "\\033]0;\${BASH_COMMAND}\\007"' DEBUG`,
143
].join("\n"),
144
);
@@ -141,11 +151,12 @@ describe("readEnvironmentFromLoginShell", () => {
151
(file, [, command = ""], options) =>
152
NodeChildProcess.execFileSync(file, ["-c", command], {
153
...options,
144
- env: { HOME: home, PATH: "/usr/bin:/bin", TERM: "dumb", BASH_ENV: bashEnv },
154
+ env: { HOME: home, PATH: tools, TERM: "dumb", BASH_ENV: bashEnv },
155
}),
156
),
157
).toEqual({ PATH: shellPath });
158
} finally {
159
+ vi.unstubAllEnvs();
160
NodeFS.rmSync(home, { recursive: true, force: true });
161
}
162
},
packages/shared/src/shell.ts
+22
-3
@@ -209,6 +209,12 @@ function envCaptureEnd(name: string): string {
209
}
210
211
const DEBUG_TRAP_SHELLS = new Set(["bash", "zsh", "sh", "ksh", "ksh93"]);
212
+const POSIX_CAPTURE_SYNTAX = { assignment: "__T3CODE_PRINTENV=$(", orElse: " || " };
213
+const PRINTENV_CAPTURE_SYNTAX = new Map(
214
+ [...DEBUG_TRAP_SHELLS, "dash", "ash"].map((shell) => [shell, POSIX_CAPTURE_SYNTAX] as const),
215
+).set("fish", { assignment: "set -l __T3CODE_PRINTENV (", orElse: "; or " });
216
+// Network PATH entries must be probed inside the shell's deadline, away from the desktop thread.
217
+const PRINTENV_LOOKUP_COMMAND = `/bin/sh -c 'command -v printenv || { [ -n "$1" ] && PATH="$1" command -v printenv; } || printf %s /usr/bin/printenv' t3`;
218
219
export function buildEnvironmentCaptureCommand(
220
shell: string,
@@ -216,6 +222,16 @@ export function buildEnvironmentCaptureCommand(
222
): string {
223
const executableName = NodePath.basename(shell).toLowerCase();
224
const isNushell = executableName.startsWith("nu");
225
+ const syntax = PRINTENV_CAPTURE_SYNTAX.get(executableName);
226
+ const supportsFallback = syntax !== undefined;
227
+ const inheritedPath = resolvePathEnvironmentVariable(process.env)
228
+ .split(POSIX_PATH_DELIMITER)
229
+ .filter(Boolean)
230
+ .map((entry) => NodePath.posix.resolve(entry))
231
+ .join(POSIX_PATH_DELIMITER);
232
+ const lookupPrintenv = `${PRINTENV_LOOKUP_COMMAND} '${inheritedPath.replaceAll("'", "'\\''")}'`;
233
+ const { assignment, orElse } = syntax ?? POSIX_CAPTURE_SYNTAX;
234
+ const resolvePrintenv = `${assignment}${lookupPrintenv}); `;
235
236
const capture = names
237
.map((name) => {
@@ -246,7 +262,9 @@ export function buildEnvironmentCaptureCommand(
262
263
return [
264
`printf '%s\\n' '${envCaptureStart(name)}'`,
249
- `printenv ${name} || true`,
265
+ supportsFallback
266
+ ? `printenv ${name}${orElse}"$__T3CODE_PRINTENV" ${name}${orElse}true`
267
+ : `printenv ${name} || true`,
268
`printf '%s\\n' '${envCaptureEnd(name)}'`,
269
].join("; ");
270
})
@@ -254,9 +272,10 @@ export function buildEnvironmentCaptureCommand(
272
273
// A DEBUG trap that echoes each command (e.g. into the terminal title) would print the markers.
274
// OpenBSD ksh (also its /bin/sh) exits on `trap - DEBUG`, so try it in a subshell first.
275
+ const command = supportsFallback ? resolvePrintenv + capture : capture;
276
return DEBUG_TRAP_SHELLS.has(executableName)
258
- ? `(trap - DEBUG) 2>/dev/null && trap - DEBUG; ${capture}`
259
- : capture;
277
+ ? `(trap - DEBUG) 2>/dev/null && trap - DEBUG; ${command}`
278
+ : command;
279
}
280
281
function buildWindowsEnvironmentCaptureCommand(names: ReadonlyArray<string>): string {