Repository navigation
feat: add install --use to switch to the version just installed - #740
Conversation
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
linghengqian
left a comment
There was a problem hiding this comment.
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:
ltsresolves 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 sayltsand "9.0.0" sorts after "10.0.0" (1.9.0 vs 1.10.0 does the same).- The version
ltsresolves to is already installed, but it was installed by number (install [email protected], theninstall -u beta@lts). No receipt sayslts, so it falls back toltsand 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:
Installstill prints "Please usevfox use Xto use it." (and copies it to the clipboard on a TTY) right before "Now using X.". Probably skip it when-uis 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".
switchFailedis 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 nodejsends up in the interactive picker (RunSearch) and-uis dropped. I haven't tried that one, going by the code.
|
Took the shape you proposed, in On the smaller points:
|
linghengqian
left a comment
There was a problem hiding this comment.
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?
misealso 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".
e4b35fd to
f0bf2ca
Compare
|
Test added in And with the early return mutated from Under that mutation the suite shows only this failure ( |
linghengqian
left a comment
There was a problem hiding this comment.
Thanks for adding the test. Two small things left, both in comments, so no behavior changes.
-
The comment above
rewritingPluginininstall_cli_test.gostill says the switch has to "follow the receipt to the version that is really on disk".--useno longer goes through the receipt; it switches to the versionInstallResolvedreturns. 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". -
InstallResolvedhas no doc comment.InstallandReinstallboth have one, but the implementation now lives inInstallResolved, 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.
|
Both taken, comments only, in
|
vfox installfollowed byvfox useis two commands for one intention, and #717 asks for exactly that pair to become one. This is theinstall -uhalf:-u/--useruns the same session-scope switchvfox usewould, after the install succeeds. It is refused with--all, the way--reinstallalready is, and it needs an activated shell for the same reasonusedoes — in a container,vfox use -gafter the install is still what puts the version onPATHfor the next step.Two details worth pointing at in review:
PreInstallhook may install something else (ltsbecomes2.0.0) and the receipt is the only record of that, soinstalledVersion()follows it;installed, but switching failed: …, and the command exits withfailed to switch <sdk>) instead of blaming the install that just succeeded.install_cli_test.gocovers the flag, its short form, the absence of it (the session is left alone), thePreInstallrewrite, a shell with no hook, and the--allrefusal.go test ./cmd/... ./internal/sdk/...passes.The
use -ihalf of #717 is untouched; say the word and I will add it as a mirror.Refs #717.