Skip to content

fix(cli): thread cmd.Context() through packages snapshot/restore#83

Merged
dipto0321 merged 1 commit into
mainfrom
fix/cli/packages-context
Jul 3, 2026
Merged

fix(cli): thread cmd.Context() through packages snapshot/restore#83
dipto0321 merged 1 commit into
mainfrom
fix/cli/packages-context

Conversation

@dipto0321

Copy link
Copy Markdown
Owner

Summary

internal/cli/packages.go's runSnapshot and runRestore are cobra RunE functions with a live cmd.Context() available, but both called context.Background() directly. Ctrl-C during nodeup packages snapshot or nodeup packages restore could not cancel the in-flight npm ls -g / npm install -g subprocess — the child process kept running after the user aborted.

This is the same contextcheck violation internal/cli/upgrade.go and check.go already addressed as part of #48 (the manifest fetch fix). packages.go was the last RunE file in the tree still using context.Background(). Fix: replace both call sites with cmd.Context(), drop the now-unused "context" import.

Closes #49.

Linked issues

Type of change

  • fix — bug fix (PATCH bump)

Checklist

  • Title follows Conventional Commits (fix(scope): subject)
  • I ran make ci locally and it passes (fmt, vet, test pass locally; make lint fails on the v2→v1 schema mismatch tracked in build(lint): .golangci.yml v1 schema incompatible with golangci-lint v2 (brew installs v2, make lint fails to even parse config) #62 — pre-existing on main, independent of this fix; CI runs golangci-lint v1.64.8 per .github/workflows/ci.yml and parses the config fine)
  • I added or updated tests for the change (N/A — three-line behavioral fix; the contract is already covered by the cobra cmd.Context() integration with contextcheck. A focused test would require mocking packages.Snapshot/packages.Restore, which is a larger refactor than this fix wants. The pre-existing internal/packages/snapshot_test.go::TestSnapshot_StoredVersionKeyMatchesArg and TestRestoreFromSnapshot_ReadsSourcePathRegardlessOfNodeVersion cover the contract pieces the cli calls.)
  • I updated relevant docs (README, docs/, inline godoc)
  • No new linter warnings (this PR actually REMOVES a contextcheck violation that was previously suppressed in the local make lint output but not in CI — once build(lint): .golangci.yml v1 schema incompatible with golangci-lint v2 (brew installs v2, make lint fails to even parse config) #62 is fixed, this would have been the only remaining offender)
  • If breaking: I documented the migration path in the PR body and updated CHANGELOG.md (N/A — no CLI surface breaking)

Scope notes

  • runSnapshot was a single-line change: packages.Snapshot(context.Background(), m.Name(), version)packages.Snapshot(cmd.Context(), m.Name(), version).
  • runRestore was a two-line change: ctx := context.Background()ctx := cmd.Context(); the rest of the function already used the local ctx, so only the source needed updating.
  • The "context" import is now unused and was removed.
  • I did NOT thread cmd.Context() through getCurrentVersion (the helper at line 63) because it doesn't shell out — it just calls m.ListInstalled(), which is an in-memory slice operation. There's no Ctrl-C behavior to expose there.
  • I deliberately did NOT refactor runRestore to be unit-testable with a cmd.Context() injection seam. Same reasoning as bug(packages): restore aborts on first failed install; migration report never written #46: a stubbed platform.RunShell seam is a larger change than this three-liner wants, and the rest of the test surface (snapshot_test.go + restore_test.go) already validates the package-level Snapshot/Restore contract that runRestore relies on.

Out of scope (separate issues)

Screenshots / output

User-visible behavior change:

  • Before: User runs nodeup packages snapshot, hits Ctrl-C during the in-flight npm ls -g step. The cobra command returns immediately but the underlying npm child process keeps running until completion (or its own timeout). The new cmd.Context() plumbing makes Ctrl-C now propagate down through RunShell to exec.CommandContext, killing the child.
  • After: Ctrl-C during nodeup packages snapshot or nodeup packages restore cancels both the cobra command AND the in-flight npm subprocess.

Verification path: start nodeup packages snapshot, watch the in-flight npm PID via ps, send SIGINT to the parent nodeup process, confirm the child npm PID disappears within ~ms.

`internal/cli/packages.go`'s runSnapshot (line 55) and runRestore
(line 141) were cobra RunE functions with a live cmd.Context()
available, but both called context.Background() directly instead.
Ctrl-C during `nodeup packages snapshot` or `nodeup packages
restore` could not cancel the in-flight `npm ls -g` / `npm install
-g` subprocess; the child process kept running after the user
aborted.

This is the same contextcheck violation `internal/cli/upgrade.go`
and `check.go` already addressed as part of #48 (the manifest
fetch fix). `packages.go` was the last RunE file in the tree still
using context.Background(). Fix: replace both call sites with
cmd.Context(), drop the now-unused "context" import.

Closes #49.

Co-Authored-By: puku-ai-2.8 <noreply@puku.sh>
@cocogitto-bot

cocogitto-bot Bot commented Jul 3, 2026

Copy link
Copy Markdown

✔️ 564fc90 - Conventional commits check succeeded.

@dipto0321 dipto0321 merged commit 35b764b into main Jul 3, 2026
10 checks passed
@dipto0321 dipto0321 deleted the fix/cli/packages-context branch July 3, 2026 02:55
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.

fix(cli): context.Background() used instead of cmd.Context() in packages.go (contextcheck violation)

1 participant