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

Always propagate root context to child command #1875

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pietern
Copy link

@pietern pietern commented Dec 9, 2022

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.

@CLAassistant
Copy link

CLAassistant commented Dec 9, 2022

CLA assistant check
All committers have signed the CLA.

@marckhouzam
Copy link
Collaborator

Thanks @pietern.
Can you first sign the CLA: #1875 (comment)

@marckhouzam marckhouzam added kind/bug A bug in cobra; unintended behavior area/cobra-command Core `cobra.Command` implementations labels Dec 9, 2022
command_test.go Outdated Show resolved Hide resolved
cmd.ctx = c.ctx
}
// Pass context of root command to child command.
cmd.ctx = c.ctx
Copy link
Collaborator

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.

Copy link
Author

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:

cobra/command.go

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.
@github-actions github-actions bot removed the area/cobra-command Core `cobra.Command` implementations label Dec 11, 2022
@github-actions
Copy link

The Cobra project currently lacks enough contributors to adequately respond to all PRs. This bot triages issues and PRs according to the following rules:

  • After 60d of inactivity, lifecycle/stale is applied. - After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied and the PR is closed.
    You can:
  • Make a comment to remove the stale label and show your support. The 60 days reset. - If a PR has lifecycle/rotten and is closed, comment and ask maintainers if they'd be interested in reopening.

@pietern
Copy link
Author

pietern commented Feb 12, 2023

Not stale.

@libozh
Copy link

libozh commented Dec 27, 2023

I had a similar request: #2077 . I think they are trying to cover similar use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug A bug in cobra; unintended behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants