fix(cobra): propagate context to subcommands in ExecuteC#2416
Open
techwizard31 wants to merge 1 commit into
Open
fix(cobra): propagate context to subcommands in ExecuteC#2416techwizard31 wants to merge 1 commit into
techwizard31 wants to merge 1 commit into
Conversation
Applied by Sentinel Engine. Files changed: - command.go (In ExecuteC, explicitly propagate the context from the root command to the subcommand to be executed. This ensures that on subsequent runs with a new context, the subcommand receives the new context instead of a stale one from a previous execution.)
|
Sentinel seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a command with subcommands was executed multiple times via
ExecuteContext, the subcommand would incorrectly receive a stale context from a previous run. This fix ensures the latest context is always propagated from the parent to the child command during execution.Closes #2193
Problem
The
ExecuteCfunction finds and executes the appropriate subcommand based on the provided arguments. However, it failed to propagate the context from the parent command (c.ctx) to the resolved subcommand (cmd.ctx). Consequently, if a root command was invoked multiple times with different contexts usingExecuteContext, the subcommand would retain and use the context from the initial execution, ignoring the newer contexts from subsequent calls.Solution
The solution involves a targeted change within the
ExecuteCfunction incommand.go. After the correct subcommandcmdis found viac.Find, the parent command's context is explicitly assigned to the subcommand with the linecmd.ctx = c.ctx. This ensures that before any flag parsing or execution of the subcommand occurs, it is updated with the fresh context provided to the parent, resolving the stale context issue.Changes
command.goTesting
All verifications passed on branch
sentinel/issue-2193/track-alpha-c1-1781081207:go vet ./...— no issuesgo test -short ./...— all tests passChecklist
go vetpassesgo test -short ./...passes