From 574c4dbf5814f03b7419efc06197b3e3b5cbeca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sat, 23 May 2026 13:50:53 +0200 Subject: [PATCH] fix: completion install writes non-empty scripts GenBashCompletionV2/GenZshCompletion/GenFishCompletion write into the buffer as a side effect; capturing buf.String() in the return statement before the Gen* call runs means the buffer is always empty. Separate the call from the return to fix evaluation order. Also call InitDefaultCompletionCmd() before iterating root.Commands() so the lazily-initialized completion subtree is visible before Execute(). Co-Authored-By: Claude Sonnet 4.6 --- cmd/netssh/main.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/netssh/main.go b/cmd/netssh/main.go index 47adbc6..cc7300c 100644 --- a/cmd/netssh/main.go +++ b/cmd/netssh/main.go @@ -208,9 +208,10 @@ func rootCmd() *cobra.Command { }, } - // cobra automatically adds a "completion" subcommand; we extend it with "install". root.AddCommand(configureCmd(), searchCmd(), cacheCmd()) + // cobra builds the "completion" command lazily; force init so we can extend it. + root.InitDefaultCompletionCmd() for _, cmd := range root.Commands() { if cmd.Name() == "completion" { cmd.AddCommand(completionInstallCmd(root)) @@ -244,7 +245,8 @@ func completionInstallCmd(root *cobra.Command) *cobra.Command { file = filepath.Join(dir, "netssh") gen = func() ([]byte, error) { var buf strings.Builder - return []byte(buf.String()), root.GenBashCompletionV2(&buf, true) + err := root.GenBashCompletionV2(&buf, true) + return []byte(buf.String()), err } note = "Reload your shell or run: source " + file case "zsh": @@ -252,7 +254,8 @@ func completionInstallCmd(root *cobra.Command) *cobra.Command { file = filepath.Join(dir, "_netssh") gen = func() ([]byte, error) { var buf strings.Builder - return []byte(buf.String()), root.GenZshCompletion(&buf) + err := root.GenZshCompletion(&buf) + return []byte(buf.String()), err } note = "Make sure ~/.zfunc is in your fpath:\n fpath=(~/.zfunc $fpath)\n autoload -Uz compinit && compinit" case "fish": @@ -261,7 +264,8 @@ func completionInstallCmd(root *cobra.Command) *cobra.Command { file = filepath.Join(dir, "netssh.fish") gen = func() ([]byte, error) { var buf strings.Builder - return []byte(buf.String()), root.GenFishCompletion(&buf, true) + err := root.GenFishCompletion(&buf, true) + return []byte(buf.String()), err } note = "Reload your shell or start a new fish session." default: