@devries-cameron20 / t3code Mirror · read-only / commits / 0720280c45
Fork 0

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, 11:00 UTC

fix(server): preserve shell paths containing spaces and dashes (#37069)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 15, 2026 at 08:31 UTC 0720280c45d4225f786633180940255dcaaaddfa
2 files changed +66 -23
apps/server/src/terminal/Manager.test.ts
+37 -10
@@ -1977,29 +1977,56 @@ it.layer(
1977 }),
1978 );
1979
1980 - it.effect("starts a $SHELL whose path contains a space", () =>
1980 + it.effect("starts an existing $SHELL whose path contains spaces before a dash", () =>
1981 Effect.gen(function* () {
1982 + const fs = yield* FileSystem.FileSystem;
1983 + const { join } = yield* Path.Path;
1984 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-shell-path-" });
1985 + const bin = join(root, "Jane -Doe Smith", ".local", "bin");
1986 + yield* fs.makeDirectory(bin, { recursive: true });
1987 + yield* fs.writeFileString(join(bin, "fish"), "");
1988 + // Temp dirs are not POSIX-absolute on Windows, so stat maps a fixed POSIX prefix onto them.
1989 + const fixtureFs = FileSystem.FileSystem.of({
1990 + ...fs,
1991 + stat: (filePath) =>
1992 + fs.stat(
1993 + filePath.startsWith("/shell-fixture/")
1994 + ? join(root, filePath.slice("/shell-fixture/".length))
1995 + : filePath,
1996 + ),
1997 + });
1998 const { manager, ptyAdapter } = yield* createManager(5, {
1983 - env: { SHELL: "/Users/Jane Doe/.local/bin/fish" },
1984 - }).pipe(Effect.provide(layerWithHostPlatform("darwin")));
1999 + env: { SHELL: "/shell-fixture/Jane -Doe Smith/.local/bin/fish -l" },
2000 + }).pipe(
2001 + Effect.provide(layerWithHostPlatform("darwin")),
2002 + Effect.provideService(FileSystem.FileSystem, fixtureFs),
2003 + );
2004
2005 yield* manager.open(openInput());
2006
2007 expect(ptyAdapter.spawnInputs.map((input) => input.shell)).toEqual([
1989 - "/Users/Jane Doe/.local/bin/fish",
2008 + "/shell-fixture/Jane -Doe Smith/.local/bin/fish",
2009 ]);
2010 }),
2011 );
2012
1994 - it.effect("starts a quoted $SHELL whose path contains a space and a dash", () =>
2013 + it.effect("keeps missing or quoted $SHELL paths that contain spaces", () =>
2014 Effect.gen(function* () {
1996 - const { manager, ptyAdapter } = yield* createManager(5, {
1997 - env: { SHELL: '"/opt/my -shell/fish" -l' },
1998 - }).pipe(Effect.provide(layerWithHostPlatform("darwin")));
2015 + for (const [shell, expected] of [
2016 + ["/Users/Jane Doe/.local/bin/fish", "/Users/Jane Doe/.local/bin/fish"],
2017 + ['"/opt/my -shell/fish" -l', "/opt/my -shell/fish"],
2018 + ]) {
2019 + const { manager, ptyAdapter } = yield* createManager(5, {
2020 + env: { SHELL: shell },
2021 + }).pipe(Effect.provide(layerWithHostPlatform("darwin")));
2022
2000 - yield* manager.open(openInput());
2023 + yield* manager.open(openInput());
2024
2002 - expect(ptyAdapter.spawnInputs.map((input) => input.shell)).toEqual(["/opt/my -shell/fish"]);
2025 + expect(
2026 + ptyAdapter.spawnInputs.map((input) => input.shell),
2027 + shell,
2028 + ).toEqual([expected]);
2029 + }
2030 }),
2031 );
2032
apps/server/src/terminal/Manager.ts
+29 -13
@@ -527,10 +527,10 @@ function defaultShellResolver(platform: NodeJS.Platform, env: NodeJS.ProcessEnv)
527 return env.SHELL ?? "bash";
528 }
529
530 -function normalizeShellCommand(
530 +const normalizeShellCommand = Effect.fn("terminal.normalizeShellCommand")(function* (
531 value: string | undefined,
532 platform: NodeJS.Platform,
533 -): string | null {
533 +) {
534 if (!value) return null;
535 const trimmed = value.trim();
536 if (trimmed.length === 0) return null;
@@ -539,12 +539,24 @@ function normalizeShellCommand(
539 return trimmed;
540 }
541
542 - // A shell path can contain spaces, so only text from the first flag (`bash -l`) is dropped,
543 - // and a quoted path is kept whole even when it contains " -".
542 const quoted = /^(['"])(.*?)\1(?:\s|$)/.exec(trimmed);
545 - const command = quoted ? quoted[2] : trimmed.split(/\s+(?=-)/)[0]?.replace(/^['"]|['"]$/g, "");
546 - return command || null;
547 -}
543 + if (quoted) return quoted[2] || null;
544 +
545 + if (trimmed.startsWith("/") && /\s/.test(trimmed)) {
546 + const fs = yield* FileSystem.FileSystem;
547 + const ends = [...trimmed.matchAll(/\s+/g)].map((match) => match.index);
548 + // Unquoted paths are ambiguous; prefer the longest existing file over flag syntax.
549 + for (const end of [trimmed.length, ...ends.reverse()]) {
550 + const candidate = trimmed.slice(0, end);
551 + const info = yield* fs
552 + .stat(candidate)
553 + .pipe(Effect.catchTags({ PlatformError: () => Effect.succeed(null) }));
554 + if (info?.type === "File") return candidate;
555 + }
556 + }
557 +
558 + return trimmed.split(/\s+(?=-)/)[0]?.replace(/^['"]|['"]$/g, "") || null;
559 +});
560
561 function basenameForPlatform(command: string, platform: NodeJS.Platform): string {
562 const normalized =
@@ -620,13 +632,13 @@ function uniqueShellCandidates(candidates: Array<ShellCandidate | null>): ShellC
632 return ordered;
633 }
634
623 -function resolveShellCandidates(
635 +const resolveShellCandidates = Effect.fn("terminal.resolveShellCandidates")(function* (
636 shellResolver: () => string,
637 platform: NodeJS.Platform,
638 env: NodeJS.ProcessEnv,
627 -): ShellCandidate[] {
639 +) {
640 const requested = shellCandidateFromCommand(
629 - normalizeShellCommand(shellResolver(), platform),
641 + yield* normalizeShellCommand(shellResolver(), platform),
642 platform,
643 );
644
@@ -644,7 +656,7 @@ function resolveShellCandidates(
656
657 return uniqueShellCandidates([
658 requested,
647 - shellCandidateFromCommand(normalizeShellCommand(env.SHELL, platform), platform),
659 + shellCandidateFromCommand(yield* normalizeShellCommand(env.SHELL, platform), platform),
660 shellCandidateFromCommand("/bin/zsh", platform),
661 shellCandidateFromCommand("/bin/bash", platform),
662 shellCandidateFromCommand("/bin/sh", platform),
@@ -652,7 +664,7 @@ function resolveShellCandidates(
664 shellCandidateFromCommand("bash", platform),
665 shellCandidateFromCommand("sh", platform),
666 ]);
655 -}
667 +});
668
669 function isRetryableShellSpawnError(error: PtyAdapter.PtySpawnError): boolean {
670 const queue: unknown[] = [error];
@@ -2691,7 +2703,11 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
2703 increment(terminalSessionsTotal, { lifecycle: eventType }).pipe(
2704 Effect.andThen(
2705 Effect.gen(function* () {
2694 - const shellCandidates = resolveShellCandidates(shellResolver, platform, baseEnv);
2706 + const shellCandidates = yield* resolveShellCandidates(
2707 + shellResolver,
2708 + platform,
2709 + baseEnv,
2710 + ).pipe(Effect.provideService(FileSystem.FileSystem, fileSystem));
2711 const terminalEnv = createTerminalSpawnEnv(
2712 baseEnv,
2713 session.runtimeEnv,