Skip to content

refactor(detector): add context.Context to Manager.ListInstalled and Current#101

Merged
dipto0321 merged 1 commit into
mainfrom
refactor/detector/add-ctx-to-interface
Jul 3, 2026
Merged

refactor(detector): add context.Context to Manager.ListInstalled and Current#101
dipto0321 merged 1 commit into
mainfrom
refactor/detector/add-ctx-to-interface

Conversation

@dipto0321

Copy link
Copy Markdown
Owner

Summary

Mechanical refactor that adds context.Context as the first
parameter to the Manager interface's two read methods:
ListInstalled and Current. The other interface methods
(Install, Uninstall, Use, SetDefault, GlobalNpmPrefix,
Version) are intentionally out of scope.

Why

The Manager interface was first laid out without ctx on any
method. The omission became user-visible when the post-upgrade
cleanup feature (PR #56 / issue #41) added Current() and shipped
across all 8 manager implementations: a Ctrl-C mid-nodeup upgrade
would still wait for the underlying manager list / fnm list /
nvm current subprocess to finish before returning, because
cancellation had no path into the read paths. Install /
Uninstall already had a cancellation story (the platform lock +
interrupted-upgrade sentinel from #46), so they aren't part of
this PR.

What

  • internal/detector/detector.go — interface signatures:
    • ListInstalled(ctx context.Context) ([]semver.Version, error)
    • Current(ctx context.Context) (semver.Version, error)
    • Both methods got a short godoc note pointing at cmd.Context()
      as the source of ctx.
  • 8 concrete implementations in internal/detector/*.go:
    fnm.go, nvm.go, volta.go, asdf.go, mise.go, n.go,
    nodenv.go, nvm_windows.go. Each method signature updated;
    each runShell(context.Background(), ...) /
    runScript(context.Background(), ...) inside the bodies
    replaced with the new ctx parameter. nvm_windows.go's
    Current is the lone non-shell body (returns the
    ErrNVMWindowsNotImplemented sentinel) — it just gets a
    discarded _ context.Context parameter.
  • 6 production call-sites in internal/cli/{check,list, upgrade,packages}.go now pass cmd.Context(). packages.go's
    helper getCurrentVersion(m) becomes
    getCurrentVersion(ctx, m); its sole caller at line 50 passes
    cmd.Context().
  • 3 mock/stub Manager types in *_test.go:
    fakeManager (system_node_test.go), listTestStubManager
    (cli/list_test.go), stubManager (cli/cleanup_test.go). Each
    takes a discarded _ context.Context on ListInstalled and
    Current; bodies unchanged.
  • 39 test call-sites in the per-manager *_test.go files now
    pass t.Context() (Go 1.24 added t.Context(); the project's
    go.mod is on go 1.24).

Linter verification

contextcheck is enabled in .golangci.yml:48 and (per the
exploration) the _test\.go exclusion only covers errcheck /
gocritic, not contextcheck. So production call-sites with a
cobra cmd.Context() in scope must use it — not
context.Background(). All 6 production call-sites do so. make lint is green.

Linked issues

Closes #53

Type of change

  • refactor — no behavior change
  • feat — new feature (MINOR bump)
  • fix — bug fix (PATCH bump)
  • perf — performance improvement
  • test — tests only
  • docs — documentation only
  • chore — maintenance / dependency bump
  • ci — CI/CD only
  • build — build system only
  • Breaking change (MAJOR bump) — explain below

Checklist

  • Title follows Conventional Commits (refactor(scope): subject)
  • I ran make ci locally and it passes (go vet,
    golangci-lint run (including contextcheck), go test -race -coverprofile=coverage.out ./... all green; coverage
    59.6% unchanged)
  • I added or updated tests for the change (no new tests; all
    39 existing test call-sites updated to the new signature, no
    behavior change)
  • I updated relevant docs (no user-facing docs change;
    CHANGELOG.md gets a ### Changed entry under
    [Unreleased])
  • No new linter warnings (make lint green)
  • If breaking: I documented the migration path in the PR
    body and updated CHANGELOG.md

Files touched (24)

Interface + 8 managers (10 files in internal/detector/):
detector.go, fnm.go, nvm.go, volta.go, asdf.go,
mise.go, n.go, nodenv.go, nvm_windows.go, and the
production test stub system_node_test.go.

Production call-sites (4 files in internal/cli/):
check.go, list.go, upgrade.go, packages.go.

Test stubs + call-sites (8 files in *_test.go):
cli/list_test.go, cli/cleanup_test.go, and the 6 per-manager
test files in internal/detector/: asdf_test.go, fnm_test.go,
mise_test.go, n_test.go, nodenv_test.go, nvm_test.go,
nvm_windows_test.go, volta_test.go (one of those is the 8th —
the stubManager in cli/cleanup_test.go is the 9th).

Changelog: CHANGELOG.md.

…Current

Mechanical refactor that adds context.Context as the first parameter to
two Manager interface methods. The other interface methods (Install,
Uninstall, Use, SetDefault, GlobalNpmPrefix, Version) are intentionally
out of scope per the issue title.

The omission became user-visible when PR #56 / issue #41 added Current()
across all 8 manager implementations: a Ctrl-C mid-`nodeup upgrade`
would still wait for the underlying `manager list` / `fnm list` /
`nvm current` subprocess to finish before returning, because
cancellation had no path into the read paths.

Interface + 8 implementations (fnm, nvm, volta, asdf, mise, n, nodenv,
nvm-windows) plumb ctx into existing runShell/runScript calls (both
already ctx-aware via internal/platform/shell.go). 6 production
call-sites in internal/cli/{check,list,upgrade,packages}.go pass
cmd.Context(); getCurrentVersion(m) becomes getCurrentVersion(ctx, m).
3 mock/stub Manager types (fakeManager, listTestStubManager,
stubManager) take a discarded _ context.Context. 39 test call-sites
in the per-manager *_test.go files now pass t.Context() (Go 1.24).

contextcheck linter is satisfied: production code uses cmd.Context(),
tests use t.Context() (no context.Background() anywhere a ctx is in
scope).

Closes #53
@cocogitto-bot

cocogitto-bot Bot commented Jul 3, 2026

Copy link
Copy Markdown

✔️ 987aac6 - Conventional commits check succeeded.

@dipto0321 dipto0321 merged commit aa44b29 into main Jul 3, 2026
10 checks passed
@dipto0321 dipto0321 deleted the refactor/detector/add-ctx-to-interface branch July 3, 2026 07:29
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.

refactor(detector): Manager interface lacks context.Context parameter on Version/ListInstalled

1 participant