Repository navigation
fix(uninstall): switch the global version without the shell hook - #811
linghengqian merged 14 commits into
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #811 +/- ##
==========================================
+ Coverage 61.08% 61.98% +0.89%
==========================================
Files 125 125
Lines 9429 9810 +381
==========================================
+ Hits 5760 6081 +321
- Misses 3164 3207 +43
- Partials 505 522 +17 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
linghengqian
left a comment
There was a problem hiding this comment.
Request changes.
The fix itself is correct, and uninstall no longer needs the hook. Three things need to change before this merges.
-
Two comments need fixing. The comment above
b.reportFindings(version, hookEnv)inuseInHook(internal/sdk/sdk.go:938-939) says the hand-off to a new shell comes "below". That hand-off moved toUseWithConfigin this PR, so the comment points at nothing now. The doc comment onUseGlobal(sdk.go:821-824) ends with a clause about the caller that does not say what the function does. Please reword it to say whatUseGlobaldoes and how it differs fromUse. -
The test comment claims more than the test checks. The comment in
TestUninstallSwitchesTheGlobalVersionWithoutAHooksays uninstall must not open a shell, but no assertion covers that. Either add a seam for the shell opener, a variable the test can replace and count, and assert it is not called on this path, or drop that clause from the comment. -
UseGlobalhas no direct tests. The CLI test covers the success path only. Nothing reaches the PreUse error return (sdk.go:828) or the not-installed return (sdk.go:831). Please add both tointernal/sdk, following the setup ofTestUseWarnsWhenTheVersionIsShadowed:- a version that is not installed: expect
*VersionNotExistsErrorand an unchanged global config; - a PreUse hook that fails (a Lua plugin defining
PLUGIN:PreUse, asinstall_as_test.godoes): expect the error and an unchanged global config.
- a version that is not installed: expect
Ran on 97a58a1 (Linux only):
- The new test fails on the PR base, 90a96e1, with exit 1 and "vfox requires hook support". It passes on this head.
go test ./...passes,go vetis clean, and the three changed files are gofmt-clean.- The description matches what I found. The Windows change (no new shell after an uninstall) is intended.
linghengqian
left a comment
There was a problem hiding this comment.
Thanks for the follow-up. The three points from my earlier review are addressed in 7dc0aa0, and CI is green. A few things still need to change before I can approve.
-
Some changed lines in UseWithConfig are not run by any test: the
return errafter useInHook (sdk.go:811-812). A case with the hook set and a plugin whose EnvKeys fails would cover it. check_test.go already has an "EnvKeys exploded" plugin you can copy. The test should also check that the global config is unchanged. -
The non-hook branch (sdk.go:815-817) only runs on Windows, and nothing checks that UseGlobal never opens a shell. That is the Windows behavior this PR changes, so it should be tested. shell.Open is a plain function, so a test cannot observe it. A package-level opener variable would fix that: a platform-independent test should assert that UseGlobal opens no shell, and a Windows-only test should assert that
use -gwithout a hook still opens one. -
The new tests in internal/sdk call UseGlobal with the global scope. On Windows, that calls env.ApplyEnvsToRegistry, which prepends the test's temp bin directory to HKCU\Environment\Path and never removes it. Please keep the registry write out of these tests. The existing "the global version" subtest in uninstall_cli_test.go takes the same path and should get the same guard in this PR.
-
Please also address this, even though it is a suggestion: UseGlobal repeats the preUse and installed-version check from UseWithConfig. A small shared helper would keep the two from drifting apart.
Checked on 7dc0aa0 on Linux: go vet and go test ./... pass. The new CLI test fails on 90a96e1 with exit 1 and the hook message, and passes on this head.
|
Thanks. All four points are in 85d75e3. New tests cover the EnvKeys error in UseWithConfig (global config kept), UseGlobal opening no shell with or without the hook, and Windows |
linghengqian
left a comment
There was a problem hiding this comment.
Request changes.
The no-hook path works. Uninstalling the global version on Linux now exits 0 and moves the global version, UseGlobal never opens a shell, and the PreUse and not-installed errors for UseGlobal, plus the EnvKeys error for UseWithConfig, have tests. On Linux, go test ./... passes, go vet is clean for linux, windows and darwin, and gofmt is clean. The new CLI test fails on the base with exit 1 and the hook message, as the description says.
I found three problems. All three block the merge.
- The fallback version is chosen by string order.
InstalledList sorts with versions[i] > versions[j] on strings, and fallbackVersion takes the first version that is not a copy. So 9.0.0 sorts above 10.0.0, and 8 sorts above 17. Reproduced:
- installed 9.0.0, 10.0.0, 11.0.0, global 11.0.0: vfox uninstall [email protected] switches to 9.0.0
- installed 8, 11, 17, 21, global 21: the switch goes to 8
This happens with and without the hook. It also happens on the base with the hook, so it predates this PR, but the PR makes the same wrong choice in shells without the hook. vfox list uses the same order. Sorting with util.CompareVersion in InstalledList fixes both cases, and the full test suite still passes with that change. Please add a test with 9.0.0, 10.0.0 and 11.0.0 that checks the list order and the global version after the uninstall.
- A failed uninstall or switch can leave nothing in use.
Uninstall unsets the global version (Unuse(env.Global)) before it removes the directory, and uninstallCmd removes the version before it switches to the fallback. So a failure at either step returns an error with no version in use. Reproduced:
- The directory cannot be removed (I made the install directory read-only): Uninstall returns permission denied, and the global version is empty afterwards.
- The fallback's EnvKeys hook returns an error: UseGlobal returns it, the global version is empty, and only 1.0.0 is still installed.
The base has the same order, so this predates the PR. Issue #809 describes exactly this state ("nothing is left in use"), and on Windows a locked file can make the removal fail. I don't think #809 is fixed while this can still happen.
Please change the order so that a failure never leaves the global version unset while another version is installed. How you do it is up to you: switch first and then remove, or restore the global setting when a later step fails. Add a test for each failure and assert that, afterwards, an installed version is still the global one.
- Two of the new comments do not match the code.
- openShell says it "starts a new shell in place of the one with the given pid". shell.Open runs the shell as a child process with command.Run() and waits for it. Nothing is replaced.
- The UseGlobal comment says it "writes it to the global config and links". On Windows it also updates the registry environment, and it removes the entries of the previous global version first. The comment should say so.
|
Thanks, all three fixed in cc6f52c.
All new tests fail on the previous commit. |
linghengqian
left a comment
There was a problem hiding this comment.
Request changes.
The three points from my last review are fixed. On cc6f52c, go build, go vet (linux, windows, darwin), go test ./... and gofmt all pass. I still found four bugs, and all four block the merge. I reproduced each one with a throwaway test, comparing cc6f52c with the base 90a96e1.
- Versions with a leading "v" sort in the wrong place (regression).
InstalledList now sorts with util.CompareVersion. That function parses each dotted part with strconv.Atoi and treats a failure as 0 (internal/shared/util/version.go:56,61). So v20.1.0 compares as 0.1.0 and sorts below 18.0.0.
With v20.1.0, v22.0.0 (global) and 18.0.0 installed:
- vfox list alpha shows 18.0.0, v20.1.0, v22.0.0 on this branch. The base shows v22.0.0, v20.1.0, 18.0.0.
- vfox uninstall [email protected] switches the global version to 18.0.0 on this branch. The base switches to v20.1.0.
The same order reaches plugins through GetInstalledVersions (internal/sdk/sdk.go:1041) and the interactive picker (cmd/commands/use.go:156). AGENTS.md gives v20.1.0 as the conventional form, so this is not a corner case. A name whose first part is not a number, such as dev or latest, is treated as 0 in the same way.
Suggested fix: sort with a key that ignores a leading "v" for ordering only, so the name stays as typed, and puts names with no numeric start below the numeric ones. Keep the change inside InstalledList, because CompareVersion is also used for plugin minimum-version checks.
- A failed uninstall still moves the global version (regression).
uninstallCmd switches the global version (cmd/commands/uninstall.go:74-81) before Uninstall checks that the version is installed (internal/sdk/sdk.go:581-584) and before it runs PreUninstall (internal/sdk/sdk.go:599-616). If either check fails, the command exits 1 and removes nothing, but the global version has already changed.
- Global alpha = "2.0.0", only 1.0.0 installed. vfox uninstall [email protected] fails with "[email protected] is not installed", and the config now says 1.0.0. The base leaves 2.0.0.
- A plugin whose PreUninstall raises an error gives the same result: the config moves to the other version and the command fails. The base leaves the config alone.
The switch should happen only after the checks that can fail without side effects. I think it belongs inside Uninstall, after the PreUninstall hook and before RemoveAll, with the fallback passed in. That keeps the order in one place. The removal-failure case can keep the switch, as the current tests expect.
- The fallback is never retried, so one broken version blocks a valid uninstall.
fallbackVersion picks one candidate, and an error from UseGlobal aborts the command. Take gamma with 1.0.0 (healthy), 2.0.0 (its EnvKeys raises) and 3.0.0 (global). vfox uninstall [email protected] prints "Auto switch to the other version.", fails with the EnvKeys error for 2.0.0, and leaves 3.0.0 installed and global. 1.0.0 is never tried, though nothing is wrong with it. The "Auto switch" line is also printed before the switch happens, so it is wrong whenever the switch fails.
Try the remaining candidates in order and take the first one that activates. If none does, stop before removing anything and name the candidate that failed. Print "Auto switch" only after the switch succeeds.
- Pre-release versions sort above their release.
CompareVersion("2.0.0-rc1", "2.0.0") returns 0, because "0-rc1" parses as 0. The text tie-break then puts 2.0.0-rc1 first. With 1.0.0, 2.0.0, 2.0.0-rc1 and 3.0.0 (global) installed, vfox uninstall [email protected] switches to 2.0.0-rc1. The base does the same, but this PR now depends on that order for the switch target, and its commit title says it picks the newest version left. A suffix after the numeric part should sort below the bare version. The key from item 1 can handle both cases.
Tests
None of the new uninstall tests cover a leading "v", a pre-release, a failing PreUninstall, or a global version that is not installed. Please add a regression test for each item above, and confirm each one fails on the current head.
The description also does not mention that the order of every command that lists versions has changed: list, the use picker, and the legacy-file lookup. Please say so there.
Nit, not a bug: the doc comment on otherVersions ("is versions without version") reads badly.
|
Thanks, all four fixed in 0336a84, with a test for each (all six fail on cc6f52c).
The description now lists the order change for |
linghengqian
left a comment
There was a problem hiding this comment.
Request changes.
I reviewed 0336a84 against 90a96e1 (upstream main). Locally, build, go vet for linux, windows and darwin, gofmt and go test ./... all pass, and CI is green. The four points from the last round are addressed.
I still found three bugs and one test problem. All of them block the merge.
- Dangling links for additions the fallback does not have (regression)
Before this change, Uninstall unset the global version, and Unuse removed the global links of the removed version, additions included. Now Uninstall calls switchGlobal instead (sdk.go:627-630), and that only creates the links of the fallback. Links for additions that only the removed version had stay in the global link directory, and then os.RemoveAll (sdk.go:657) deletes the directory they point to.
Repro: [email protected] with an addition extra-1.0.0, [email protected] without it, 1.0.0 global, then vfox uninstall [email protected]. On main, /sdks/extra is removed. On this head it stays, and stat on it fails.
Fix: after the switch, remove the global links that point into the directory being removed. Do not remove them by name, because the main link now points at the fallback.
- A failed switch leaves the config and the links disagreeing
useInHook writes the config (sdk.go:955) before it creates the links (sdk.go:964). If the link step fails, switchGlobal tries the next candidate or returns an error, and the config has already changed.
Repro: make /sdks read-only, install alpha 1.0.0 and 2.0.0, make 2.0.0 global, run vfox uninstall [email protected]. The command fails with "nothing was removed", which is true for the removal. But the config now says alpha = "1.0.0", while sdks/alpha still points at 2.0.0 and 2.0.0 is still installed. The doc comment on switchGlobal (sdk.go:668) says it changes nothing, and that is not true.
On Windows the same failure comes after useInHook has already removed the old version's registry entries (sdk.go:933-936), so the registry ends up with neither version. I have not run this on Windows; this part is from reading the code.
Fix: when the link step fails, put the previous global version back, or create the links before the config is saved. Add a test where the link step fails after the save. The existing EnvKeys test only covers a failure before the save.
- The restore path reports a success that did not happen
When the only version cannot be removed, the output is:
Unset alpha successfully.
Now using [email protected].
unlinkat .../alpha-2.0.0: permission denied
The config is restored, which is good. But the output says the version was unset and then made active again. The restore goes through useInHook, which prints "Now using" (sdk.go:979) and runs the use diagnostics. A failed uninstall should not print that.
Fix: restore quietly, or print one line that says the uninstall failed and the global version is unchanged.
- TestCompareInstalledOrdersNewestFirst does not test InstalledList
The test re-implements the sort closure from InstalledList (installed_order_test.go:26-31) and sorts its own slice. I changed InstalledList back to plain text order, and the test still passed. The CLI tests fail under that change, so the behavior is covered somewhere, but this test would not catch a regression in InstalledList.
Fix: call InstalledList through a fixture with real version directories, or delete the test.
Regression tests: items 1 to 3 have none. Please add one for each. Each should fail on this head and pass after the fix.
Please also fix these in the same pass. They are not bugs by themselves:
- fallbackVersions (uninstall.go:94) says "newest first". It keeps the order it is given, so say that.
- The copy rule is written twice: uninstall.go:99 checks only Resolved, while sdk's copyReceipt also requires Requested. Expose one check from sdk and use it in the command.
- The Uninstall doc comment does not say what happens when fallbacks are given but none can be used.
Not in this diff: preUse still sorts with util.VersionSort, which treats 2.0.0-rc1 and 2.0.0 as equal. Here use alpha@2 picks 2.0.0 only because the new input order puts it first; on main it picked 2.0.0-rc1. It should use compareInstalled too, in a follow-up.
|
Thanks, all confirmed and fixed in 4703892.
Tests: one each for items 1 to 3, all failing on 0336a84, plus one for a config that cannot be written. There is no failure after the save to test any more, since the links come first. Also done: the |
linghengqian
left a comment
There was a problem hiding this comment.
Request changes.
I checked 4703892 against the base 90a96e1. go build, go vet (linux, windows, darwin), gofmt on the changed files and go test ./... all pass here, and CI is green. The points from the last round are fixed: the ordering, the retry over fallbacks, the dangling links of the global version, the quiet restore, and the InstalledList test. I still found seven things that block the merge. Each one needs a test that fails before the fix.
- A failed removal can leave the global version unset (new in this PR).
Uninstall unsets the global version and removes its links (internal/sdk/sdk.go:629-652) before os.RemoveAll (sdk.go:665). If RemoveAll deletes the main runtime and then fails on a file inside an addition, the deferred restore fails with "runtime not found". I reproduced it with an SDK test: 2.0.0 is the only version and it is global, and v-2.0.0/extra-2.0.0/locked is a read-only directory with a file in it. The result is an empty global config, no global link, and no main runtime.
The existing tests lock the version directory itself. RemoveAll then fails before it deletes anything, so the restore works. A locked addition is not covered.
Suggested direction: leave the config and the links alone until RemoveAll has succeeded, then unset them. Windows needs care here, because unuse builds the registry entries from the files, so those have to be computed before RemoveAll. Please add a test with a locked addition.
- Pre-releases with a number in the suffix sort in the wrong order (new in this PR).
compareInstalled compares the suffix as text (sdk.go:1091), so 2.0.0-rc9 ranks above 2.0.0-rc10. With 2.0.0, 2.0.0-rc9 and 2.0.0-rc10 installed, InstalledList returns them in that order. This shows in vfox list, in the fallback that uninstall picks (rc9 instead of rc10), and in GetInstalledVersions for plugins. The description says "by version number", and this case does not follow that. Please compare the numeric parts of the suffix as numbers, and add a test with rc9 and rc10.
- preUse still sorts with util.VersionSort (existing on main, but in code this PR reorders).
preUse (sdk.go:771-775) sorts with util.VersionSort. CompareVersion treats every pre-release of 2.0.0 as equal to 2.0.0, so the result depends on the input order. With only 2.0.0-rc9 and 2.0.0-rc10 installed, preUse("2") returns rc9 on main and on this head. use and exec (cmd/commands/exec.go:207) both go through it. use alpha@2 only picks 2.0.0 over its release candidates because the release happens to come first in the input. Please sort with compareInstalled here, and add a test.
- Uninstalling a version that is not global can leave dangling global links (existing on main; the description says it is fixed).
removeLinksInto only runs inside if b.globalVersion() == version (sdk.go:629-652). Repro: install 1.0.0 with an addition extra-1.0.0, use -g 1.0.0, use -g 2.0.0, then uninstall 1.0.0. sdks/extra is still there and its target is gone. The description says Uninstall removes the global links that still point into the removed directory, and this is that case. Please call removeLinksInto(path, env.Global) on every uninstall. It only removes links that point into the directory being removed, so it is safe when the version is not global.
- PreUninstall can run for a version that stays installed (new in this PR).
The switch comes after the PreUninstall hook (sdk.go:606-633). If no fallback can be used, for example because its EnvKeys errors, Uninstall returns an error and removes nothing, but the hook has already run. In a test that counts the hook calls, the count is 1 and 2.0.0 is still installed. Please check every fallback (resolve, installed, EnvKeys) before the hook, then run the hook, then switch. Add a test that counts PreUninstall calls.
- Best-effort errors are only logged at debug level (new in this PR; from reading the code).
removeLinksInto (sdk.go:1283) and revertLinks (sdk.go:1021) log at debug level and return nothing. The uninstall then prints "Uninstalled ... successfully!" even when a link could not be removed. AGENTS.md asks for best-effort cleanup to be explained in the code. Please either return the error, or add a comment that says why it is best effort and print a warning when it fails.
- The docs do not describe the new behavior (new in this PR).
docs/usage/core-commands.md (Uninstall, line 150) and docs/zh-hans/usage/core-commands.md (line 151) do not say that uninstalling the global version now switches to another installed version, or which one it picks. Please add a short paragraph to both.
|
Thanks for the careful review. All seven are in the new commit, each with a test that fails on 4703892:
The PR description is updated. |
linghengqian
left a comment
There was a problem hiding this comment.
Request changes.
I checked the PR at a81a08b. The no-hook fix works, go build, go vet (linux, windows, darwin) and go test ./... pass, gofmt is clean for the files this PR touches, and CI was green when I checked. Most of the earlier comments are addressed. I still found bugs in the failure paths of the uninstall code. Each item below is a bug and blocks the merge.
- A removal that fails partway leaves the global config and the links pointing at a version that is gone.
Repro on Linux: one installed version that is global, plus an addition whose subdirectory holds a file. Make that subdirectory read-only and run vfox uninstall [email protected]. It fails with permission denied and exits 1. In my run, alpha-2.0.0 was already deleted by then, since RemoveAll does not delete in a fixed order. The global config still says alpha = "2.0.0", and sdks/alpha still points into the deleted directory. Running the uninstall again fails with "is not installed" and changes nothing. vfox unuse -g alpha clears the config, but sdks/alpha and sdks/extra stay behind as dangling links.
On main, the same run clears the config and removes both links. Main also deletes part of the directory, so partial deletion is not new. What is new is that the config and the links still point at the version afterwards.
The config-unwritable case is worse than on main. Here the version is removed first, and then the config write fails: exit 1 with "could not be unset", the config still names 2.0.0, and a retry says "not installed". On main, the same failure happens before anything is removed: "failed to save configs", the config is unchanged, the version is still installed, and a retry works once the permissions are fixed.
Question: what state should a failed uninstall of the global version leave? Two possible rules are (a) the config never names a version that is not installed, and (b) a failed uninstall leaves the version installed and the config unchanged. After a partial removal, the code breaks both. Options I can see:
- Make the removal take effect in one step, for example by moving the version directory out of the
v-namespace before deleting its contents. The version is then either untouched or gone, and the config and links can be cleared once it is gone. - Keep the current order, and let a retry finish the global cleanup for a version that is not installed.
Whichever is chosen, please add a test that runs the retry after each failure. TestUninstallKeepsTheOnlyVersionWhenAnAdditionCannotBeRemoved only checks the state right after the failure.
- After a switch to a fallback, a failed removal leaves a stale link.
Repro: 1.0.0 (no addition) and 2.0.0 (with an addition extra) are installed, and 2.0.0 is global. Make the addition's subdirectory read-only and uninstall 2.0.0. The global moves to 1.0.0, then the removal fails with permission denied. sdks/extra still points at v-2.0.0/extra-2.0.0. removeLinksInto runs only after a successful removal (internal/sdk/sdk.go:663-674), and the retry is refused with "not installed", so nothing removes the link. On main, this command does not switch the global version, and the links are removed before the failing delete.
The output does not explain the state either. It ends with Auto switch to the other version: [email protected]. followed by unlinkat ...: permission denied. It does not say that 2.0.0 was not removed, or that the global version has already changed.
Expected: after a failed uninstall, the stale link is gone or a retry removes it, and the message says what state the uninstall left.
- PreUninstall can run for a version that stays installed.
Uninstall calls the hook (internal/sdk/sdk.go:628-646) before switchGlobal (internal/sdk/sdk.go:648-652). If the config cannot be written during the switch, the hook has already run, the command says "nothing was removed", and the version is still installed. I reproduced this in an SDK test with a read-only config: the hook is called once, 2.0.0 stays installed, and the global still names 2.0.0. The new checks cover EnvKeys and PreUse failures, but not this case. Main has the same order (its PreUninstall runs before Unuse writes the config), but this path is rewritten in this PR.
Question: should the switch happen before the hook? If so, what should happen when the hook fails? If not, the error should say that PreUninstall has already run. Whichever you choose, please add a test that counts hook calls with an unwritable config.
UseGlobalis not called by any code path.
Nothing outside internal/sdk/sdk.go and its tests calls UseGlobal (declared at sdk.go:64, defined at sdk.go:918). Uninstall goes through usableFallbacks and switchGlobal, but the PR description still says uninstall calls UseGlobal. Should the method be removed from the Sdk interface, with use_global_test.go rewritten against code that runs? Or should a production path call it? As it stands, those tests cover a function that no command can reach.
- Uninstall prints the
usemessage.
A successful fallback switch prints Now using [email protected]. and then Auto switch to the other version: [email protected].. The first line comes from useInHook (sdk.go:956). Main prints both lines too, but this code is rewritten here. The output should not say "Now using" for a switch the user did not ask for.
- On Windows, the unset path changes the registry before it saves the config.
unsetRemovedGlobal (internal/sdk/sdk.go:688-694) removes the registry entries and then saves the config. If the save fails, PATH has already been cleaned while the config still names the version. apply saves the config first and changes the registry afterwards, and unuse on main also removes the registry entries before it saves. I could not run this on Windows. On Linux, I checked the call order with a recording stub: the registry removal runs, then the save fails.
Expected: the registry changes only after the config save succeeds.
Nit, not a blocker: the doc comment on otherVersions still reads badly.
Please address the items above and request a new review.
|
Thanks, all six are fixed in f2eefbd. 1. and 2. I went with rule (b) up to the removal and rule (a) after it. The removal is now one step: the directory is renamed to I also reworded the |
linghengqian
left a comment
There was a problem hiding this comment.
Not ready to merge. The first problem is a regression. The other two are a broken guarantee and missing tests.
- Leftover directories change how a leading "v" is read (regression)
HasVersion now counts .v-<version>.removing leftovers (internal/sdk/sdk.go:1605). StripVersionPrefix uses HasVersion to decide whether a typed "v" is part of the name (internal/sdk/link.go:75), and ResolveVersionName (internal/manager_alias.go:72) depends on it. install (cmd/commands/install.go:161) calls the same function. I only read that path; I did not run it.
Repro with the CLI test harness: install [email protected], then create $VFOX_HOME/cache/alpha/.v-v1.0.0.removing.
vfox use -g [email protected]fails with "[email protected] is not installed" (exit 1). Without the directory it selects 1.0.0.vfox uninstall [email protected]prints "Deleted the files that were left of [email protected]." and exits 0. 1.0.0 is still installed.
A leftover is not a version, so it should not change how the name is read. One option: resolve installed names as typed, then installed names without the "v", then leftovers as typed. Add tests for use and uninstall with a leftover present. TestHasVersion (internal/sdk/link_test.go:616) has no leftover case, and no test calls StripVersionPrefix with one.
- The rollback guarantee fails when the rollback itself fails
The PR body says: "If anything fails up to step 3, the version stays installed, and the config, links and registry name it as before."
That does not hold when the restore also fails. I reproduced this with a PreUninstall hook that makes the config file, its directory and the install directory read-only, so the rename fails and then the restore fails.
- No other version: the global setting is unset and 2.0.0 is still installed.
- Fallback 1.0.0: the global setting is 1.0.0, 2.0.0 is still installed, and the link points to 1.0.0.
The error says "could not be set back" but not what the global version is now. No test reaches that branch (internal/sdk/sdk.go:634 has zero hits in the PR's tests). restoreGlobal (internal/sdk/sdk.go:730-755) also writes the config before it restores the links, so if the link restore fails, the config and the links disagree.
Either narrow the guarantee to the case where the restore succeeds, and make the error name the current global version and the command that fixes it, or add a real check before the first change. Either way, add a test that forces the restore to fail.
- Tests the PR describes do not exist
The PR comment on fallback checks says a fallback that PreUse resolves to the version being removed is refused, and that the test counts PreUninstall calls. The branch at internal/sdk/sdk.go:847 is never executed. The only PreUse test makes the hook return an error.
Also untested:
- the loop over additions in the PreUninstall context (internal/sdk/sdk.go:648)
- the final return in compareSuffix (internal/sdk/sdk.go:1298)
- the sa == "" branch in compareInstalled (internal/sdk/sdk.go:1269-1270)
The last two decide the order of versions in vfox list and use.
What I checked
- go build, go vet (linux, windows, darwin), gofmt on the touched files, and go test ./... on Linux all pass. The one gofmt complaint, internal/plugin/model_test.go, is in a file this PR does not touch.
- CI on the head commit was green on every job when I checked. Windows tests ran only in CI, not locally.
- By reading the code, I found no path that deletes a user's SDK directory. Links are removed as links, and RemoveAll does not follow them on Unix. The Windows junction case is checked by reading only.
- Rollback for failures before the rename is correct in every case I traced: links, config save, PreUninstall, and the rename.
|
All three are fixed in a106935. 1.
|
linghengqian
left a comment
There was a problem hiding this comment.
Requesting changes.
The fix for #809 works. On main, vfox uninstall [email protected] without a hook prints "Auto switch", then fails with "vfox requires hook support", and the global config ends up empty. On this branch the global version moves to 1.0.0 and the command succeeds. Build, go vet for linux, windows and darwin, go test ./... and CI are green.
The problems are in the failure paths of the new code. All five are blocking. Line numbers refer to a106935.
1. A failed uninstall drops the attributes of the global entry.
restoreGlobal writes the config back with vfoxToml.SetTool (sdk.go:754). That replaces the entry with a new ToolConfig and an empty Attr. releaseGlobal removes the entry with RemoveTool (sdk.go:722), so the loss also happens without a fallback. With test-sdk = { version = "2.0.0", vendor = "openjdk" } under [tools], a failed rename leaves test-sdk = "2.0.0". I reproduced this with and without a fallback. The description says the config is set back, and it is not. The restore should write back the entry it took, attributes included. A test should check it.
2. The links are not set back after a failed restore.
moveLinks in restoreGlobal returns early when there is no fallback (sdk.go:741-742), and it clears the target when CheckRuntimeExist is false (sdk.go:744-745). That gives two wrong results:
- No fallback. The removal fails, then the config write fails during the restore. The config has no global version, but
sdks/<sdk>still points at the version that is still installed. The description says the links agree with the config. They don't.TestUninstallSaysWhatTheGlobalVersionIsWhenItCannotBeRestoredpins this (internal/sdk/use_global_test.go:402). - The global version is a linked version whose target is gone, and the rename fails. The config goes back to the broken version, and the global link that existed before is removed and not recreated. I reproduced this.
I would snapshot the global entry and the global link targets before the switch, and put the snapshot back on failure. That way the restore does not depend on GetRuntimePackage succeeding.
3. Leftover files are cleaned up in some cases only.
A .v-<version>.removing directory is deleted only when that same version is uninstalled while it is not installed (sdk.go:596-599). The SDK directory is deleted only when no version is left (cmd/commands/uninstall.go:84-85). So:
- After a failed removal of 2.0.0, reinstalling 2.0.0 and uninstalling it succeeds with "Uninstalled [email protected] successfully!", and the leftover directory stays on disk with no message.
TestUninstallWhileFilesOfAnEarlierOneAreLeft(cmd/commands/uninstall_cli_test.go:259) locks this in. - If 2.0.0 was the last version, the
RemoveAlldeletes the leftover. While another version stays installed, only a later uninstall of the same version, run while that version is not installed, deletes it.
The docs say that running the command again deletes the rest. That is only true while the version is not installed. Please delete the leftovers of the same version after a successful removal, or report them, so the result does not depend on whether other versions exist.
4. Errors are dropped without a word.
- sdk.go:718:
registryEnvs, _ = b.EnvKeysForScope(version, env.Global) - sdk.go:762-766 in
restoreGlobal: an error fromEnvKeysForScopeis ignored, and_ = env.RemoveEnvsFromRegistry(envs)discards the result.
AGENTS.md asks for errors to be handled, or for best-effort cleanup to be explained in a comment. Either log at debug level with a comment, or return the error.
5. A skipped fallback is invisible.
usableFallbacks skips a candidate whose PreUse or EnvKeys fails, and records that only at debug level. With 1.0.0, 2.0.0 and 3.0.0 installed and 2.0.0 failing in EnvKeys, uninstalling 3.0.0 prints "Auto switch to the other version: [email protected]." and nothing about 2.0.0. AGENTS.md says actual PreUse hook errors propagate. Please print one warning per skipped candidate, with the reason, at the default level. If silent skipping is intended, say so in AGENTS.md too.
Checked, fine
go build,go vetfor linux, windows and darwin, andgo test ./...on linux as a non-root user pass.gofmt -lflags onlyinternal/plugin/model_test.go, which is unformatted on main too.git diff --checkis clean, and the changed files have no CRLF endings.- A broken linked global version with a usable fallback uninstalls correctly: the config switches, the global link points at the fallback, and the package is removed.
- Nothing was run on Windows. The registry branches are covered only by CI.
Please add regression tests for items 1 to 3 with this change.
|
Thanks, all five are real. Pushed c66a0db.
|
linghengqian
left a comment
There was a problem hiding this comment.
Request changes.
I checked c66a0db against the base 90a96e1 on Linux. Build, go vet for linux,
windows and darwin, gofmt on the changed files, go test ./... and go test -race
on internal/sdk and cmd/commands all pass. CI is green. I did not run the
Windows paths locally.
Three of the problems below are bugs in the uninstall path, and each one blocks
the merge. The first is a regression from this PR. The second predates it, but
it sits in the function this PR rewrites. The third is a missing recovery path.
- A fallback that depends on the removed directory is chosen (regression)
Repro on Linux, where alpha 2.0.0 is the only installed version:
vfox link alpha@x ~/.vfox/cache/alpha/v-2.0.0/alpha-2.0.0
vfox use -g [email protected]
vfox uninstall [email protected]
Actual: exit 0, "Uninstalled [email protected] successfully!", then "Auto switch to
the other version: alpha@x.", then "ERROR alpha@x is linked to ..., which no
longer exists." The config says alpha = "x", and ~/.vfox/sdks/alpha does not
resolve to a working runtime.
On 90a96e1 the same commands end with the global version unset. The candidate
is dropped there because InstalledList is read after the removal. This PR reads
the list before the removal, and switchGlobal checks each candidate only before
the rename. A candidate that goes through the directory being renamed passes the
check and breaks right after it.
Fix: in switchGlobal, resolve the candidate's main runtime and each addition
with filepath.EvalSymlinks, and refuse any that lie inside
b.packagePath(removed). Add a test with a fallback linked into the removed
directory. The expected result is either the next candidate or no change at all.
- An unreadable global config lets uninstall remove the version it names
Repro: ~/.vfox.toml contains "[tools" (invalid TOML) and alpha = "2.0.0", with
1.0.0 also installed. Run vfox uninstall [email protected].
Actual: exit 0, "Uninstalled [email protected] successfully!", the directory is gone,
and the config still names 2.0.0.
Cause: isGlobal is computed with globalVersion(), which logs the load error and
returns "". The globalState() added here returns the same error, but only after
isGlobal is already true. 90a96e1 behaves the same way, so this predates the PR,
but the decision is in the function this PR rewrites.
Fix: read the global version and keep the error, and return it before anything
changes. Add a test with an unparsable global config.
- A failed link removal cannot be cleaned up by running uninstall again
Repro: alpha 2.0.0 is the only installed version, it is global, and it has an
addition "extra" that exists only in 2.0.0. Run vfox use -g [email protected], make
~/.vfox/sdks read-only, then run vfox uninstall [email protected].
Actual: two warnings ("Some links to [email protected] could not be removed ...
permission denied"), then "Uninstalled [email protected] successfully!" and "Unset
alpha successfully.", exit 0. Make sdks writable and run the same command again:
"[email protected] is not installed", exit 1. sdks/alpha and sdks/extra still point
into the removed directory. vfox unuse -g alpha does not remove them either,
because Unuse only removes links of the version the config names.
Cause: the branch for a version that is not installed (Uninstall, the
"if !installed" block) deletes leftover files and returns. It never reaches
removeLinksInto, so no retry does the link cleanup.
Fix: run the global-link cleanup in that branch too, against
b.packagePath(version), since the links point at that path. Also make a link
that cannot be removed fail the command (exit 1, name the link), the same way
leftover files do, so that running uninstall again is the recovery path. Add a
test that runs the retry after the link warning. TestUninstallWarnsWhenALinkCannotBeRemoved
only checks the first run.
- Smaller items, all required
- The Uninstall doc comment (sdk.go:587-590) says that after the rename "all
that can fail is deleting its files". Removing links can fail too, and that
is reported as a warning. The PR description says the same ("After step 3
only deleting files can fail"). Fix the wording, or change the behavior as
item 3 describes. - compareInstalled (sdk.go:1344-1345) says a name that does not start with a
number "comes before every numbered one". sortNewestFirst reverses that
order, so dev and latest are listed last. Say which order the comment means. - use_global_test.go, and several test names in it such as
TestUseGlobalScopeWithoutTheHookOpensAShellOnWindows, still say UseGlobal,
which this PR removed. Rename the file and the tests.
Once items 1 to 3 have tests that fail on this head and pass after the fix,
please request a new review.
|
All four are done in 016612a.
The tests for 1 to 3 fail on c66a0db. |
linghengqian
left a comment
There was a problem hiding this comment.
Request changes.
I checked 016612a. Build, go vet (linux, windows, darwin), gofmt on the changed files and go test ./... all pass, and CI is green. The items from the previous round are addressed. One problem is left, and it blocks the merge.
- Uninstall can leave the global version broken and still report success.
Uninstall only looks at the global version when it is the version being removed. The global version can still reach the removed runtime through a link. Steps on Linux, with alpha 2.0.0 installed:
vfox link alpha@x ~/.vfox/cache/alpha/v-2.0.0/alpha-2.0.0
vfox use -g alpha@x
vfox uninstall [email protected]
The uninstall prints "Uninstalled [email protected] successfully!" and exits 0. Afterwards the global config still says alpha = "x", vfox list alpha prints "no available version", and sdks/alpha resolves through v-x/alpha-x into a directory that no longer exists.
A non-global linked version that points into the removed runtime is left unusable in the same way. It drops out of vfox list, and no warning is printed.
main behaves the same, so this is not a regression from this PR. It still blocks. This PR rewrites the uninstall path and adds dependsOn (internal/sdk/sdk.go:966), which refuses fallbacks that reach the removed directory (internal/sdk/sdk.go:946). The global version is the case that matters most, and the check does not cover it.
Requested change:
- Before anything changes, if the global entry names another installed version V and dependsOn(V, version) is true, return an error that names V and says the global version has to be switched first. Nothing is removed in that case.
- For non-global linked versions that reach the removed runtime, either refuse the same way or print a warning that names each one. A silent break is not acceptable.
- Add an internal/sdk test: a global version linked into the runtime of the version being removed. Assert the error, the unchanged global entry and global link, and that the version is still installed. A CLI test for the steps above would be good too.
- Nits, not bugs:
- internal/sdk/sdk.go:581: "That happens first, so a version that PreUninstall has run for is never one the global setting cannot be moved away from" is hard to follow. Say directly that the global switch runs before PreUninstall, so the hook never runs for a version the global setting cannot leave.
- cmd/commands/uninstall.go:90: "returns versions without version" could be "returns the versions other than version".
- internal/sdk/sdk.go:887: say when links are reported: only when the version's directory is gone and the global config does not name the version.
|
Done in 06b4ca2.
|
linghengqian
left a comment
There was a problem hiding this comment.
Request changes.
- uninstall deletes the record of a version the warning just called broken
cmd/commands/uninstall.go:84-85
With alpha@x linked into [email protected] and no other working version:
$ vfox uninstall [email protected]
Uninstalled [email protected] successfully!
WARNING alpha@x no longer works: it is linked into the directory of [email protected].
$ vfox doctor alpha
No problems found.
The warning is accurate, but right after it the command runs os.RemoveAll on the
SDK directory, because InstalledList() is empty. The v-x record goes with it.
If another working version is left, the same command keeps the record, and doctor
reports sdk.link-dangling with "fix: vfox uninstall alpha@x" (check.go:110).
So the outcome depends on an unrelated version, and in the first case nothing
tells the user that a record was removed.
Keep the broken records: remove the SDK directory only when no v-* entry is left,
broken links included. Add a CLI test that runs doctor after the uninstall in
both cases.
- Uninstalling an unrelated version now fails when the global config cannot be read
internal/sdk/sdk.go:600-604
With ~/.vfox.toml unparsable, "vfox uninstall [email protected]" fails with "could not
read the global version, so nothing was removed", although the config names
2.0.0. On main the same command succeeds. "vfox list" still works.
Failing closed is defensible, since the file might name the version. But it
changes every uninstall, and neither the docs nor the tests cover it (the existing
test only uses the version the config names). Either document it next to the
Uninstall section with the fix, or narrow the behavior and test that.
- The refusal hint has no target when nothing else is installed
internal/sdk/sdk.go:651
"switch the global version to another one first" cannot be followed when the only
other installation is the global linked version. Say "switch or unset the global
version first (vfox unuse -g )".
Not a bug, but it does not belong in this change: the "link" paragraph in
AGENTS.md (CheckNotAlias, --detect naming) is about link, not uninstall. Split it out.
What I ran: go build, go vet, gofmt on the changed files, and go test ./... on
Linux (go1.27.3): all pass. Random sequences of install, link, use -g and
uninstall (200 seeds, invariants checked after each step): no violations. The same
with random permission failures on the config, the link dir, the SDK dir and the
version dirs (300 seeds): a failed uninstall left the state unchanged, and a
removed version had no link left pointing into it. Not run: Windows and macOS
locally (CI is green on both).
|
Done in 99c041b.
AGENTS.md: the
|
linghengqian
left a comment
There was a problem hiding this comment.
Not ready to merge at 99c041b.
The uninstall code does what the description says in every case I ran. The docs give the wrong location for the global config, and that is the blocking item.
- The global config is not ~/.vfox.toml
docs/usage/core-commands.md:167 and docs/zh-hans/usage/core-commands.md:168 say the refusal applies when ~/.vfox.toml cannot be read. The global config is .vfox.toml in the user root, which is ~/.vfox unless ~/.version-fox exists (internal/pathmeta/path_meta.go:112). So the file is ~/.vfox/.vfox.toml. I checked both locations on Linux with a fresh HOME:
- a broken
~/.vfox.tomlis ignored, andvfox uninstall [email protected]succeeds; - a broken
~/.vfox/.vfox.tomlmakes the same command fail with "could not read the global version, so nothing was removed".
Someone who follows the page edits a file vfox never reads. Please fix both pages, and name the user root rather than a fixed path.
What else I checked on Linux with a sandboxed HOME, against main:
- uninstalling a linked global version, with and without a fallback, leaves no global link behind, and the fallback takes over when there is one;
- after 11.0.0 is removed, 10.0.0 takes over rather than 9.0.0;
- after a linked version is removed, its broken record stays, and vfox doctor reports sdk.link-dangling for it.
go build, go vet, gofmt and go test ./... pass.
|
Fixed in 360febe, both pages. They now say I ran the built binary with a fresh HOME: a broken |
linghengqian
left a comment
There was a problem hiding this comment.
You need to resolve merge conflicts.
Uninstalling the global version in a shell without 'vfox activate' printed 'Auto switch to the other version.' and then failed with exit status 1, because the switch went through Use, which needs the hook on Linux and macOS (and on Windows would open a new shell). The global scope is only the config file and its links, so uninstall now moves it with UseGlobal, which needs no hook and opens no shell. Fixes #809
- UseGlobal: reword the doc comment to say what it does. - useInHook: the hand-off to a new shell is now in the caller. - uninstall test: drop the claim about opening no shell, which the test does not check. - Add tests for a version that is not installed and a failing PreUse hook; both leave the global config unchanged.
…gistry - UseWithConfig: test that an EnvKeys error from useInHook is returned and the global config is kept. - Add an openShell seam: UseGlobal opens no shell with or without the hook, and on Windows use -g without the hook still opens one. - Make env.ApplyEnvsToRegistry and env.RemoveEnvsFromRegistry variables so the sdk tests and the uninstall CLI tests leave the user's registry alone. - Share the PreUse and installed check of UseWithConfig and UseGlobal in resolveInstalled.
Sort InstalledList by version number (10.0.0 before 9.0.0), so the fallback and vfox list agree. Switch the global version to the fallback before the version is removed, and make the version global again if its removal fails after it was unset. Fix the openShell and UseGlobal comments.
- Uninstall takes the versions that may take over and switches the global version after the installed check and PreUninstall, before removing anything. A failed check or hook leaves the global version alone. - Each fallback is tried in order; the first that can be used takes over. If none can, nothing is removed and the error names each one that failed. "Auto switch" is printed only after the switch worked. - InstalledList ignores a leading "v", puts a pre-release before its release and names that do not start with a number after numbered ones.
…iled restore leaves
… finish earlier uninstalls
…n an unreadable config or links that stay A fallback that is reached through the directory of the removed version broke right after the switch. It is refused now, like one that resolves to the removed version. An unreadable global config was taken for one that does not name the version. It stops the uninstall before anything changes. A global link that could not be removed was only a warning, and no later command removed it. It fails the command, and the next uninstall of the version removes it.
…r versions that break
…to leave the global version
|
Rebased on main (39d3679). The only conflict was the command list in |
360febe to
39d3679
Compare
Problem
Fixes #809. On Linux and macOS,
vfox uninstall <sdk>@<global version>in a shell withoutvfox activateprints "Auto switch to the other version.", then fails with exit status 1 ("vfox requires hook support"), and the global config is left with no version.Root cause
uninstallCmdmoves the global version withsource.Use(..., env.Global).UseWithConfigchecks the hook first and returns that error on Linux/macOS. On Windows it gets past the check, butuseInHookthen opens a new shell (shell.Open), which an uninstall should not do either.Fix
Sdk.Uninstall(version, fallbacks...)does not go throughUseWithConfig. The global config is read first: if it cannot be read, nothing is removed, whichever version is asked for (documented). Whenversionis the global one, the order is:EnvKeys, links, config) and make the first one that works the global version, with one warning for each that was skipped, or unset it when no other version is installed;PreUninstall;.v-<version>.removing, which is the one step that removes it;vfox use -gsets it back. After step 3, removing the global links and deleting files can fail: the command says what is left and where, exits 1, and the next uninstall of that version removes the rest, whether it was installed again or not.dependsOn).dependents). Their entries are kept, sovfox doctorreports them: the directory of the SDK goes only when no version directory or interrupted installation is left in it (HasVersionDirs).applyis links, config, then Windows registry. If the links or the config fail, the links go back to what they were (linksOfScope,restoreLinks). This also applies tovfox use. The registry is changed only after the config is saved, also when the version is unset.Uninstallremoves the global links that point into the removed directory, also when the version is not the global one. A link that cannot be removed is named and fails the command.useInHookup intoUseWithConfig, sovfox usebehaves as before and uninstall never opens a shell. Uninstall prints "Auto switch" only, after the version is gone.uninstallpasses the other installed versions: installations first, then copies made withinstall --as.Order change
InstalledListnow sorts newest first by version number instead of by text: 10.0.0 before 9.0.0, a leadingvignored (v20.1.0 = 20.1.0), a pre-release before its release (2.0.0 > 2.0.0-rc1), the numbers in a suffix compared as numbers (rc10 > rc9), names that do not start with a number (dev, latest) last.preUseuses the same order, souse alpha@2picks 2.0.0-rc10 over 2.0.0-rc9;util.VersionSorthad no other user and is gone. This changes the order in every place that lists installed versions:vfox list, thevfox usepicker, the legacy-file lookup, andGetInstalledVersionsfor plugins.Testing
TestUninstallSwitchesTheGlobalVersionWithoutAHook(fixture with__VFOX_SHELLremoved): fails on main with exit 1 and the hook message, passes now.PreUninstallfails. Each with and without another version where that matters. They fail on a81a08b, except the links,PreUninstalland CLI config-with-a-fallback cases, which pass on both.PreUninstallcalls and registry calls are counted with an unwritable config (0 on the failure, 1 hook call on the retry).PreUseresolves to the removed version, the additions given toPreUninstall, and leftovers with a leadingv.usefails; leftovers of an earlier uninstall are deleted or reported; a skipped fallback is named. Checked by taking each piece out of the code: a test fails every time.vfox doctorafter it. Each piece taken out of the code fails a test.InstalledListandpreUse, fallback choice, additions' links and links of a version that is not global: as before.go test ./...passes on macOS;go vetclean for linux, windows and darwin; gofmt clean for the touched files. Not run on Windows.