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
Always propagate root context to child command #1875
base: main
Are you sure you want to change the base?
Conversation
Thanks @pietern. |
cmd.ctx = c.ctx | ||
} | ||
// Pass context of root command to child command. | ||
cmd.ctx = c.ctx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm worried think this could be a breaking change...
If a program calls ExecuteContext
on another command than that command would get a different context on the root if we always pass it down.
I'll be honest that I don't know if this is a valid scenario. But we have to be very careful since Cobra is used by so many programs. I'll have to test to see if there is a potential problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand what you mean. In fact without other changes executing a subcommand with a context would not have effect and it would use the root's context. The line of code that makes that happen turned out to not be covered by tests:
Line 1012 in 9235920
return c.Root().ExecuteC() |
I added a test to cover this scenario and changed the line such that a command's context is also passed down to the root if execute was called on a subcommand.
The context passed to the root command should propagate to its children not only on the first execution but also subsequent calls. Calling the same command multiple times is common when testing cobra applications.
5f0e7bb
to
8e2c959
Compare
The Cobra project currently lacks enough contributors to adequately respond to all PRs. This bot triages issues and PRs according to the following rules:
|
Not stale. |
I had a similar request: #2077 . I think they are trying to cover similar use cases. |
The context passed to the root command should propagate to its children not only on the first execution but also subsequent calls. Calling the same command multiple times is common when testing cobra applications.