From 564fc9031eae84a4cbfb44c8d8381a1f7eb8b74c Mon Sep 17 00:00:00 2001 From: dipto0321 Date: Fri, 3 Jul 2026 08:51:33 +0600 Subject: [PATCH] fix(cli): thread cmd.Context() through packages snapshot/restore `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 --- CHANGELOG.md | 13 +++++++++++++ internal/cli/packages.go | 5 ++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1c6cd8..43c97d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -238,6 +238,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 (omits-on-nil current/error), the table renderer (empty state, mixed empty/error/success), and the `--manager` resolution helper. Closes #45. +- `internal/cli/packages.go`: `runSnapshot` and `runRestore` now + pass `cmd.Context()` to `packages.Snapshot` / `packages.Restore` + (and to the `--from` branch via the same `ctx` local) instead of + `context.Background()`. Both are cobra `RunE` functions with a + live context available — using `context.Background()` meant + Ctrl-C during `nodeup packages snapshot` or `nodeup packages + restore` could not cancel the in-flight `npm ls -g` / `npm + install -g` subprocess, so the child process kept running after + the user aborted. The fix is the same contextcheck violation + `internal/cli/upgrade.go` and `check.go` already addressed as + part of #48 — `packages.go` was the last `RunE` file in the tree + still using `context.Background()`. The unused `"context"` + import is dropped. Closes #49. ## [0.0.0] - 2024-07-01 diff --git a/internal/cli/packages.go b/internal/cli/packages.go index c1cf02c..269406a 100644 --- a/internal/cli/packages.go +++ b/internal/cli/packages.go @@ -1,7 +1,6 @@ package cli import ( - "context" "fmt" "github.com/Masterminds/semver/v3" @@ -52,7 +51,7 @@ func runSnapshot(cmd *cobra.Command, args []string) error { return fmt.Errorf("get current version: %w", err) } - if err := packages.Snapshot(context.Background(), m.Name(), version); err != nil { + if err := packages.Snapshot(cmd.Context(), m.Name(), version); err != nil { return fmt.Errorf("snapshot failed: %w", err) } @@ -138,7 +137,7 @@ the "interrupted upgrade" warning).`, } func runRestore(cmd *cobra.Command, args []string) error { - ctx := context.Background() + ctx := cmd.Context() fromPath, _ := cmd.Flags().GetString("from") // runRestore doubles as the "replay the upgrade-in-progress sentinel"