Skip to content
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

Open
KernelDeimos opened this issue Aug 9, 2021 · 4 comments
Open

Context only propagates to child commands the first time #1469

KernelDeimos opened this issue Aug 9, 2021 · 4 comments
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users kind/bug A bug in cobra; unintended behavior

Comments

@KernelDeimos
Copy link

KernelDeimos commented Aug 9, 2021

I have an application that can be called in two ways:

  1. manually invoke from the CLI
  2. process directives in specially-formatted lines of a text file, which contain CLI arguments in the same format

For the second use case, I'm creating a Context that I'm passing to ExecuteContext 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:

func init() {
    cobra.OverwriteChildContexts = true
}
@umarcor
Copy link
Contributor

umarcor commented Aug 26, 2021

Probably related to #908.

@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

@marckhouzam
Copy link
Collaborator

Very similar to #1109

@johnSchnake johnSchnake added kind/bug A bug in cobra; unintended behavior area/lib Methods and functions that exist in the cobra library and consumed by users labels Apr 7, 2022
@tarvitz
Copy link

tarvitz commented Sep 15, 2022

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
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users kind/bug A bug in cobra; unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants