Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/server/src/project/ProjectSetupScriptRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-execute 2>&1); or begin; set -l s $status; for __t3i in (seq 3); read -l --prompt-str '' __t3l; end; printf '%s\\n' $e; 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 =
Expand Down
16 changes: 15 additions & 1 deletion apps/server/src/project/ProjectSetupScriptRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,30 @@ 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");
switch (shell) {
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 (fish before
// 3.2 rejects unknown commands there). A failed check reads away the lines typed after it, so a retry finds an
// empty editor, then prints the error last so the setup output tail still shows it.
return [
"begin; if string match -qr '^(3\\.([2-9]|[1-9][0-9])|[4-9]|[1-9][0-9])\\.' -- $version",
`set -l e (printf '%s' ${quoteFish(body)} | ${quoteFish(shellPath)} --no-execute 2>&1); or begin; set -l s $status; for __t3i in (seq ${body.split("\r").length + 2}); read -l --prompt-str '' __t3l; end; printf '%s\\n' $e; printf '\\n${sentinel}%s\\n' $s; 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)}}`,
Expand Down Expand Up @@ -576,6 +589,7 @@ export const make = Effect.gen(function* () {
commandLine = wrapCommandForCompletion(
script.command,
completionShell,
shell,
completionSentinel(completionToken),
);
}
Expand Down
Loading