Skip to content

feat: add install --use to switch to the version just installed - #740

Merged
linghengqian merged 4 commits into
version-fox:mainfrom
paoValle:feat/install-use
Nov 2, 2026
Merged

linghengqian merged 4 commits into
version-fox:mainfrom
paoValle:feat/install-use

Conversation

@paoValle

Copy link
Copy Markdown
Contributor

vfox install followed by vfox use is two commands for one intention, and #717 asks for exactly that pair to become one. This is the install -u half:

vfox install -u nodejs@20

-u/--use runs the same session-scope switch vfox use would, after the install succeeds. It is refused with --all, the way --reinstall already is, and it needs an activated shell for the same reason use does — in a container, vfox use -g after the install is still what puts the version on PATH for the next step.

Two details worth pointing at in review:

  • it switches to the version that is on disk, not the one typed: a plugin's PreInstall hook may install something else (lts becomes 2.0.0) and the receipt is the only record of that, so installedVersion() follows it;
  • a failed switch now says so (installed, but switching failed: …, and the command exits with failed to switch <sdk>) instead of blaming the install that just succeeded.

install_cli_test.go covers the flag, its short form, the absence of it (the session is left alone), the PreInstall rewrite, a shell with no hook, and the --all refusal. go test ./cmd/... ./internal/sdk/... passes.

The use -i half of #717 is untouched; say the word and I will add it as a mirror.

Refs #717.

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.41860% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 51.60%. Comparing base (f4a9bae) to head (bb5320a).

Files with missing lines Patch % Lines
internal/sdk/sdk.go 55.00% 9 Missing ⚠️
cmd/commands/install.go 91.30% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #740      +/-   ##
==========================================
+ Coverage   51.45%   51.60%   +0.15%     
==========================================
  Files         111      111              
  Lines        8256     8276      +20     
==========================================
+ Hits         4248     4271      +23     
+ Misses       3586     3585       -1     
+ Partials      422      420       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@linghengqian linghengqian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up. Several SDKs on one line works the way I'd expect: I ran install -u with five different ones and all five ended up in the session scope, and when one fails to install the others are still installed and switched.

What I'm not happy with is installedVersion(). It looks for a receipt whose Requested equals what was typed and returns the first match from InstalledList(), which is sorted as strings. When a plugin rewrites the request there are two ways to get this wrong:

  1. lts resolves to 9.0.0 one day and 10.0.0 the next. The second run prints "Install [email protected] success!" and then switches to 9.0.0, because both receipts say lts and "9.0.0" sorts after "10.0.0" (1.9.0 vs 1.10.0 does the same).
  2. The version lts resolves to is already installed, but it was installed by number (install [email protected], then install -u beta@lts). No receipt says lts, so it falls back to lts and fails with "installed, but switching failed: beta@lts is not installed".

The first one is worse, since it quietly picks the wrong version. I'd also rather not depend on the receipt here: recordInstall only logs when it can't write one, and versions installed before receipts existed don't have any.

These two tests fail on the branch and show both cases:

// A plugin whose "lts" moves from one run to the next.
const movingPlugin = `PLUGIN = {name="beta", version="0.0.1", description="test", author="test", minRuntimeVersion="0.0.0"}
function PLUGIN:Available(ctx) return {{version="lts"}} end
function PLUGIN:PreInstall(ctx)
  if ctx.version == "lts" then return {version=os.getenv("BETA_LTS")} end
  return {version=ctx.version}
end
function PLUGIN:EnvKeys(ctx) return {{key="PATH", value=ctx.path .. "/bin"}} end
`

func TestInstallCommandUseFollowsAMovingRequest(t *testing.T) {
	f := newCLIFixture(t, false)
	f.writePlugin(t, "beta", movingPlugin)

	t.Setenv("BETA_LTS", "9.0.0")
	f.run(t, "install", "-u", "beta@lts").mustSucceed(t, "first install -u")

	t.Setenv("BETA_LTS", "10.0.0")
	f.run(t, "install", "-u", "beta@lts").mustSucceed(t, "second install -u")
	mustContain(t, "session config", readFile(t, filepath.Join(f.userHome, "tmp", "test-session", ".vfox.toml")), `beta = "10.0.0"`)
}

func TestInstallCommandUseWhenTheResolvedVersionIsAlreadyInstalled(t *testing.T) {
	f := newCLIFixture(t, false)
	f.writePlugin(t, "beta", movingPlugin)
	t.Setenv("BETA_LTS", "2.0.0")

	f.run(t, "install", "[email protected]").mustSucceed(t, "install [email protected]")
	f.run(t, "install", "-u", "beta@lts").mustSucceed(t, "install -u beta@lts")
	mustContain(t, "session config", readFile(t, filepath.Join(f.userHome, "tmp", "test-session", ".vfox.toml")), `beta = "2.0.0"`)
}

I think the fix is to stop looking the version up afterwards. install() already knows the answer on every path that succeeds: the version as typed when it's already installed, sdkVersion once PreInstall has resolved it. So it can return it. Something like InstallResolved(version, reinstall) (Version, error) on Sdk, with Install and Reinstall as thin wrappers, then hand the result to UseWithConfig and delete installedVersion. impl is the only implementer of Sdk, so the change stays small. I tried it and the two tests above pass along with your existing ones. If you'd rather keep this PR smaller, passing resolvedVersion straight through is safe as well: a rewritten request then fails with "lts is not installed" instead of picking the wrong version, though the existing PreInstall rewrite test would have to go.

I'd want this sorted before it merges. A few smaller things, none of them blocking:

  • Install still prints "Please use vfox use X to use it." (and copies it to the clipboard on a TTY) right before "Now using X.". Probably skip it when -u is set.
  • With several SDKs and no hook, each line says "installed, but switching failed" but the last line is "failed to install some SDKs: a, b". switchFailed is also one bool for the whole run, so a failed switch of [email protected] followed by a failed install of [email protected] ends up as "failed to switch gradle".
  • vfox install -u [email protected] [email protected] is accepted. Both get installed, the session is switched twice and ends on 9.8.2. That's fine with me, but the first "Now using" is noise. Either switch once per SDK for its last argument, or refuse it like --all, and mention whichever you pick in the docs.
  • With no version and nothing in the project config, vfox install -u nodejs ends up in the interactive picker (RunSearch) and -u is dropped. I haven't tried that one, going by the code.

@paoValle

Copy link
Copy Markdown
Contributor Author

Took the shape you proposed, in e4b35fd: Sdk.InstallResolved(version, reinstall) (Version, error), Install and Reinstall as wrappers, installedVersion() deleted, and --use switches to what install returns. Both of your cases are in install_cli_test.go and both fail on the previous head, so the bug is pinned rather than described.

On the smaller points:

  • Two versions of one SDK: refused before anything is installed, --use cannot switch two versions of beta, give one. That is the "refuse it like --all" branch; both docs files say so.
  • -u with no version: it now stops instead of ending in the picker and dropping the flag — install --use needs a version for nodejs, for example nodejs@latest.
  • The Please use vfox use X hint and the per-SDK summary are unchanged. The hint is printed by the SDK, and install -a, exec and the picker all go through Install, so suppressing it for -u means threading the flag through the SDK API; happy to do it in a follow-up if you would rather have it here. The summary is still one switchFailed for the run, which is the mislabel you describe.

go test ./cmd/... ./internal/sdk/... passes (3 packages ok); the nine TestInstallCommand* cases pass individually.

@linghengqian linghengqian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick turnaround. InstallResolved is what I had in mind, and both cases from my review are pinned by tests now. I checked the branch against main at f4a9bae: it merges cleanly, the tests pass, and the two new regression tests fail on d252d75 and pass here.

One thing left: a test for -u on a version that is already installed. That is what a second run of a setup script does, and it goes through the early return version, nil in InstallResolved, whose result no test looks at. If I change it to return "", nil, the whole suite stays green, while vfox install -u [email protected] fails with "installed, but switching failed: alpha@ is not installed".

This passes on your branch and fails with that change:

func TestInstallCommandUseOnAnInstalledVersion(t *testing.T) {
	f := newCLIFixture(t, false)

	f.run(t, "install", "[email protected]").mustSucceed(t, "install [email protected]")
	out := f.run(t, "install", "-u", "[email protected]").mustSucceed(t, "install -u [email protected]").output()
	mustContain(t, "install -u", out, "already installed", "Now using")
	mustContain(t, "session config", readFile(t, filepath.Join(f.userHome, "tmp", "test-session", ".vfox.toml")), `alpha = "1.0.0"`)
}

Add it to install_cli_test.go and I'm happy to merge. Nothing else from me: the "Please use vfox use X" hint and the summary line are fine as follow-ups.

  • CC @aooohan @bytemain @Chance-fyi . Do you think there are any other issues with the design of this parameter? mise also features a similar design.

`vfox install` followed by `vfox use` is two commands for one intention, and
version-fox#717 asks for either `install -u <sdk>` or `use -i <sdk>`. This is the first:
`-u/--use` runs the same session-scope switch `vfox use` does, once the version
is installed, and is refused with `--all` the way `--reinstall` already is.

It switches to the version that ended up on disk, not to the one typed: a
plugin's PreInstall hook may install something else (`lts` becomes `2.0.0`), and
its receipt is the only record of that. A failed switch says so instead of
blaming the install, and the command's own failure now reads "failed to switch".

The new `install_cli_test.go` covers the flag, the absence of it (the session is
left alone), the PreInstall rewrite, a shell without a hook, and the refusal.
`go test ./cmd/... ./internal/sdk/...` passes.
`installedVersion()` guessed which version a request ended up as by scanning
the receipts for one that quotes the same request. That is wrong twice: a
plugin that rewrites `lts` to today's release leaves several receipts that
say `lts`, and `InstalledList()` is sorted as strings, so `9.0.0` wins over
`10.0.0`; and a version installed by number has no receipt saying `lts` at
all.

`install` already knows the answer on every path that succeeds, so it
returns it: `Sdk.InstallResolved(version, reinstall) (Version, error)`, with
`Install` and `Reinstall` as wrappers for the callers that do not need it.
`--use` switches to what it returns and the lookup goes away.

`--use` also refuses two versions of the same SDK and a missing version
rather than switching twice or dropping the flag in the picker, and the
docs say so.
A second run of a setup script asks for a version that is already on disk, so
it takes the early return in InstallResolved. Nothing looked at the version
that path hands back: changing it to an empty version left the whole suite
green, while `vfox install -u [email protected]` then failed with "installed, but
switching failed: alpha@ is not installed".
@paoValle

Copy link
Copy Markdown
Contributor Author

Test added in f0bf2ca, rebased on main (f4a9bae) — yours verbatim, one file, 11 lines; the "Please use vfox use X" hint and the summary line are untouched as you said.

$ go test ./cmd/commands/ -run TestInstallCommandUseOnAnInstalledVersion -v
--- PASS: TestInstallCommandUseOnAnInstalledVersion (0.01s)
ok  github.com/version-fox/vfox/cmd/commands  0.428s

And with the early return mutated from return version, nil to return "", nil:

--- FAIL: TestInstallCommandUseOnAnInstalledVersion (0.01s)
    install_cli_test.go:121: install -u [email protected] failed with 1
        stdout: [email protected] is already installed
        installed, but switching failed: alpha@ is not installed
FAIL  github.com/version-fox/vfox/cmd/commands  0.423s

Under that mutation the suite shows only this failure (cmd and internal/sdk stay green), so the claim you made is now pinned exactly.

@linghengqian linghengqian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test. Two small things left, both in comments, so no behavior changes.

  1. The comment above rewritingPlugin in install_cli_test.go still says the switch has to "follow the receipt to the version that is really on disk". --use no longer goes through the receipt; it switches to the version InstallResolved returns. Something like:

    // A plugin may install something other than what was asked for: the switch has to
    // follow the version InstallResolved hands back, the one that is really on disk.

    The comment above movingPlugin ("not an older receipt that says the same thing") describes the old lookup as well, so it could be reworded in the same pass, for example "not the one from an earlier run".

  2. InstallResolved has no doc comment. Install and Reinstall both have one, but the implementation now lives in InstallResolved, and the interface only has a short trailing comment. What it returns is the whole point of the method, and the signature doesn't say that the version can differ from the one passed in. A short comment would do, for example:

    // InstallResolved installs version, or with reinstall replaces it, and returns the
    // version that is on disk afterwards. That is the version PreInstall resolved the
    // request to, which can differ from the one asked for (`lts` becoming `2.0.0`); if
    // the version as given is already installed it is returned as is and PreInstall is
    // not called. The version is only meaningful when err is nil. Install and Reinstall
    // wrap this for callers that do not need it.

@paoValle

paoValle commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Both taken, comments only, in bb5320a:

  • rewritingPlugin's comment now says the switch follows the version InstallResolved hands back, and movingPlugin's says "not the one from an earlier run".
  • InstallResolved has its doc comment, worded as you wrote it: it returns what is on disk afterwards, that can differ from the request, and the version is only meaningful when err is nil.
$ go build ./... && go test ./cmd/... ./internal/sdk/...
ok  	github.com/version-fox/vfox/cmd	1.439s
ok  	github.com/version-fox/vfox/cmd/commands	2.635s
ok  	github.com/version-fox/vfox/internal/sdk	0.645s

gofmt -l on the two files is empty.

@linghengqian linghengqian added enhancement New feature or request and removed need-more-info labels Nov 2, 2026
@linghengqian linghengqian added this to the 1.0.14 milestone Nov 2, 2026

@linghengqian linghengqian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@linghengqian
linghengqian merged commit 4766d24 into version-fox:main Nov 2, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants