From 5dad42575c39e1cca2d28fbc6f8d69f66add2bec Mon Sep 17 00:00:00 2001
From: maxlandon <maximelandon@gmail.com>
Date: Sat, 30 Sep 2023 20:48:28 +0200
Subject: [PATCH] Avoid infinitely walking up the command tree

---
 completions.go | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/completions.go b/completions.go
index 68eb445b..0ee4f28a 100644
--- a/completions.go
+++ b/completions.go
@@ -159,11 +159,17 @@ func (c *Command) GetFlagCompletion(flag *pflag.Flag) (func(cmd *Command, args [
 
 	completionFunc, exists := c.flagCompletionFunctions[flag]
 
+	// If found it here, return now
 	if completionFunc != nil && exists {
 		return completionFunc, exists
 	}
 
-	// If not found on the current, walk up the command tree.
+	// If we are already at the root command level, return anyway
+	if !c.HasParent() {
+		return nil, false
+	}
+
+	// Or walk up the command tree.
 	return c.Parent().GetFlagCompletion(flag)
 }