Skip to content

fix(uninstall): switch the global version without the shell hook - #811

Merged
linghengqian merged 14 commits into
version-fox:mainfrom
bardrop-oss:fix-uninstall-switch-without-hook
Nov 24, 2026
Merged

linghengqian merged 14 commits into
version-fox:mainfrom
bardrop-oss:fix-uninstall-switch-without-hook

Conversation

@bardrop

@bardrop bardrop commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Fixes #809. On Linux and macOS, vfox uninstall <sdk>@<global version> in a shell without vfox activate prints "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

uninstallCmd moves the global version with source.Use(..., env.Global). UseWithConfig checks the hook first and returns that error on Linux/macOS. On Windows it gets past the check, but useInHook then opens a new shell (shell.Open), which an uninstall should not do either.

Fix

  • Sdk.Uninstall(version, fallbacks...) does not go through UseWithConfig. The global config is read first: if it cannot be read, nothing is removed, whichever version is asked for (documented). When version is the global one, the order is:
    1. go through the fallbacks (resolve, installed, 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;
    2. run PreUninstall;
    3. rename the version directory to .v-<version>.removing, which is the one step that removes it;
    4. remove the global links that still point into it, and delete its files and those an earlier uninstall of the same version left.
  • If anything fails up to step 3, the version stays installed. The global entry (attributes included) and the links of this SDK are put back from a snapshot taken before the switch, so this works for a broken linked version too, and the registry follows as far as the plugin can give the environment. If that fails too, the links agree with the config, and the error says what the global version is now and that vfox use -g sets 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.
  • A fallback whose main runtime or an addition is reached through the directory being removed, also when it only passes through it, is refused like one that resolves to the removed version (dependsOn).
  • If the global version is another one that is linked into the directory being removed, nothing is removed, and the error says to switch or unset the global version first. Other versions linked into it get a warning each after the removal (dependents). Their entries are kept, so vfox doctor reports them: the directory of the SDK goes only when no version directory or interrupted installation is left in it (HasVersionDirs).
  • The order inside apply is 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 to vfox use. The registry is changed only after the config is saved, also when the version is unset.
  • Every Uninstall removes 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.
  • The "open a new shell when there is no hook" step moved from useInHook up into UseWithConfig, so vfox use behaves as before and uninstall never opens a shell. Uninstall prints "Auto switch" only, after the version is gone.
  • uninstall passes the other installed versions: installations first, then copies made with install --as.

Order change

InstalledList now sorts newest first by version number instead of by text: 10.0.0 before 9.0.0, a leading v ignored (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. preUse uses the same order, so use alpha@2 picks 2.0.0-rc10 over 2.0.0-rc9; util.VersionSort had no other user and is gone. This changes the order in every place that lists installed versions: vfox list, the vfox use picker, the legacy-file lookup, and GetInstalledVersions for plugins.

Testing

  • TestUninstallSwitchesTheGlobalVersionWithoutAHook (fixture with __VFOX_SHELL removed): fails on main with exit 1 and the hook message, passes now.
  • Failure paths, each followed by a retry: the directory cannot be renamed, files cannot be deleted, the config cannot be written, the links cannot be moved, PreUninstall fails. Each with and without another version where that matters. They fail on a81a08b, except the links, PreUninstall and CLI config-with-a-fallback cases, which pass on both.
  • PreUninstall calls and registry calls are counted with an unwritable config (0 on the failure, 1 hook call on the retry).
  • A restore that fails (config, links), a fallback that PreUse resolves to the removed version, the additions given to PreUninstall, and leftovers with a leading v.
  • A failed uninstall keeps the attributes and the links, with and without a fallback, and leaves the links of other SDKs alone; the link of a broken linked version comes back when the rename, the switch or a use fails; 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.
  • Fallbacks linked into the removed directory (directly, over, below and through other links, and an addition), a global config that cannot be parsed, and global links that cannot be removed, with the retry: these fail on c66a0db. Each piece taken out of the code fails a test.
  • A global version that depends on the removed directory, in the same six ways, and the warning for one that is not global, with vfox doctor after it. Each piece taken out of the code fails a test.
  • The order of InstalledList and preUse, fallback choice, additions' links and links of a version that is not global: as before.
  • go test ./... passes on macOS; go vet clean for linux, windows and darwin; gofmt clean for the touched files. Not run on Windows.

Copilot AI balanced review requested due to automatic review settings November 21, 2026 13:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@linghengqian linghengqian added this to the 1.1.1 milestone Nov 21, 2026
@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.31579% with 122 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.98%. Comparing base (1953446) to head (39d3679).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/sdk/sdk.go 73.18% 83 Missing and 35 partials ⚠️
internal/env/registry_stub.go 0.00% 2 Missing ⚠️
internal/sdk/installation.go 75.00% 1 Missing and 1 partial ⚠️
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.
📢 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.

Request changes.

The fix itself is correct, and uninstall no longer needs the hook. Three things need to change before this merges.

  1. Two comments need fixing. The comment above b.reportFindings(version, hookEnv) in useInHook (internal/sdk/sdk.go:938-939) says the hand-off to a new shell comes "below". That hand-off moved to UseWithConfig in this PR, so the comment points at nothing now. The doc comment on UseGlobal (sdk.go:821-824) ends with a clause about the caller that does not say what the function does. Please reword it to say what UseGlobal does and how it differs from Use.

  2. The test comment claims more than the test checks. The comment in TestUninstallSwitchesTheGlobalVersionWithoutAHook says 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.

  3. UseGlobal has 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 to internal/sdk, following the setup of TestUseWarnsWhenTheVersionIsShadowed:

    • a version that is not installed: expect *VersionNotExistsError and an unchanged global config;
    • a PreUse hook that fails (a Lua plugin defining PLUGIN:PreUse, as install_as_test.go does): expect the error and an unchanged global config.

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 vet is 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 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 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.

  1. Some changed lines in UseWithConfig are not run by any test: the return err after 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.

  2. 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 -g without a hook still opens one.

  3. 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.

  4. 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.

@bardrop

bardrop commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

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 use -g without the hook still opening one, through a new openShell variable. The registry functions in env are now variables, and the sdk tests and both uninstall CLI tests swap them out, and UseWithConfig and UseGlobal share resolveInstalled. I broke each guarded line on purpose and the matching test failed. The Windows test I could only check by forcing the Windows branch on macOS, so CI is the real run for it.

@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.

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.

  1. 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.

  1. 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.

  1. 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.

@bardrop

bardrop commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, all three fixed in cc6f52c.

  1. InstalledList now sorts with util.CompareVersion (newest first, ties by text). New test: 9.0.0, 10.0.0, 11.0.0 with 11.0.0 global → vfox list shows 11, 10, 9, and the uninstall moves the global version to 10.0.0.
  2. uninstallCmd now switches to the fallback first, then removes. If the switch fails, nothing is removed and the global version stays. In Uninstall, if removing fails after the global version was unset, the version is made global again. Tests: read-only install dir (with and without another version left) and a fallback whose EnvKeys errors. Each checks that an installed version is still global afterwards.
  3. Both comments fixed.

All new tests fail on the previous commit. go test ./... passes, go vet is clean for linux, windows and darwin, gofmt is clean.

@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.

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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

@bardrop

bardrop commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, all four fixed in 0336a84, with a test for each (all six fail on cc6f52c).

  1. InstalledList now sorts with its own key (compareInstalled in sdk.go): a leading v is ignored for ordering only, names that do not start with a number go below numbered ones. CompareVersion is untouched.
  2. The switch moved into Uninstall, after the "is installed" check and PreUninstall, before the removal. A version that is not installed or a failing hook leaves the global config as it was. The removal-failure case keeps the switch.
  3. Each fallback is tried in order (installations first, then install --as copies). If none can be used, nothing is removed and the error names each one. "Auto switch" is printed only after the switch worked.
  4. A suffix sorts below the bare version, so 2.0.0 is picked before 2.0.0-rc1.

The description now lists the order change for list, the use picker, the legacy-file lookup and GetInstalledVersions. The otherVersions comment is reworded.

@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.

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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

@bardrop

bardrop commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, all confirmed and fixed in 4703892.

  1. After the global switch, Uninstall removes the global links whose target is inside the directory being removed. The main link is kept because it points at the fallback. The same runs when there is no fallback.
  2. useInHook now creates the links first, then saves the config, then updates the Windows registry. If the links or the config fail, the links go back to the version in the config. I checked vet for Windows, but I have not run the registry part on Windows either.
  3. The restore is quiet, and "Unset" is printed only after the removal worked.
  4. TestInstalledListIsNewestFirst calls InstalledList on real version directories. It fails when I switch back to text order.

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 fallbackVersions and Uninstall comments, and Receipt.IsCopy() is now the one copy check, used by the command too. I left preUse for a follow-up.

@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.

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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

@bardrop

bardrop commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review. All seven are in the new commit, each with a test that fails on 4703892:

  1. With no fallback, the config, links and registry stay as they are until the removal has succeeded, then they are unset. The registry entries are computed before it. Test with a locked addition: TestUninstallKeepsTheOnlyVersionWhenAnAdditionCannotBeRemoved. If the config cannot be written after that, the command fails with a clear error and the aliases are still cleaned up.
  2. The numbers in a suffix compare as numbers. Test with rc9/rc10 and beta.2/beta.11.
  3. preUse sorts with the same function as InstalledList. util.VersionSort had no other user, so I removed it.
  4. removeLinksInto runs on every uninstall, after the removal.
  5. Every fallback is checked (resolve, installed, EnvKeys) before the hook. A fallback that PreUse resolves to the version being removed is refused too. The test counts the PreUninstall calls.
  6. revertLinks and removeLinksInto return their errors. apply joins them to its own error. After a removal it is best effort, with a comment that says why and a warning.
  7. Both docs pages have a paragraph. Please check my Chinese wording.

The PR description is updated.

@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.

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.

  1. 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.

  1. 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.

  1. 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.

  1. UseGlobal is 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.

  1. Uninstall prints the use message.

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.

  1. 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.

@bardrop

bardrop commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

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 .v-<version>.removing. Before that, any failure leaves the version installed, and the config and links go back to it. After that, only deleting files can fail. The config and links are already right then, the message says the version is no longer installed and where the files are, and the same command again deletes them. The tests run the retry after each failure, with and without another version.
3. The switch (or unset) now happens before the hook. If the hook fails, the config and links go back to the version. With an unwritable config the hook runs 0 times, and once on the retry. If the rename fails after the hook, the error says that the hook has already run.
4. UseGlobal is gone, and its tests now use UseWithConfig and Uninstall.
5. Uninstall prints only "Auto switch", after the version is gone.
6. The config is saved first, then the registry. A test with a recording stub checks that nothing touches the registry when the save fails.

I also reworded the otherVersions comment and updated the description and the docs. I could not run it on Windows, only vet it there.

@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.

Review of #811 (head f2eefbd)

Not ready to merge. The first problem is a regression. The other two are a broken guarantee and missing tests.

  1. 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.

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.

  1. 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.

  1. 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.

@bardrop

bardrop commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

All three are fixed in a106935.

1. HasVersion no longer counts leftovers, so they do not change how a name is read. uninstall looks for leftovers under the name as typed only when nothing is installed under it or without the "v" (new HasLeftovers). Your repro is a CLI test now (use, uninstall, then the leftovers), and TestHasVersion and StripVersionPrefix have a case with one.
2. I narrowed the description. restoreGlobal now goes links, config, registry, and puts the links back when the config fails, so the two agree. The error names the current global version (or says there is none) and the command. The test forces the restore to fail with your hook, for the config with and without a fallback, and for the links.
3. You are right, I said that the fallback that PreUse resolves to the removed version was tested, and it was not. That test is there now and counts the PreUninstall calls. The additions loop and both compare branches have tests too.

go test ./... passes on macOS, vet is clean for linux, windows and darwin.

@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.

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. TestUninstallSaysWhatTheGlobalVersionIsWhenItCannotBeRestored pins 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 RemoveAll deletes 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 from EnvKeysForScope is 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 vet for linux, windows and darwin, and go test ./... on linux as a non-root user pass.
  • gofmt -l flags only internal/plugin/model_test.go, which is unformatted on main too.
  • git diff --check is 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.

@bardrop

bardrop commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, all five are real. Pushed c66a0db.

  1. Attributes. The global entry is read before the switch and that same entry is written back, with its attributes. TestUninstallRestoresTheAttributesAndTheLinks checks it with and without a fallback.
  2. Links. linksOfScope takes the links before the switch and restoreLinks puts them back, so nothing depends on GetRuntimePackage any more. It touches only links that are or were this SDK's. apply uses the same, so a use that fails over a broken linked version keeps its link too. When the config cannot be restored: without a fallback the links into the version are removed, with one they stay at the fallback. The test that pinned the old result now expects no link. TestTheLinkOfABrokenVersionIsRestored covers your second case, plus a failing switch and a failing use.
  3. Leftovers. Every successful removal also deletes the leftovers of the same version, and reports the ones it cannot delete (exit 1). Replaced the test that locked the old behavior in with TestUninstallDeletesTheFilesOfAnEarlierOne (with and without another version) and TestUninstallReportsTheFilesOfAnEarlierOne. Docs updated, en and zh.
  4. Errors. Both EnvKeysForScope results and the RemoveEnvsFromRegistry one in the restore are logged at debug level, with a comment on why they are best effort.
  5. Skipped fallbacks. One warning per skipped candidate, with the reason. Candidates after the one that took over are not tried any more, so they are not named. usableFallbacks went into switchGlobal, since apply checks EnvKeys before it changes anything. AGENTS.md says it too.

go test ./... passes on macOS and go vet is clean for linux, windows and darwin. Nothing run on Windows by me.

@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.

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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

@bardrop

bardrop commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

All four are done in 016612a.

  1. Fallback in the removed directory. switchGlobal refuses it, for the main runtime and the additions. I walk the links one by one instead of using EvalSymlinks: a fallback that goes into the removed directory and out again over a link in it ends outside, but breaks all the same. The walk compares directories with os.SameFile. With your repro the command fails, the global version stays 2.0.0 and nothing is removed. With another version left, that one takes over. Two versions linked to the same outside directory still work.
  2. Unreadable config. The global entry is read first and the error is returned before anything changes. This also stops the uninstall of a version the config may not name, because there is no telling.
  3. Links. A link that cannot be removed fails the command (exit 1, ErrLinksLeft, the path in the message), and the next uninstall of the version removes it, also under a name with a leading "v". It does that only when the directory of the version is gone and the config does not name it, so it does not touch the links of a version that was deleted by hand. TestUninstallThatLeavesLinksIsFinishedByARetry replaces the test that only checked the first run, and runs it for the only version and for one that is not global.
  4. Small items. Doc comment, description and the compareInstalled comment are fixed, and the docs mention the links. use_global_test.go is uninstall_test.go, the test is TestUseWithoutTheHookOpensAShellOnWindows, and the helper is mustMakeGlobal.

The tests for 1 to 3 fail on c66a0db. go test ./... and go test -race on both packages pass on macOS, and go vet is clean for linux, windows and darwin. I have not run the Windows paths locally.

@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.

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.

  1. 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.
  1. 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.

@bardrop

bardrop commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Done in 06b4ca2.

  1. Global version. Uninstall collects the installed versions that depend on the directory first (dependents). If the global version is one of them, it returns an error that names it and says to switch first, before anything changes. Your steps now exit 1 with "the global version alpha@x is linked into the directory of [email protected], so nothing was removed".
    The others get one warning each after the removal: "alpha@x no longer works: it is linked into the directory of [email protected]." I chose the warning over refusing, because these can be unlinked or linked again at any time.
    TestUninstallRefusesAVersionTheGlobalOneDependsOn checks the error, the entry, the links, both versions and the hook count, for the six ways of depending that the fallback test uses, and that the uninstall works after the switch. TestUninstallWarnsOfTheVersionsThatBreak covers the warning. Both cases have a CLI test. Docs (en, zh) and description updated.
    One thing I left as it is on main: when no working version is left after that, the command deletes the directory of the SDK, and the broken x entry with it.
  2. Nits. All three comments are changed.

go test ./... and -race on internal/sdk and cmd/commands pass on macOS, vet is clean for linux, windows and darwin.

@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.

Request changes.

  1. 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.

  1. 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.

  1. 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).

@bardrop

bardrop commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Done in 99c041b.

  1. Broken records. The SDK directory is removed only when no v-* entry is left in it, broken links included (HasVersionDirs). I count an interrupted installation too, since doctor reports that as well. TestUninstallKeepsTheEntriesThatBreak runs doctor after the uninstall in both cases, then the fix doctor names, and checks that the directory goes only when nothing is left.
  2. Unreadable config. Documented next to Uninstall (en, zh) with the fix. I kept it for every version, because the file may also name a version linked into the one being removed. The test now covers a version the config does not name, and the retry after the file is corrected or deleted.
  3. Hint. It now says "switch or unset the global version first (vfox unuse -g alpha)". The CLI test follows it.

AGENTS.md: the CheckNotAlias and --detect text is on main already, this PR only changed the end of that line. I moved the uninstall sentence to a bullet of its own, so the only change left in the link paragraph is the rename to fallbackVersions.

go test ./... passes on macOS, vet is clean for linux, windows and darwin, and -race on the uninstall, link and doctor tests of internal/sdk passes.

@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.

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.

  1. 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.toml is ignored, and vfox uninstall [email protected] succeeds;
  • a broken ~/.vfox/.vfox.toml makes 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.

@bardrop

bardrop commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 360febe, both pages. They now say .vfox.toml in the user root, which is ~/.version-fox if that directory exists, otherwise ~/.vfox.

I ran the built binary with a fresh HOME: a broken ~/.vfox.toml is ignored, a broken ~/.vfox/.vfox.toml stops the uninstall. With ~/.version-fox as the user root, the file in it stops the uninstall and ~/.vfox/.vfox.toml is ignored. No code changed.

@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.

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.
…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.
@bardrop

bardrop commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main (39d3679). The only conflict was the command list in cmd/commands/cli_test.go: main added Add, this PR added Unuse, both are kept. Nothing else changed, and go test ./... passes on macOS.

@bardrop
bardrop force-pushed the fix-uninstall-switch-without-hook branch from 360febe to 39d3679 Compare November 24, 2026 15:06

@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 added bug Something isn't working and removed need-more-info labels Nov 24, 2026
@linghengqian
linghengqian merged commit 43acf4e into version-fox:main Nov 24, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uninstalling the current version exits 1 without a shell hook, and nothing is left in use

3 participants