Skip to content

Commit ba4d01c

Browse files
fix(shared): login-shell environment probe ignores DEBUG trap output (#32646)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 631ea1b commit ba4d01c

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎packages/shared/src/shell.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,39 @@ describe("readEnvironmentFromLoginShell", () => {
118118
},
119119
);
120120

121+
it.runIf(NodeFS.existsSync("/bin/bash"))(
122+
"ignores probe markers a bash DEBUG trap echoes into the terminal title",
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`;
127+
const bashEnv = NodePath.join(home, "env.bash");
128+
NodeFS.writeFileSync(
129+
bashEnv,
130+
[
131+
`export PATH=${JSON.stringify(shellPath)}`,
132+
`trap 'echo -ne "\\033]0;\${BASH_COMMAND}\\007"' DEBUG`,
133+
].join("\n"),
134+
);
135+
136+
expect(
137+
readEnvironmentFromLoginShell(
138+
"/bin/bash",
139+
["XDG_DATA_HOME", "PATH"],
140+
// Non-interactive bash reads only BASH_ENV, not the host's /etc startup files that -il runs.
141+
(file, [, command = ""], options) =>
142+
NodeChildProcess.execFileSync(file, ["-c", command], {
143+
...options,
144+
env: { HOME: home, PATH: "/usr/bin:/bin", TERM: "dumb", BASH_ENV: bashEnv },
145+
}),
146+
),
147+
).toEqual({ PATH: shellPath });
148+
} finally {
149+
NodeFS.rmSync(home, { recursive: true, force: true });
150+
}
151+
},
152+
);
153+
121154
it("extracts multiple environment variables from a login shell command", () => {
122155
const execFile = vi.fn<
123156
(

‎packages/shared/src/shell.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,16 @@ function envCaptureEnd(name: string): string {
208208
return `__T3CODE_ENV_${name}_END__`;
209209
}
210210

211+
const DEBUG_TRAP_SHELLS = new Set(["bash", "zsh", "sh", "ksh", "ksh93"]);
212+
211213
export function buildEnvironmentCaptureCommand(
212214
shell: string,
213215
names: ReadonlyArray<string>,
214216
): string {
215217
const executableName = NodePath.basename(shell).toLowerCase();
216218
const isNushell = executableName.startsWith("nu");
217219

218-
return names
220+
const capture = names
219221
.map((name) => {
220222
if (!SHELL_ENV_NAME_PATTERN.test(name)) {
221223
throw new Error(`Unsupported environment variable name: ${name}`);
@@ -249,6 +251,12 @@ export function buildEnvironmentCaptureCommand(
249251
].join("; ");
250252
})
251253
.join("; ");
254+
255+
// A DEBUG trap that echoes each command (e.g. into the terminal title) would print the markers.
256+
// OpenBSD ksh (also its /bin/sh) exits on `trap - DEBUG`, so try it in a subshell first.
257+
return DEBUG_TRAP_SHELLS.has(executableName)
258+
? `(trap - DEBUG) 2>/dev/null && trap - DEBUG; ${capture}`
259+
: capture;
252260
}
253261

254262
function buildWindowsEnvironmentCaptureCommand(names: ReadonlyArray<string>): string {

0 commit comments

Comments
 (0)