Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 13 additions & 1 deletion pkgs/mcpx/internal/cli/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ func zshCompletion(cmds []Command, sch *settings.Schema) string {
b.WriteString("# Generated by `mcpx completion zsh`.\n\n")
b.WriteString("_mcpx() {\n local -a commands\n commands=(\n")
for _, c := range cmds {
fmt.Fprintf(&b, " %s:%s\n", c.Name, zshEscape(c.Summary))
// Quote each entry: summaries carry apostrophes, parens and glob
// brackets (issue #272), none of which survive unquoted.
fmt.Fprintf(&b, " '%s:%s'\n", c.Name, zshDescribe(c.Summary))
}
b.WriteString(" )\n\n")
b.WriteString(" if (( CURRENT == 2 )); then\n")
Expand Down Expand Up @@ -152,6 +154,16 @@ func zshEscape(s string) string {
return s
}

// zshDescribe prepares a _describe entry's description. The whole entry is
// emitted single-quoted, so the shell itself needs no escaping; what remains
// is _describe's own syntax, where a colon in the description must be
// backslash-escaped so it is not read as another name separator.
func zshDescribe(s string) string {
s = strings.ReplaceAll(s, ":", `\:`)
s = strings.ReplaceAll(s, `'`, `'\''`)
return s
}

func fishQuote(s string) string {
return "'" + strings.ReplaceAll(s, "'", `\'`) + "'"
}
52 changes: 52 additions & 0 deletions pkgs/mcpx/internal/cli/completion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cli

import (
"strings"
"testing"
)

// TestZshCompletionCommandsAreQuoted is the regression test for issue #272:
// `mcpx completion zsh | zsh -n` failed with "parse error near `;;'".
// Summaries carry apostrophes, parens and glob brackets, so every entry in
// the _describe commands array must be single-quoted; an unquoted '\” escape
// is only valid inside quotes.
func TestZshCompletionCommandsAreQuoted(t *testing.T) {
out, err := Completion("zsh")
if err != nil {
t.Fatal(err)
}
start := strings.Index(out, " commands=(\n")
if start < 0 {
t.Fatal("commands array not found in zsh completion")
}
rest := out[start+len(" commands=(\n"):]
end := strings.Index(rest, "\n )\n")
if end < 0 {
t.Fatal("commands array not terminated in zsh completion")
}
lines := strings.Split(rest[:end], "\n")
if len(lines) == 0 {
t.Fatal("commands array is empty")
}
for _, l := range lines {
if !strings.HasPrefix(l, " '") || !strings.HasSuffix(l, "'") {
t.Errorf("unquoted commands entry: %q", l)
}
}
// At least one summary really does contain an apostrophe, so this test
// guards the case that broke, not a vacuous shape.
if !strings.Contains(out, `server'\''s prompts`) {
t.Error("expected an apostrophe-bearing entry (server's prompts) in the commands array")
}
}

// TestZshDescribeEscapesColonsAndQuotes pins zshDescribe: the entry is
// single-quoted for the shell, and _describe's own colon separator inside the
// description is backslash-escaped.
func TestZshDescribeEscapesColonsAndQuotes(t *testing.T) {
got := zshDescribe("list a server's prompts: verbosely")
want := `list a server'\''s prompts\: verbosely`
if got != want {
t.Errorf("zshDescribe = %q, want %q", got, want)
}
}
Loading