Skip to content

Commit 12a62ed

Browse files
KenDJohnsonmaria-rcksjuliusmarminge
authored
fix(server): worktree setup no longer hangs when the shell is Nushell (pingdotgg#29461)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 3de49ba commit 12a62ed

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎apps/server/src/project/ProjectSetupScriptRunner.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const openSnapshot = (history: string): TerminalSessionSnapshot => ({
3838
});
3939

4040
const makeHarness = Effect.fn(function* (options: {
41+
readonly command?: string;
4142
readonly history?: string;
4243
readonly opening?: Deferred.Deferred<void>;
4344
readonly shell?: string | null;
@@ -88,7 +89,7 @@ const makeHarness = Effect.fn(function* (options: {
8889
{
8990
id: "setup",
9091
name: "Setup",
91-
command: "vp i",
92+
command: options.command ?? "vp i",
9293
icon: "configure",
9394
runOnWorktreeCreate: true,
9495
},
@@ -102,6 +103,7 @@ const makeHarness = Effect.fn(function* (options: {
102103

103104
const setup = Effect.fn(function* (
104105
options: {
106+
readonly command?: string;
105107
readonly history?: string;
106108
readonly shell?: string | null;
107109
} = {},
@@ -123,6 +125,32 @@ describe("ProjectSetupScriptRunner", () => {
123125
}),
124126
);
125127

128+
it.effect("types an escaped Nushell wrapper once its line editor starts", () =>
129+
Effect.gen(function* () {
130+
const command =
131+
'print "quoted"; print \'C:\\tools\'; print "$env.PATH"\nexit 7 # comment\r\n\t';
132+
const escaped =
133+
'print \\"quoted\\"; print \'C:\\\\tools\'; print \\"$env.PATH\\"\\u{a}exit 7 # comment\\u{d}\\u{a}\\u{9}';
134+
const { writes, emit } = yield* setup({ shell: "/opt/homebrew/bin/nu", command });
135+
yield* TestClock.adjust("1 second");
136+
expect(yield* Queue.size(writes)).toBe(0);
137+
yield* emit({ ...event, type: "output", data: "> \x1b[?2004h" });
138+
const written = yield* Queue.take(writes);
139+
const sentinel = written.match(/__T3_SETUP_DONE___[0-9a-f]{32}:/)?.[0];
140+
expect(sentinel).toBeDefined();
141+
expect(written).toBe(
142+
` ${[
143+
"$env.LAST_EXIT_CODE = 0",
144+
"let __t3f = try { mktemp --tmpdir-path $env.T3CODE_WORKTREE_PATH --suffix .nu .t3-setup-XXXXXXXX } catch {|e| print -e $e.msg; null }",
145+
`if $__t3f == null { $env.LAST_EXIT_CODE = 1 } else { try { [$"rm --permanent ($__t3f | to nuon)" "${escaped}"] | str join (char newline) | save --force --raw $__t3f; ^$nu.current-exe --login --commands $"cd $env.T3CODE_WORKTREE_PATH; source ($__t3f | to nuon)" } catch {|e| if $e != null and $e.exit_code? == null { print -e $e.msg }; if $env.LAST_EXIT_CODE == 0 { $env.LAST_EXIT_CODE = 1 } } }`,
146+
"let __t3c = $env.LAST_EXIT_CODE",
147+
"try { if $__t3f != null and ($__t3f | path exists) { rm --permanent $__t3f } } catch {|e| print -e $e.msg }",
148+
`print $"\\n${sentinel}($__t3c)"`,
149+
].join("; ")}\r`,
150+
);
151+
}),
152+
);
153+
126154
it.effect("shows a startup prompt and types only once the line editor starts", () =>
127155
Effect.gen(function* () {
128156
const { result, writes, outputLines, emit } = yield* setup();

‎apps/server/src/project/ProjectSetupScriptRunner.ts‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ function stripTerminalControl(text: string): string {
154154
.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, "");
155155
}
156156

157-
// Line editors (zsh, fish, bash 5.1+) enable bracketed paste when they start reading a command.
157+
// Line editors (zsh, fish, nu, bash 5.1+) enable bracketed paste when they start reading a command.
158158
const BRACKETED_PASTE_ON = "\x1b[?2004h";
159159
const BRACKETED_PASTE_OFF = "\x1b[?2004l";
160160
// Longer than one subprocess poll plus its ps timeout, so a startup prompt is seen as busy first.
@@ -166,12 +166,13 @@ function lineEditorReading(text: string, previous: boolean): boolean {
166166
return on === off ? previous : on > off;
167167
}
168168

169-
type CompletionShell = "posix" | "fish" | "powershell";
169+
type CompletionShell = "posix" | "fish" | "nushell" | "powershell";
170170

171171
function resolveCompletionShell(shell: string): CompletionShell | null {
172172
const name = (shell.split(/[/\\]/).at(-1) ?? shell).toLowerCase().replace(/\.exe$/, "");
173173
if (name === "cmd") return null;
174174
if (name === "fish") return "fish";
175+
if (name === "nu") return "nushell";
175176
if (name === "pwsh" || name === "powershell") return "powershell";
176177
return "posix";
177178
}
@@ -188,6 +189,20 @@ function wrapCommandForCompletion(
188189
return `$global:LASTEXITCODE = $null; & {\r${body}\r}; if ($null -ne $LASTEXITCODE) { $__t3c = $LASTEXITCODE } elseif ($?) { $__t3c = 0 } else { $__t3c = 1 }; Write-Host "${sentinel}$__t3c"`;
189190
case "fish":
190191
return `begin\r${body}\rend; printf '\\n${sentinel}%s\\n' $status`;
192+
case "nushell": {
193+
const escaped = command.replace(/["\\\x00-\x1f]/g, (char) =>
194+
char === '"' || char === "\\" ? `\\${char}` : `\\u{${char.charCodeAt(0).toString(16)}}`,
195+
);
196+
// Nu resolves imports before running cd; source a file rooted in the worktree.
197+
return [
198+
"$env.LAST_EXIT_CODE = 0",
199+
"let __t3f = try { mktemp --tmpdir-path $env.T3CODE_WORKTREE_PATH --suffix .nu .t3-setup-XXXXXXXX } catch {|e| print -e $e.msg; null }",
200+
`if $__t3f == null { $env.LAST_EXIT_CODE = 1 } else { try { [$"rm --permanent ($__t3f | to nuon)" "${escaped}"] | str join (char newline) | save --force --raw $__t3f; ^$nu.current-exe --login --commands $"cd $env.T3CODE_WORKTREE_PATH; source ($__t3f | to nuon)" } catch {|e| if $e != null and $e.exit_code? == null { print -e $e.msg }; if $env.LAST_EXIT_CODE == 0 { $env.LAST_EXIT_CODE = 1 } } }`,
201+
"let __t3c = $env.LAST_EXIT_CODE",
202+
"try { if $__t3f != null and ($__t3f | path exists) { rm --permanent $__t3f } } catch {|e| print -e $e.msg }",
203+
`print $"\\n${sentinel}($__t3c)"`,
204+
].join("; ");
205+
}
191206
case "posix":
192207
return `( ${body}\r); printf '\\n${sentinel}%s\\n' "$?"`;
193208
}
@@ -517,9 +532,9 @@ export const make = Effect.gen(function* () {
517532
// Return once the terminal exists so callers can show it while a startup prompt waits.
518533
// PowerShell and cmd have no line-editor signal.
519534
yield* (
520-
completionShell === "posix" || completionShell === "fish"
521-
? readiness.wait
522-
: Effect.succeed("ready" as const)
535+
completionShell === "powershell" || completionShell === null
536+
? Effect.succeed("ready" as const)
537+
: readiness.wait
523538
).pipe(
524539
Effect.flatMap((shell) =>
525540
shell === "exited" || commandLine === null

0 commit comments

Comments
 (0)