-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Context only propagates to child commands the first time #1469
Comments
Probably related to #908. |
This issue is being marked as stale due to a long period of inactivity |
Very similar to #1109 |
I've got the same trouble as well. I've bypassed it by traversing back to the root command and using its context instead of using the current cmd.Context() Hacky, but it's working. // ExtractContext traverse backwards to the root command and extracts the context
// of it.
// More info: https://github.com/spf13/cobra/issues/1469
func ExtractContext(cmd *cobra.Command) context.Context {
if cmd == nil {
return nil
}
var (
ctx = cmd.Context()
p = cmd.Parent()
)
if cmd.Parent() == nil {
return ctx
}
for {
ctx = p.Context()
p = p.Parent()
if p == nil {
break
}
}
return ctx
} |
radhus
added a commit
to einride/aip-cli-go
that referenced
this issue
Oct 11, 2022
Sometimes cmd.Context() returns nil, specifically in this project when called with `help completion` as arguments. This seems to be a known issue, see e.g. [1] and [2]. Use workaround from [3] try fetch context from parents, otherwise default to empty Config, to avoid crashing. [1]: spf13/cobra#1469 [2]: spf13/cobra#1109 [3]: spf13/cobra#1469 (comment)
radhus
added a commit
to einride/aip-cli-go
that referenced
this issue
Oct 11, 2022
Sometimes cmd.Context() returns nil, specifically in this project when called with `help completion` as arguments. This seems to be a known issue, see e.g. [1] and [2]. Use workaround from [3] try fetch context from parents, otherwise default to empty Config, to avoid crashing. [1]: spf13/cobra#1469 [2]: spf13/cobra#1109 [3]: spf13/cobra#1469 (comment)
radhus
added a commit
to einride/aip-cli-go
that referenced
this issue
Oct 11, 2022
Sometimes cmd.Context() returns nil, specifically in this project when called with `help completion` as arguments. This seems to be a known issue, see e.g. [1] and [2]. Use workaround from [3] try fetch context from parents, otherwise default to empty Config, to avoid crashing. [1]: spf13/cobra#1469 [2]: spf13/cobra#1109 [3]: spf13/cobra#1469 (comment)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have an application that can be called in two ways:
For the second use case, I'm creating a
Context
that I'm passing toExecuteContext
for each of my directives. I noticed that the first time, sub-commands get my new context. However, for subsequent inputs the context is only updated on the root command.Is this behaviour intentional? I can imagine this being overlooked as Cobra commands are typically executed only once for an instance of a program. I realize fixing this would cause a regression, so I'd like to propose something like this as an opt-in for the behaviour I was expecting:
The text was updated successfully, but these errors were encountered: