Skip to content

fix(cobra): propagate context to subcommands in ExecuteC#2416

Open
techwizard31 wants to merge 1 commit into
spf13:mainfrom
techwizard31:sentinel/issue-2193/track-alpha-c1-1781081207
Open

fix(cobra): propagate context to subcommands in ExecuteC#2416
techwizard31 wants to merge 1 commit into
spf13:mainfrom
techwizard31:sentinel/issue-2193/track-alpha-c1-1781081207

Conversation

@techwizard31

Copy link
Copy Markdown

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 ExecuteC function 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 using ExecuteContext, 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 ExecuteC function in command.go. After the correct subcommand cmd is found via c.Find, the parent command's context is explicitly assigned to the subcommand with the line cmd.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

File Lines Description
command.go 1084–1170 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.

Testing

All verification checkpoints passed. build ./... ✓  vet ./... ✓  test ['.'] ✓

All verifications passed on branch sentinel/issue-2193/track-alpha-c1-1781081207:

  • go vet ./... — no issues
  • go test -short ./... — all tests pass

Checklist

  • Code follows project conventions
  • go vet passes
  • go test -short ./... passes
  • No unrelated changes included
  • Changelog updated (if applicable)

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.)
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Incorrect copy of command context from parent to child (trivial fix)

2 participants