diff --git a/apps/server/src/project/ProjectSetupScriptRunner.test.ts b/apps/server/src/project/ProjectSetupScriptRunner.test.ts index c495feb4cef66..93b2629f33fc0 100644 --- a/apps/server/src/project/ProjectSetupScriptRunner.test.ts +++ b/apps/server/src/project/ProjectSetupScriptRunner.test.ts @@ -142,6 +142,20 @@ describe("ProjectSetupScriptRunner", () => { }), ); + it.effect("a fish setup command that fails to parse still reports completion", () => + Effect.gen(function* () { + const command = 'node "${HOME:-.}/x.mjs" setup'; + const { writes } = yield* setup({ shell: "/usr/bin/fish", command }); + yield* TestClock.adjust("3 seconds"); + const [check, ...rest] = (yield* Queue.take(writes)).split("\r"); + expect(check).toContain( + `'node "\${HOME:-.}/x.mjs" setup' | '/usr/bin/fish' --no-config --no-execute; or begin; set -l s $status; for __t3i in (seq 3); read -l --prompt-str '' __t3l; end; printf`, + ); + // Typed on its own line so the fish line editor still expands abbreviations. + expect(rest).toContain(command); + }), + ); + it.effect("types an escaped Nushell wrapper once its line editor starts", () => Effect.gen(function* () { const command = diff --git a/apps/server/src/project/ProjectSetupScriptRunner.ts b/apps/server/src/project/ProjectSetupScriptRunner.ts index 1c94a3f118757..01bccf4ab757b 100644 --- a/apps/server/src/project/ProjectSetupScriptRunner.ts +++ b/apps/server/src/project/ProjectSetupScriptRunner.ts @@ -227,9 +227,14 @@ function wrapPosixCommand(command: string, sentinel: string): string { return lines.join("\r "); } +function quoteFish(value: string): string { + return `'${value.replace(/[\\']/g, "\\$&")}'`; +} + function wrapCommandForCompletion( command: string, shell: CompletionShell, + shellPath: string, sentinel: string, ): string { const body = command.replace(/\r?\n/g, "\r"); @@ -237,7 +242,18 @@ function wrapCommandForCompletion( case "powershell": return `$global:LASTEXITCODE = $null; & {\r${body}\r}; if ($null -ne $LASTEXITCODE) { $__t3c = $LASTEXITCODE } elseif ($?) { $__t3c = 0 } else { $__t3c = 1 }; Write-Host "${sentinel}$__t3c"`; case "fish": - return `begin\r${body}\rend; printf '\\n${sentinel}%s\\n' $status`; + // The closing line protects the sentinel from trailing comments, heredocs and scripts reading stdin. + // Interactive fish leaves a body that fails to parse in its editor, so it is parse-checked first, except on + // fish before 3.3 (no --no-config) or when it uses an abbreviation, which only the editor expands. A failed + // check reads away the lines typed after it, so none are left in the editor of a shell a retry reuses. + return [ + "begin; if string match -qr '^(3\\.([3-9]|[1-9][0-9])|[4-9]|[1-9][0-9])\\.' -- $version", + "and not abbr --show | string match -q -- '* --regex *'", + "set -l a (abbr --list | string escape --style=regex)", + `if test (count $a) -eq 0; or not string match -qr -- '(^|[\\s;|&(])('(string join '|' -- $a)')($|[\\s;|&)])' ${quoteFish(body)}`, + `printf '%s' ${quoteFish(body)} | ${quoteFish(shellPath)} --no-config --no-execute; or begin; set -l s $status; for __t3i in (seq ${body.split("\r").length + 2}); read -l --prompt-str '' __t3l; end; printf '\\n${sentinel}%s\\n' $s; end`, + `end; end; end\rbegin\r${body}\rend; printf '\\n${sentinel}%s\\n' $status`, + ].join("; "); case "nushell": { const escaped = command.replace(/["\\\x00-\x1f]/g, (char) => char === '"' || char === "\\" ? `\\${char}` : `\\u{${char.charCodeAt(0).toString(16)}}`, @@ -576,6 +592,7 @@ export const make = Effect.gen(function* () { commandLine = wrapCommandForCompletion( script.command, completionShell, + shell, completionSentinel(completionToken), ); }