From 4ae151865c0efded2b7e2a48e1e2216e3733db00 Mon Sep 17 00:00:00 2001 From: Qiheng He Date: Tue, 10 Nov 2026 10:33:36 +0800 Subject: [PATCH 1/2] feat shadowHints adaptation --- README.md | 4 + README_CN.md | 2 + docs/changes.md | 18 +++- docs/development.md | 12 ++- docs/managers.md | 184 ++++++++++++++++++++++++++++++++++ docs/pnpm.md | 18 +++- docs/troubleshooting.md | 11 +- docs/zh-CN/changes.md | 6 +- docs/zh-CN/development.md | 8 +- docs/zh-CN/managers.md | 169 +++++++++++++++++++++++++++++++ docs/zh-CN/pnpm.md | 14 ++- docs/zh-CN/troubleshooting.md | 10 +- hooks/pre_use.lua | 31 +++--- lib/util.lua | 42 ++++++++ metadata.lua | 37 +++++++ tests/hooks_test.lua | 76 +++++++++++++- 16 files changed, 611 insertions(+), 31 deletions(-) create mode 100644 docs/managers.md create mode 100644 docs/zh-CN/managers.md diff --git a/README.md b/README.md index 519dddd..97ef957 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,10 @@ node -v `vfox search nodejs` lists every release, with the npm that comes with it and a mark on the ones of an LTS line. In an interactive terminal it lets you pick one. +Behavior that only exists on `main` and not in the latest release is marked +"Unreleased" in the pages below. [docs/changes.md](docs/changes.md) lists it, and +[docs/source-build.md](docs/source-build.md) gets you `main`. + ### Example: Ubuntu 26.04 ```bash diff --git a/README_CN.md b/README_CN.md index 58f101d..a92471f 100644 --- a/README_CN.md +++ b/README_CN.md @@ -34,6 +34,8 @@ node -v `vfox search nodejs` 会列出每个版本及其自带的 npm,属于 LTS 版本线的版本带有标记。在交互式终端里可以从中选一个。 +只存在于 `main`、还没进入最新发布版的行为,在下面各页里标有“未发布”。[docs/zh-CN/changes.md](docs/zh-CN/changes.md) 里列出了这些行为,想直接用上 `main`,见 [docs/zh-CN/source-build.md](docs/zh-CN/source-build.md)。 + ### 示例:Ubuntu 26.04 ```bash diff --git a/docs/changes.md b/docs/changes.md index 68c8fdf..607ee87 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -4,7 +4,23 @@ What changed in each release, as a user notices it. GitHub's [release pages](https://github.com/version-fox/vfox-nodejs/releases) list the -pull requests. +pull requests. The newest release at the time of writing is 0.5.4, and `main` is +ahead of it by everything under "Unreleased". +[source-build.md](source-build.md) gets you those changes now. + +## Unreleased + +- On vfox 1.1.0 and later a `node` that shadows vfox's is reported by vfox + itself with the plugin's hint, instead of the plugin's `Note:`. Release + 0.5.4 prints `Note: comes before vfox in PATH, ...` from + `PreUse` on every vfox, so with the new vfox the same shadowing prints + both the core warning and the plugin note. The plugin now declares + `PLUGIN.shadowHints` for pnpm, mise, asdf, nvm, Homebrew and winget (each + with its handoff command and a link to `docs/pnpm.md` or the new + `docs/managers.md`) and keeps its own `Note:` for vfox before 1.1.0 + only, which ignores the new metadata field. See [pnpm.md](pnpm.md), + [managers.md](managers.md) and + [troubleshooting.md](troubleshooting.md). ## 0.5.4 (2026-11-04) diff --git a/docs/development.md b/docs/development.md index 848ccd1..7addd82 100644 --- a/docs/development.md +++ b/docs/development.md @@ -5,14 +5,14 @@ ## Layout ```text -metadata.lua plugin name, version, and .node-version and .nvmrc as legacy files +metadata.lua plugin name, version, .node-version and .nvmrc as legacy files, and shadowHints for a shadowing node hooks/available.lua vfox search: the version index hooks/pre_install.lua vfox install: choose the package, its URL and its checksum hooks/post_install.lua musl: fail when node cannot run, such as without libstdc++ or libatomic hooks/env_keys.lua PATH: bin on Linux and macOS, the install directory on Windows -hooks/pre_use.lua vfox use: map 24 or 24.21 to an installed release, note a shadowing node +hooks/pre_use.lua vfox use: map 24 or 24.21 to an installed release (and, on vfox before 1.1.0, note a shadowing node) hooks/parse_legacy_file.lua .nvmrc and .node-version -lib/util.lua mirror URL and download headers, musl detection and missing libraries, running a command, version comparison and matching, shorthands, PATH probe, the LTS lines the plugin learned +lib/util.lua mirror URL and download headers, musl detection and missing libraries, running a command, version comparison and matching, shorthands, PATH probe, vfox version check, the LTS lines the plugin learned .lts-cache not in the repository: what the plugin learned about LTS lines, written next to it tests/hooks_test.lua offline hook tests tests/e2e/ container tests, see e2e.md @@ -235,8 +235,10 @@ errors of a failed checksum request, musl and `PostInstall` (with `util.capture` `ParseLegacyFile` under each strategy, in UTF-16 and with a byte order mark, and with the removal of `env-state.json` (`os.getenv` and `os.remove` stubbed), what the plugin learns about LTS lines (the file is a temporary one, and every case -starts with a fresh one that knows no line), `PreUse` with the shadowing `node`, -and the helpers of `lib/util.lua`. Add the case a change needs there. +starts with a fresh one that knows no line), `PreUse` with and without the +shadowing `node` on old and new vfox (`RUNTIME.version` before and from +1.1.0), `PLUGIN.shadowHints` in `metadata.lua`, and the helpers of +`lib/util.lua`. Add the case a change needs there. gopher-lua 1.1.1 loses a `local x = nil` that a closure assigns: the outer scope still reads `nil`. A test that collects a value from inside `with_util` or diff --git a/docs/managers.md b/docs/managers.md new file mode 100644 index 0000000..2d8f6cf --- /dev/null +++ b/docs/managers.md @@ -0,0 +1,184 @@ +# Other Node.js managers + +[中文](zh-CN/managers.md) | [English](managers.md) + +pnpm has its own page ([pnpm.md](pnpm.md)): `pnpm runtime set node -g` +puts a second `node` into `$PNPM_HOME/bin`. This page is for the same +problem from the other managers people commonly have next to vfox: +mise, asdf, nvm, Homebrew and winget (or the official installer, which +puts `node` in the same place as winget). + +The symptom is always the same: `vfox use nodejs@` prints +`Now using`, `vfox current nodejs` agrees, but `node --version` prints +the other manager's version, because its `node` comes before vfox's in +`PATH` (`type -a node` lists it first). On vfox 1.1.0 and later vfox +warns itself and appends the plugin's hint for the manager it +recognizes; on older vfox the plugin prints its own `Note:`. Unreleased: +release 0.5.4 prints the `Note:`; on vfox 1.1.0 and later the warning +comes from vfox itself with the plugin's hint. + +Pick at most one manager for `node` in a shell. There are two roads. +Road A, the default below, hands `PATH` to vfox without removing the +other manager itself: its `node` stops shadowing, and vfox installs and +selects its own releases. Road B keeps the files where they are and +registers one of them with vfox: + +```sh +vfox link nodejs@ +vfox use nodejs@ +``` + +`` is the SDK directory the plugin expects: the directory holding +`bin/` with `bin/node` in it on Linux and macOS, the directory holding +`node.exe` itself on Windows. `vfox link` checks that and refuses +anything else. Link the real install, never a shims directory +(`mise/shims`, `.asdf/shims`, `$PNPM_HOME/bin`): a shim is a +version-switching proxy, and pnpm's shim additionally ships no npm, so +a linked shim selects nothing stable. A linked version stays its +owner's: the other manager may upgrade or prune it underneath vfox +(`brew upgrade` replaces the Cellar directory, mise/asdf prune +installs), vfox only reports that, `vfox install --reinstall` does not +apply to it, and `vfox uninstall` removes the link only. The `` +is used as written (`24-system`, `brew-24`, `nvm-22`); use letters, +digits, `.`, `_`, `+` and `-`, never `latest`. The nodejs plugin has no +`Detect` hook, so `vfox link --detect nodejs` finds nothing; pass the +directory yourself. + +## mise + +mise keeps shims in `~/.local/share/mise/shims/` (`%LOCALAPPDATA%\mise\shims\` +on Windows, e.g. `...\mise\shims\node.exe`) and the real installs in +`~/.local/share/mise/installs/node//`. `mise activate` puts the +shims first in `PATH`, so its `node` wins. + +To let vfox own `node` (road A), drop the request and delete the +installs: + +```sh +mise unuse -g node +mise uninstall --all node +``` + +In a project directory, `mise unuse node` edits that project's config +instead of the global one. Official reference: +[uninstall](https://mise.jdx.dev/cli/uninstall.html) and +[unuse](https://mise.jdx.dev/cli/unuse.html). + +Or link one real install (road B); never the shims directory: + +```sh +mise ls node +vfox link nodejs@mise-22 ~/.local/share/mise/installs/node/22.0.0 +vfox use nodejs@mise-22 +``` + +## asdf + +asdf keeps shims in `~/.asdf/shims/` (`$ASDF_DATA_DIR/shims` when that +is set), e.g. `~/.asdf/shims/node`, and the real installs in +`~/.asdf/installs/nodejs//`. The shims directory is prepended +to `PATH`, so its `node` wins. + +To let vfox own `node` (road A), uninstall the versions and drop the +pin: + +```sh +asdf uninstall nodejs +``` + +Then delete the `nodejs ...` line from `./.tool-versions` and +`~/.tool-versions` (or `asdf unset nodejs` on asdf 0.16+), and run +`asdf reshim nodejs` if a stale shim stays. Official reference: +[versions](https://asdf-vm.com/manage/versions.html) (uninstall and +shims on one page). + +Or link one real install (road B); never the shims directory: + +```sh +asdf list nodejs +asdf where nodejs 22.13.0 +vfox link nodejs@asdf-22 ~/.asdf/installs/nodejs/22.13.0 +vfox use nodejs@asdf-22 +``` + +## nvm + +nvm (nvm-sh) keeps versions in `~/.nvm/versions/node/v/`, e.g. +`~/.nvm/versions/node/v22.14.0/bin/node`, and `nvm use` prepends that +`bin/` to `PATH`, so its `node` wins. Check with `nvm which current`. + +To hand the current shell to vfox without deleting anything (road A), +run: + +```sh +nvm deactivate +``` + +The installs stay for a later `nvm use`. `nvm unload` is stronger: it +also unloads the `nvm` function itself, so prefer `deactivate` for a +handoff. Official reference: +[restoring PATH](https://github.com/nvm-sh/nvm#restoring-path). + +Or link one installed version (road B): + +```sh +nvm which 22 +vfox link nodejs@nvm-22 ~/.nvm/versions/node/v22.14.0 +vfox use nodejs@nvm-22 +``` + +## Homebrew + +Homebrew links `node` into its prefix: `/opt/homebrew/bin/node` on Apple +silicon, `/usr/local/bin/node` on Intel Macs, +`/home/linuxbrew/.linuxbrew/bin/node` on Linux. Whichever `node` is first +in `PATH` wins (`which -a node`). + +To let vfox own `node` while keeping the keg around (road A), run: + +```sh +brew unlink node +``` + +`brew link node` brings it back. To remove it fully, `brew uninstall +node`. The Intel prefix `/usr/local/bin/node` does not contain +`homebrew`, so there vfox warns without the Homebrew hint; the fix is +the same. Official reference: +[manpage](https://docs.brew.sh/Manpage) (`unlink`, `uninstall`). + +Or link the Cellar directory (road B); a later `brew upgrade` replaces +it, so the link can dangle and need relinking: + +```sh +ls $(brew --cellar node) +vfox link nodejs@brew-24 /opt/homebrew/Cellar/node/24.5.0 +vfox use nodejs@brew-24 +``` + +## winget and the official installer + +winget runs the official MSI, which installs machine-wide to +`C:\Program Files\nodejs\`, e.g. `C:\Program Files\nodejs\node.exe`, +and appends that directory to the system `PATH`. The LTS line uses the +`OpenJS.NodeJS.LTS` package id instead of `OpenJS.NodeJS`. + +To let vfox own `node` (road A), uninstall it (Add/Remove Programs runs +the same uninstaller): + +```powershell +winget list --name Node.js +winget uninstall -e --id OpenJS.NodeJS +``` + +If the LTS line is installed, replace the id with +`OpenJS.NodeJS.LTS`. Open a new terminal afterwards so the stale `PATH` +entry is gone. Official download page: +[nodejs.org](https://nodejs.org/en/download). + +Or link the directory (road B); an MSI upgrade replaces the files +underneath, which vfox only reports: + +```powershell +vfox link nodejs@system "C:\Program Files\nodejs" +vfox use nodejs@system +``` diff --git a/docs/pnpm.md b/docs/pnpm.md index 5000cf0..c27ef05 100644 --- a/docs/pnpm.md +++ b/docs/pnpm.md @@ -39,13 +39,27 @@ Observed with pnpm 12.14.0 and vfox 1.0.12 in bash on Ubuntu. pnpm's `globalShims` setting and `PNPM_SHIM_BYPASS=1` only turn off the project lookup; `node` still runs pnpm's global Node.js, not vfox's. -`vfox use nodejs@` prints a note when a `node` sits ahead of vfox -this way, whichever tool put it there: +`vfox use nodejs@` warns when a `node` sits ahead of vfox +this way, whichever tool put it there. Unreleased: release 0.5.4 prints +the `Note:` below from the plugin; on vfox 1.1.0 and later the warning +comes from vfox itself with the plugin's hint. + +On vfox 1.1.0 and later: + +```text +node runs /home/user/.local/share/pnpm/bin/node instead of nodejs@24.21.0: its directory comes before vfox in PATH. +pnpm installed its own node. Run `pnpm remove -g node` so vfox's node runs. See https://github.com/version-fox/vfox-nodejs/blob/main/docs/pnpm.md +``` + +On older vfox, the plugin prints: ```text Note: /home/user/.local/share/pnpm/bin/node comes before vfox in PATH, so `node` will not run nodejs@24.21.0. See https://github.com/version-fox/vfox-nodejs/blob/main/docs/pnpm.md ``` +For the same problem from mise, asdf, nvm, Homebrew or winget, see +[managers.md](managers.md). + ## Keeping vfox in charge of `node` Remove the Node.js that pnpm installed globally. pnpm keeps working, since diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 4917091..5ab9618 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -132,7 +132,16 @@ These are not errors. | `Note: Node.js versions below 16 do not support Apple Silicon (arm64), using x64 architecture with Rosetta 2` | macOS arm64 and a release before 16.0.0, see [platforms.md](platforms.md#macos) | | `Note: musl libc detected, installed node-v22.23.3-linux-x64-musl.tar.gz. See ...` | the plugin took the musl package, see [musl.md](musl.md) | | `Note: node-v18.20.8-linux-x64-musl.tar.gz is not listed in https://nodejs.org/dist/v18.20.8/, using unofficial musl build` | nodejs.org has no such musl package and the plugin takes it from unofficial-builds.nodejs.org | -| ``Note: /home/me/.local/share/pnpm/bin/node comes before vfox in PATH, so `node` will not run nodejs@24.21.0. See ...`` | another `node` shadows vfox's, see [pnpm.md](pnpm.md) | +| ``Note: /home/me/.local/share/pnpm/bin/node comes before vfox in PATH, so `node` will not run nodejs@24.21.0. See ...`` | another `node` shadows vfox's on vfox before 1.1.0, see [pnpm.md](pnpm.md) for pnpm and [managers.md](managers.md) for mise, asdf, nvm, Homebrew and winget. Unreleased: release 0.5.4 prints this; on vfox 1.1.0 and later vfox warns itself (`node runs ... instead of nodejs@...`) and appends the plugin's hint | + +### Warnings from vfox itself + +Unreleased: vfox 1.1.0 and later reports a shadowing `node` with the +plugin's hint; release 0.5.4 prints the `Note:` above from the plugin. + +| Warning | Meaning | +| --- | --- | +| ``node runs /home/me/.local/share/pnpm/bin/node instead of nodejs@24.21.0: its directory comes before vfox in PATH. pnpm installed its own node. Run `pnpm remove -g node` so vfox's node runs. See ...`` | another `node` shadows vfox's, see [pnpm.md](pnpm.md) for pnpm and [managers.md](managers.md) for mise, asdf, nvm, Homebrew and winget | ### Errors from Node.js itself diff --git a/docs/zh-CN/changes.md b/docs/zh-CN/changes.md index 67e9ea6..454487c 100644 --- a/docs/zh-CN/changes.md +++ b/docs/zh-CN/changes.md @@ -2,7 +2,11 @@ [中文](changes.md) | [English](../changes.md) -按发布版本列出用户能感受到的变化。GitHub 的 [Release 页面](https://github.com/version-fox/vfox-nodejs/releases)列出了对应的 PR。 +按发布版本列出用户能感受到的变化。GitHub 的 [Release 页面](https://github.com/version-fox/vfox-nodejs/releases)列出了对应的 PR。写这篇文档时最新的发布版是 0.5.4,`main` 比它多出“未发布”下面的所有内容。想现在就用上这些变化,见 [source-build.md](source-build.md)。 + +## 未发布 + +- 在 vfox 1.1.0 及以后的版本里,遮蔽 vfox 的 `node` 由 vfox 自己报告,并附带插件的提示,不再由插件打印 `Note:`。0.5.4 会在所有 vfox 上从 `PreUse` 打印 `Note: comes before vfox in PATH, ...`,所以在新 vfox 上,同一个遮蔽会同时打印核心警告和插件的提示。插件现在为 pnpm、mise、asdf、nvm、Homebrew 和 winget 声明 `PLUGIN.shadowHints`(各自的交接命令,以及指向 `docs/pnpm.md` 或新建 `docs/managers.md` 的链接),只在 1.1.0 之前的 vfox 上保留自己的 `Note:`,老 vfox 会忽略这个新的元数据字段。见 [pnpm.md](pnpm.md)、[managers.md](managers.md)和 [troubleshooting.md](troubleshooting.md)。 ## 0.5.4(2026-11-04) diff --git a/docs/zh-CN/development.md b/docs/zh-CN/development.md index f31e728..7ed4031 100644 --- a/docs/zh-CN/development.md +++ b/docs/zh-CN/development.md @@ -5,14 +5,14 @@ ## 目录结构 ```text -metadata.lua 插件名、版本,以及把 .node-version 和 .nvmrc 注册为兼容版本文件 +metadata.lua 插件名、版本、把 .node-version 和 .nvmrc 注册为兼容版本文件,以及遮蔽 node 时的 shadowHints hooks/available.lua vfox search:版本索引 hooks/pre_install.lua vfox install:选择包,以及它的 URL 和校验和 hooks/post_install.lua musl:node 无法运行时报错,比如缺少 libstdc++ 或 libatomic hooks/env_keys.lua PATH:Linux 和 macOS 上是 bin,Windows 上是安装目录 -hooks/pre_use.lua vfox use:把 24 或 24.21 映射到已安装的版本,vfox 的 node 被遮蔽时给出提示 +hooks/pre_use.lua vfox use:把 24 或 24.21 映射到已安装的版本(只在 vfox 1.1.0 之前,vfox 的 node 被遮蔽时给出提示) hooks/parse_legacy_file.lua .nvmrc 和 .node-version -lib/util.lua 镜像 URL 与下载请求头、musl 检测与缺失的库、运行命令、版本比较与匹配、简写、PATH 探测、插件学到的 LTS 版本线 +lib/util.lua 镜像 URL 与下载请求头、musl 检测与缺失的库、运行命令、版本比较与匹配、简写、PATH 探测、vfox 版本检查、插件学到的 LTS 版本线 .lts-cache 不在仓库里:插件学到的 LTS 版本线,写在插件旁边 tests/hooks_test.lua 离线的钩子测试 tests/e2e/ 容器测试,见 e2e.md @@ -113,7 +113,7 @@ line lithium 26 go run github.com/yuin/gopher-lua/cmd/glua@v1.1.1 tests/hooks_test.lua ``` -请在仓库根目录运行。`tests/hooks_test.lua` 自己模拟了 `http` 和 `json`(通过 `package.preload`),为每个用例设置 `RUNTIME`,请求的应答来自 `state.response`、`state.decoded` 和 `state.responder`。一个用例写作 `test("name", function() ... end)`,调用它会重置状态和 `available_result`。测试覆盖 `Available` 及其失败情况、各平台的包名和校验和请求失败时的错误、musl 与 `PostInstall`(通过 `with_util` 模拟了 `util.capture`、`util.file_exists` 和 `util.has_libstdcxx`)、各策略下的 `ParseLegacyFile`(包括 UTF-16 和带字节顺序标记的文件,以及删除 `env-state.json`,其中 `os.getenv` 和 `os.remove` 是模拟的)、插件如何学到 LTS 版本线(文件是临时的,每个用例开始时都有一个什么版本线也不认识的新文件)、有 `node` 遮蔽 vfox 时的 `PreUse`,以及 `lib/util.lua` 里的辅助函数。改动需要的用例就加在那里。 +请在仓库根目录运行。`tests/hooks_test.lua` 自己模拟了 `http` 和 `json`(通过 `package.preload`),为每个用例设置 `RUNTIME`,请求的应答来自 `state.response`、`state.decoded` 和 `state.responder`。一个用例写作 `test("name", function() ... end)`,调用它会重置状态和 `available_result`。测试覆盖 `Available` 及其失败情况、各平台的包名和校验和请求失败时的错误、musl 与 `PostInstall`(通过 `with_util` 模拟了 `util.capture`、`util.file_exists` 和 `util.has_libstdcxx`)、各策略下的 `ParseLegacyFile`(包括 UTF-16 和带字节顺序标记的文件,以及删除 `env-state.json`,其中 `os.getenv` 和 `os.remove` 是模拟的)、插件如何学到 LTS 版本线(文件是临时的,每个用例开始时都有一个什么版本线也不认识的新文件)、新老 vfox 下有无遮蔽 `node` 时的 `PreUse`(`RUNTIME.version` 在 1.1.0 之前和之后)、`metadata.lua` 里的 `PLUGIN.shadowHints`,以及 `lib/util.lua` 里的辅助函数。改动需要的用例就加在那里。 gopher-lua 1.1.1 会丢掉闭包对 `local x = nil` 的赋值:外层作用域读到的仍然是 `nil`。要在 `with_util` 或 `pcall` 内部收集值的测试,必须把值放在表的字段里。它还有一个毛病:`local file = path ~= nil and io.open(path, "r") or nil` 之后的 `if file == nil` 会以 Go 的“nil pointer dereference”结束,所以要分成两条语句,先判断 `path`,再打开文件。新代码要用 `glua` 跑一遍,不能只靠 LuaLS。 diff --git a/docs/zh-CN/managers.md b/docs/zh-CN/managers.md new file mode 100644 index 0000000..c5ba40b --- /dev/null +++ b/docs/zh-CN/managers.md @@ -0,0 +1,169 @@ +# 其他 Node.js 管理器 + +[中文](managers.md) | [English](../managers.md) + +pnpm 单独有一页([pnpm.md](pnpm.md)):`pnpm runtime set node -g` 会在 +`$PNPM_HOME/bin` 里放进第二个 `node`。这一页处理同样的问题,只是换成 +vfox 旁边常见的其他管理器:mise、asdf、nvm、Homebrew,以及 winget +(官方安装程序把 `node` 装在和 winget 同一个地方)。 + +现象总是一样的:`vfox use nodejs@` 打印 `Now using`, +`vfox current nodejs` 也一致,但 `node --version` 打印的是另一个管理器 +的版本,因为它的 `node` 排在 `PATH` 里 vfox 的前面(`type -a node` +把它列在最前)。在 vfox 1.1.0 及以后的版本里,vfox 自己给出警告,并 +附带插件能认出来的那个管理器的提示;在更早的 vfox 里,插件打印自己的 +`Note:`。未发布:0.5.4 会由插件打印 `Note:`;在 vfox 1.1.0 及以后的 +版本里,这条警告由 vfox 自己打印,并附带插件的提示。 + +一个 Shell 里最多让一个管理器管 `node`。有两条路。路 A 是下面的默认 +做法,把 `PATH` 交给 vfox:对方的 `node` 不再遮挡,vfox 安装并选用 +自己的版本。路 B 是把对方的真实安装目录原地注册给 vfox: + +```sh +vfox link nodejs@ +vfox use nodejs@ +``` + +`` 是插件要的 SDK 目录:Linux 和 macOS 上是装着 `bin/`(里面有 +`bin/node`)的那个目录,Windows 上是直接装着 `node.exe` 的目录。 +`vfox link` 会检查,不像就拒绝。链真实安装,不要链 shim 目录 +(`mise/shims`、`.asdf/shims`、`$PNPM_HOME/bin`):shim 是按目录切换 +版本的代理,pnpm 的 shim 还不带 npm,链过去选不中稳定版本。链过去的 +版本还是人家管的:对方升级或清理会动 vfox 底下的文件(`brew upgrade` +换掉 Cellar 目录,mise/asdf 清理安装),vfox 只会提示, +`vfox install --reinstall` 对它无效,`vfox uninstall` 只删链接。 +`` 按写下的用(`24-system`、`brew-24`、`nvm-22`),只能用字母、 +数字、`.`、`_`、`+`、`-`,不能叫 `latest`。nodejs 插件没有 `Detect` +钩子,`vfox link --detect nodejs` 找不到东西,目录要自己传。 + +## mise + +mise 把 shim 放在 `~/.local/share/mise/shims/`(Windows 是 +`%LOCALAPPDATA%\mise\shims\`,比如 `...\mise\shims\node.exe`),真正的 +安装在 `~/.local/share/mise/installs/node//`。`mise activate` +把 shim 目录放到 `PATH` 最前面,所以它的 `node` 会胜出。 + +想让 vfox 接管 `node`,先去掉配置里的请求,再删掉已安装的版本: + +```sh +mise unuse -g node +mise uninstall --all node +``` + +在项目目录里执行 `mise unuse node`,改的是该项目的配置,而不是全局 +配置。官方文档:[uninstall](https://mise.jdx.dev/cli/uninstall.html) +和 [unuse](https://mise.jdx.dev/cli/unuse.html)。 + +也可以链一个真实安装(路 B),不要链 shim 目录: + +```sh +mise ls node +vfox link nodejs@mise-22 ~/.local/share/mise/installs/node/22.0.0 +vfox use nodejs@mise-22 +``` + +## asdf + +asdf 把 shim 放在 `~/.asdf/shims/`(设置了 `$ASDF_DATA_DIR` 时是 +`$ASDF_DATA_DIR/shims`),比如 `~/.asdf/shims/node`,真正的安装在 +`~/.asdf/installs/nodejs//`。shim 目录被加到 `PATH` 最前面, +所以它的 `node` 会胜出。 + +想让 vfox 接管 `node`,先卸载对应版本,再去掉版本固定: + +```sh +asdf uninstall nodejs +``` + +然后删掉 `./.tool-versions` 和 `~/.tool-versions` 里的 `nodejs ...` +那一行(asdf 0.16 及以后可以用 `asdf unset nodejs`),残留的 shim 用 +`asdf reshim nodejs` 清掉。官方文档: +[versions](https://asdf-vm.com/manage/versions.html)(卸载和 shim 在 +同一页)。 + +也可以链一个真实安装(路 B),不要链 shim 目录: + +```sh +asdf list nodejs +asdf where nodejs 22.13.0 +vfox link nodejs@asdf-22 ~/.asdf/installs/nodejs/22.13.0 +vfox use nodejs@asdf-22 +``` + +## nvm + +nvm(nvm-sh)把版本放在 `~/.nvm/versions/node/v/`,比如 +`~/.nvm/versions/node/v22.14.0/bin/node`,`nvm use` 会把那个 `bin/` +放到 `PATH` 最前面,所以它的 `node` 会胜出。用 `nvm which current` +可以确认。 + +想在当前 Shell 里把 `node` 交给 vfox,同时不删任何已安装版本,执行: + +```sh +nvm deactivate +``` + +安装都还在,以后还能 `nvm use` 切回去。`nvm unload` 更重:它连 `nvm` +命令本身一起卸载,交接请用 `deactivate`。官方文档: +[restoring PATH](https://github.com/nvm-sh/nvm#restoring-path)。 + +也可以链一个已安装版本(路 B): + +```sh +nvm which 22 +vfox link nodejs@nvm-22 ~/.nvm/versions/node/v22.14.0 +vfox use nodejs@nvm-22 +``` + +## Homebrew + +Homebrew 把 `node` 链接到它的 prefix 下:Apple silicon 是 +`/opt/homebrew/bin/node`,Intel Mac 是 `/usr/local/bin/node`,Linuxbrew +是 `/home/linuxbrew/.linuxbrew/bin/node`。`PATH` 里排在最前面的那个 +`node` 会真正运行(`which -a node`)。 + +想让 vfox 接管 `node`,同时保留已下载的包,执行: + +```sh +brew unlink node +``` + +`brew link node` 可以恢复。彻底删掉用 `brew uninstall node`。Intel 的 +`/usr/local/bin/node` 里没有 `homebrew` 字样,那里 vfox 只会给出通用 +警告而没有 Homebrew 提示,处理办法是一样的。官方文档: +[manpage](https://docs.brew.sh/Manpage)(`unlink`、`uninstall`)。 + +也可以链 Cellar 目录(路 B),但 `brew upgrade` 会换掉它,链接可能悬 +空,到时重链: + +```sh +ls $(brew --cellar node) +vfox link nodejs@brew-24 /opt/homebrew/Cellar/node/24.5.0 +vfox use nodejs@brew-24 +``` + +## winget 和官方安装程序 + +winget 实际运行的是官方 MSI,整机安装到 `C:\Program Files\nodejs\`, +比如 `C:\Program Files\nodejs\node.exe`,并把这个目录追加到系统 +`PATH`。LTS 版本线用的包 id 是 `OpenJS.NodeJS.LTS`,而不是 +`OpenJS.NodeJS`。 + +想让 vfox 接管 `node`,先卸载它(“应用和功能”里卸载走的是同一个卸载 +程序): + +```powershell +winget list --name Node.js +winget uninstall -e --id OpenJS.NodeJS +``` + +装的是 LTS 就把 id 换成 `OpenJS.NodeJS.LTS`。之后重开一个终端,旧的 +`PATH` 条目才会消失。官方下载页: +[nodejs.org](https://nodejs.org/en/download)。 + +也可以链这个目录(路 B),MSI 升级会换掉底下的文件,vfox 只会提示: + +```powershell +vfox link nodejs@system "C:\Program Files\nodejs" +vfox use nodejs@system +``` diff --git a/docs/zh-CN/pnpm.md b/docs/zh-CN/pnpm.md index 69920f3..7f4c9ea 100644 --- a/docs/zh-CN/pnpm.md +++ b/docs/zh-CN/pnpm.md @@ -19,12 +19,24 @@ pnpm 作为包管理器使用(比如通过 `npm install -g pnpm` 安装)时 2. 每次出现提示符时,`vfox env` 都会把 `PATH` 里排在它自己的第一个路径之前的条目,保留在所有 SDK 路径的前面,这样已经激活的 Python 虚拟环境才能保持优先。`$PNPM_HOME/bin` 正是这样一个条目。 3. 那个 `node` 是 pnpm 的 shim:它运行 pnpm 的全局 Node.js,或者当前项目通过 `devEngines.runtime`、`engines.runtime`、`.node-version` 或 `.nvmrc` 固定的版本(见[项目感知的全局 bin](https://pnpm.io/global-packages#project-aware-global-bins))。pnpm 的 `globalShims` 设置和 `PNPM_SHIM_BYPASS=1` 只能关掉按项目查找;`node` 运行的仍然是 pnpm 的全局 Node.js,而不是 vfox 的。 -不管是哪个工具放的,只要有 `node` 排在 vfox 前面,`vfox use nodejs@` 就会打印一条提示: +不管是哪个工具放的,只要有 `node` 排在 vfox 前面,`vfox use nodejs@` 就会给出警告。未发布:0.5.4 会由插件打印下面的 `Note:`;在 vfox 1.1.0 及以后的版本里,这条警告由 vfox 自己打印,并附带插件的提示。 + +在 vfox 1.1.0 及以后的版本里: + +```text +node runs /home/user/.local/share/pnpm/bin/node instead of nodejs@24.21.0: its directory comes before vfox in PATH. +pnpm installed its own node. Run `pnpm remove -g node` so vfox's node runs. See https://github.com/version-fox/vfox-nodejs/blob/main/docs/pnpm.md +``` + +在更早的 vfox 里,插件会打印: ```text Note: /home/user/.local/share/pnpm/bin/node comes before vfox in PATH, so `node` will not run nodejs@24.21.0. See https://github.com/version-fox/vfox-nodejs/blob/main/docs/pnpm.md ``` +同样的问题如果来自 mise、asdf、nvm、Homebrew 或 winget,见 +[managers.md](managers.md)。 + ## 让 vfox 继续管理 `node` 卸掉 pnpm 全局安装的 Node.js。pnpm 照常可用,因为 `pnpm setup` 已经把它复制到了 `$PNPM_HOME`: diff --git a/docs/zh-CN/troubleshooting.md b/docs/zh-CN/troubleshooting.md index 6243a03..09fe8dd 100644 --- a/docs/zh-CN/troubleshooting.md +++ b/docs/zh-CN/troubleshooting.md @@ -73,7 +73,15 @@ vfox 会打印 `Verifying checksum ...`,拿下载的文件和这个校验 | `Note: Node.js versions below 16 do not support Apple Silicon (arm64), using x64 architecture with Rosetta 2` | 在 macOS arm64 上安装早于 16.0.0 的版本,见 [platforms.md](platforms.md#macos) | | `Note: musl libc detected, installed node-v22.23.3-linux-x64-musl.tar.gz. See ...` | 插件安装的是 musl 包,见 [musl.md](musl.md) | | `Note: node-v18.20.8-linux-x64-musl.tar.gz is not listed in https://nodejs.org/dist/v18.20.8/, using unofficial musl build` | nodejs.org 没有这个 musl 包,插件改从 unofficial-builds.nodejs.org 获取 | -| ``Note: /home/me/.local/share/pnpm/bin/node comes before vfox in PATH, so `node` will not run nodejs@24.21.0. See ...`` | 另一个 `node` 遮蔽了 vfox 的,见 [pnpm.md](pnpm.md) | +| ``Note: /home/me/.local/share/pnpm/bin/node comes before vfox in PATH, so `node` will not run nodejs@24.21.0. See ...`` | 在 vfox 1.1.0 之前的版本里,另一个 `node` 遮蔽了 vfox 的,pnpm 见 [pnpm.md](pnpm.md),mise、asdf、nvm、Homebrew 和 winget 见 [managers.md](managers.md)。未发布:0.5.4 会打印这条;在 vfox 1.1.0 及以后的版本里,vfox 自己给出警告(`node runs ... instead of nodejs@...`),并附带插件的提示 | + +### vfox 自己给出的警告 + +未发布:在 vfox 1.1.0 及以后的版本里,遮蔽 vfox 的 `node` 由 vfox 报告,并附带插件的提示;0.5.4 会由插件打印上面的 `Note:`。 + +| 警告 | 含义 | +| --- | --- | +| ``node runs /home/me/.local/share/pnpm/bin/node instead of nodejs@24.21.0: its directory comes before vfox in PATH. pnpm installed its own node. Run `pnpm remove -g node` so vfox's node runs. See ...`` | 另一个 `node` 遮蔽了 vfox 的,pnpm 见 [pnpm.md](pnpm.md),mise、asdf、nvm、Homebrew 和 winget 见 [managers.md](managers.md) | ### Node.js 自己报的错 diff --git a/hooks/pre_use.lua b/hooks/pre_use.lua index 6f8dc12..92a8a21 100644 --- a/hooks/pre_use.lua +++ b/hooks/pre_use.lua @@ -26,19 +26,24 @@ function PLUGIN:PreUse(ctx) version = shorthands[version] end - -- vfox reports the switch even when another `node` keeps winning PATH - -- lookup, so say so here instead of leaving `node --version` to - -- contradict it. - local shadowing = util.shadowing_node() - if version ~= nil and shadowing ~= nil then - print( - "Note: " - .. shadowing - .. " comes before vfox in PATH, so `node` will not run nodejs@" - .. version - .. ". See " - .. util.PnpmDocUrl - ) + -- vfox before 1.1.0 has no PLUGIN.shadowHints, so say here that another + -- `node` keeps winning PATH lookup instead of leaving `node --version` + -- to contradict the switch. vfox 1.1.0 and later warns itself and + -- appends the plugin's shadowHints, so stay quiet there to avoid a + -- duplicate note for the same problem. + -- See https://github.com/version-fox/vfox/issues/737 + if not util.supports_shadow_hints() then + local shadowing = util.shadowing_node() + if version ~= nil and shadowing ~= nil then + print( + "Note: " + .. shadowing + .. " comes before vfox in PATH, so `node` will not run nodejs@" + .. version + .. ". See " + .. util.PnpmDocUrl + ) + end end --- return the version information diff --git a/lib/util.lua b/lib/util.lua index a2071bc..4d67e79 100644 --- a/lib/util.lua +++ b/lib/util.lua @@ -526,6 +526,45 @@ function UTIL.refresh_lts_cache(plugin) pcall(plugin.Available, plugin, {}) end +--- Whether the running vfox understands PLUGIN.shadowHints (v1.1.0 and later). +--- Older vfox ignores the metadata field, so PreUse keeps printing its own +--- Note there. Unknown or empty RUNTIME.version counts as old. +--- See https://github.com/version-fox/vfox/issues/737 +--- @return boolean +function UTIL.supports_shadow_hints() + if RUNTIME == nil or RUNTIME.version == nil or RUNTIME.version == "" then + return false + end + local cleaned = tostring(RUNTIME.version):gsub("^v", ""):match("^[^%-%+]+") + if cleaned == nil then + return false + end + local parts = {} + for part in string.gmatch(cleaned .. ".", "([^.]*)%.") do + -- Drop an empty segment such as the trailing one of "1.1."; anything + -- else must be a number, otherwise the version is unreadable. + if part == "" then + return false + end + if not part:match("^%d+$") then + return false + end + table.insert(parts, tonumber(part)) + end + local want = { 1, 1, 0 } + for i = 1, math.max(#parts, #want) do + local got = parts[i] or 0 + local need = want[i] or 0 + if got > need then + return true + end + if got < need then + return false + end + end + return true +end + --- Whether a PATH entry belongs to vfox: like vfox's own check, one of its --- segments is `.vfox` (or the legacy `.version-fox`). local function is_vfox_path(dir) @@ -543,6 +582,9 @@ end --- `pnpm runtime set node -g` writes to `$PNPM_HOME/bin`, shadows the --- version `vfox use` selects. With no vfox path on PATH yet, vfox puts its --- paths first and nothing shadows them. +--- Legacy path for vfox before 1.1.0: that vfox has no PLUGIN.shadowHints, +--- so PreUse prints the Note itself. vfox 1.1.0 and later warns itself and +--- appends the plugin's shadowHints, and PreUse stays quiet. --- See https://github.com/version-fox/vfox-nodejs/blob/main/docs/pnpm.md --- @return string|nil path of the shadowing executable function UTIL.shadowing_node() diff --git a/metadata.lua b/metadata.lua index 4989143..ede0b92 100644 --- a/metadata.lua +++ b/metadata.lua @@ -39,3 +39,40 @@ PLUGIN.legacyFilenames = { ".node-version", ".nvmrc", } + +--- Hints for a command of this SDK shadowed by another executable in PATH. +--- vfox 1.1.0 and later prints these with its own shadowing warning; older +--- vfox ignores this field and the plugin prints its own Note from PreUse. +--- See https://github.com/version-fox/vfox/blob/main/docs/plugins/create/howto.md#shadowhints +PLUGIN.shadowHints = { + { + match = "pnpm", + message = "pnpm installed its own node. Run `pnpm remove -g node` so vfox's node runs.", + url = "https://github.com/version-fox/vfox-nodejs/blob/main/docs/pnpm.md", + }, + { + match = "mise", + message = "mise installed its own node. Run `mise unuse -g node` and `mise uninstall --all node` so vfox's node runs.", + url = "https://github.com/version-fox/vfox-nodejs/blob/main/docs/managers.md#mise", + }, + { + match = "asdf", + message = "asdf installed its own node. Run `asdf uninstall nodejs ` and drop it from `.tool-versions` so vfox's node runs.", + url = "https://github.com/version-fox/vfox-nodejs/blob/main/docs/managers.md#asdf", + }, + { + match = "nvm", + message = "nvm installed its own node. Run `nvm deactivate` in this shell so vfox's node runs.", + url = "https://github.com/version-fox/vfox-nodejs/blob/main/docs/managers.md#nvm", + }, + { + match = "homebrew", + message = "Homebrew installed its own node. Run `brew unlink node` (or `brew uninstall node`) so vfox's node runs.", + url = "https://github.com/version-fox/vfox-nodejs/blob/main/docs/managers.md#homebrew", + }, + { + match = "nodejs", + message = "A Node.js installed outside vfox (winget or the official installer) comes before vfox in PATH. Run `winget uninstall -e --id OpenJS.NodeJS` so vfox's node runs.", + url = "https://github.com/version-fox/vfox-nodejs/blob/main/docs/managers.md#winget-and-the-official-installer", + }, +} diff --git a/tests/hooks_test.lua b/tests/hooks_test.lua index 4f1d6e2..28f4810 100644 --- a/tests/hooks_test.lua +++ b/tests/hooks_test.lua @@ -936,8 +936,8 @@ local function with_path(path, existing, fn) return printed end local installed = { ["24.21.0"] = { version = "24.21.0" }, ["22.23.3"] = { version = "22.23.3" } } -test("pre_use notes a node ahead of vfox", function() - RUNTIME = { osType = "linux", archType = "amd64" } +test("pre_use notes a node ahead of vfox on old vfox", function() + RUNTIME = { osType = "linux", archType = "amd64", version = "1.0.13" } local printed = with_path("/home/u/.local/share/pnpm/bin:/home/u/.vfox/sdks/nodejs/bin:/usr/bin", { ["/home/u/.local/share/pnpm/bin/node"] = true, ["/home/u/.vfox/sdks/nodejs/bin/node"] = true, @@ -949,6 +949,30 @@ test("pre_use notes a node ahead of vfox", function() assert(printed[1]:find("nodejs@24.21.0", 1, true), printed[1]) assert(printed[1]:find("docs/pnpm.md", 1, true), printed[1]) end) +test("pre_use notes without RUNTIME.version (old vfox)", function() + RUNTIME = { osType = "linux", archType = "amd64" } + local printed = with_path("/home/u/.local/share/pnpm/bin:/home/u/.vfox/sdks/nodejs/bin:/usr/bin", { + ["/home/u/.local/share/pnpm/bin/node"] = true, + ["/home/u/.vfox/sdks/nodejs/bin/node"] = true, + }, function() + assert(PLUGIN:PreUse({ version = "24", installedSdks = installed }).version == "24.21.0") + end) + assert(#printed == 1, "expected one note without RUNTIME.version") +end) +test("pre_use stays quiet with a shadowing node on vfox 1.1.0+", function() + -- vfox 1.1.0 warns itself and appends PLUGIN.shadowHints; the plugin's + -- own Note would duplicate it. See https://github.com/version-fox/vfox/issues/737 + for _, version in ipairs({ "1.1.0", "v1.1.0", "1.2.0", "2.0.0" }) do + RUNTIME = { osType = "linux", archType = "amd64", version = version } + local printed = with_path("/home/u/.local/share/pnpm/bin:/home/u/.vfox/sdks/nodejs/bin:/usr/bin", { + ["/home/u/.local/share/pnpm/bin/node"] = true, + ["/home/u/.vfox/sdks/nodejs/bin/node"] = true, + }, function() + assert(PLUGIN:PreUse({ version = "24", installedSdks = installed }).version == "24.21.0") + end) + assert(#printed == 0, version .. " should stay quiet, got " .. tostring(printed[1])) + end +end) test("pre_use stays quiet when vfox comes first", function() RUNTIME = { osType = "linux", archType = "amd64" } local printed = with_path("/home/u/.vfox/sdks/nodejs/bin:/home/u/.local/share/pnpm/bin", { @@ -982,6 +1006,54 @@ test("shadowing_node on windows", function() assert(util.shadowing_node() == pnpm .. "\\node.exe", tostring(util.shadowing_node())) end) end) +test("supports_shadow_hints follows RUNTIME.version", function() + -- vfox 1.1.0 added PLUGIN.shadowHints. See https://github.com/version-fox/vfox/issues/737 + local old = { "0.6.0", "1.0.0", "1.0.13", "1.0.99", "0.99.99", "1.0.0", "" } + for _, version in ipairs(old) do + RUNTIME = { osType = "linux", archType = "amd64", version = version } + assert(util.supports_shadow_hints() == false, version .. " should be old") + end + RUNTIME = nil + assert(util.supports_shadow_hints() == false, "nil RUNTIME should be old") + RUNTIME = { osType = "linux", archType = "amd64" } + assert(util.supports_shadow_hints() == false, "missing version should be old") + local new = { "1.1.0", "v1.1.0", "1.1.1", "1.2.0", "2.0.0", "1.10.0" } + for _, version in ipairs(new) do + RUNTIME = { osType = "linux", archType = "amd64", version = version } + assert(util.supports_shadow_hints() == true, version .. " should be new") + end +end) +test("metadata declares shadowHints for known managers", function() + local saved_plugin = PLUGIN + PLUGIN = {} + dofile("metadata.lua") + local hints = PLUGIN.shadowHints + assert(hints ~= nil and #hints >= 6, "shadowHints must cover pnpm, mise, asdf, nvm, homebrew and winget") + local want = { + pnpm = { command = "pnpm remove %-g node", url = "docs/pnpm%.md" }, + mise = { command = "mise unuse %-g node", url = "docs/managers%.md#mise" }, + asdf = { command = "asdf uninstall nodejs", url = "docs/managers%.md#asdf" }, + nvm = { command = "nvm deactivate", url = "docs/managers%.md#nvm" }, + homebrew = { command = "brew unlink node", url = "docs/managers%.md#homebrew" }, + nodejs = { command = "winget uninstall", url = "docs/managers%.md#winget" }, + } + local seen = {} + for _, hint in ipairs(hints) do + assert(type(hint.match) == "string" and hint.match ~= "", "match must not be empty") + assert(type(hint.message) == "string" and hint.message ~= "", "message must not be empty") + local key = hint.match:lower() + local expected = want[key] + assert(expected ~= nil, "unexpected shadowHints match: " .. tostring(hint.match)) + assert(not seen[key], "duplicate shadowHints match: " .. key) + seen[key] = true + assert(hint.message:find(expected.command, 1) ~= nil, hint.message) + assert(hint.url ~= nil and hint.url:find(expected.url, 1) ~= nil, tostring(hint.url)) + end + for key in pairs(want) do + assert(seen[key], "shadowHints must match " .. key) + end + PLUGIN = saved_plugin +end) -- lib/util.lua helpers. test("version_prefix reads what a version file asks for", function() local accepted = { From 1bd9d1f8b13dc7146badbe18cc15375d1369334a Mon Sep 17 00:00:00 2001 From: Qiheng He Date: Tue, 10 Nov 2026 11:51:29 +0800 Subject: [PATCH 2/2] feat: add Detect hook so vfox link --detect nodejs finds existing installs Scan nvm, mise, asdf and Homebrew Cellar directories with a working node in them, plus the winget directory on Windows, and report each under the plain release number the binary prints. Shims, pnpm's global runtime and copies without a versioned home are skipped. Refs version-fox/vfox#737 --- docs/changes.md | 6 + docs/development.md | 10 +- docs/managers.md | 74 +++++++---- docs/zh-CN/changes.md | 1 + docs/zh-CN/development.md | 7 +- docs/zh-CN/managers.md | 67 ++++++---- hooks/detect.lua | 25 ++++ lib/util.lua | 178 +++++++++++++++++++++++++ tests/hooks_test.lua | 269 +++++++++++++++++++++++++++++++++++++- 9 files changed, 585 insertions(+), 52 deletions(-) create mode 100644 hooks/detect.lua diff --git a/docs/changes.md b/docs/changes.md index 607ee87..be291c3 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -21,6 +21,12 @@ ahead of it by everything under "Unreleased". only, which ignores the new metadata field. See [pnpm.md](pnpm.md), [managers.md](managers.md) and [troubleshooting.md](troubleshooting.md). +- `vfox link --detect nodejs` links the Node.js already on the machine + instead of downloading another copy: the nvm, mise, asdf and Homebrew + Cellar directories with a working `node` in them, and the winget + directory on Windows, each under the plain release number the binary + reports. Shims, pnpm's global runtime and copies without a versioned + home are skipped. See [managers.md](managers.md). ## 0.5.4 (2026-11-04) diff --git a/docs/development.md b/docs/development.md index 7addd82..5a19610 100644 --- a/docs/development.md +++ b/docs/development.md @@ -12,6 +12,7 @@ hooks/post_install.lua musl: fail when node cannot run, such as without hooks/env_keys.lua PATH: bin on Linux and macOS, the install directory on Windows hooks/pre_use.lua vfox use: map 24 or 24.21 to an installed release (and, on vfox before 1.1.0, note a shadowing node) hooks/parse_legacy_file.lua .nvmrc and .node-version +hooks/detect.lua vfox link --detect: the nvm, mise, asdf, Homebrew and winget installs on this machine lib/util.lua mirror URL and download headers, musl detection and missing libraries, running a command, version comparison and matching, shorthands, PATH probe, vfox version check, the LTS lines the plugin learned .lts-cache not in the repository: what the plugin learned about LTS lines, written next to it tests/hooks_test.lua offline hook tests @@ -37,6 +38,7 @@ loaded with `require("util")`. | `vfox use nodejs@24` | `PreUse` returns the installed release to select. `EnvKeys` provides `PATH` | | a shell hook, `cd` into a directory with `.nvmrc` | `ParseLegacyFile`, then `EnvKeys` | | `vfox uninstall nodejs@24.21.0` | none, the name has to be exact | +| `vfox link --detect nodejs` | `Detect` | A few contracts that cost time to find: @@ -237,7 +239,9 @@ with the removal of `env-state.json` (`os.getenv` and `os.remove` stubbed), what the plugin learns about LTS lines (the file is a temporary one, and every case starts with a fresh one that knows no line), `PreUse` with and without the shadowing `node` on old and new vfox (`RUNTIME.version` before and from -1.1.0), `PLUGIN.shadowHints` in `metadata.lua`, and the helpers of +1.1.0), `PLUGIN.shadowHints` in `metadata.lua`, `Detect` over stubbed +install roots, listings and `node --version` outputs (`os.getenv`, +`list_dir`, `file_exists` and `capture` stubbed), and the helpers of `lib/util.lua`. Add the case a change needs there. gopher-lua 1.1.1 loses a `local x = nil` that a closure assigns: the outer scope @@ -249,7 +253,8 @@ Check new code under `glua`, not only with LuaLS. To call one hook against the real servers, vfox 1.0.13 and later has `vfox plugin run`. It does not register the plugin, `--os` and `--arch` answer -for another platform, `--offline` rejects HTTP, and `VFOX_NODEJS_MIRROR` applies: +for another platform, `--offline` rejects HTTP, and `VFOX_NODEJS_MIRROR` applies +(`Detect` needs vfox 1.1.0 or later, which knows the hook): ```bash vfox plugin run . Available --json @@ -257,6 +262,7 @@ vfox plugin run . PreInstall --input '{"version":"24"}' --os windows --arch arm6 vfox plugin run . PreUse --input '{"version":"22","installedSdks":{"22.23.3":{"version":"22.23.3"}}}' --json vfox plugin run . ParseLegacyFile --input '{"filename":".nvmrc","filepath":"/tmp/p/.nvmrc","strategy":"latest_installed","installedVersions":["24.21.0"]}' --json vfox plugin run . EnvKeys --input '{"path":"C:\\vfox\\nodejs-24.21.0"}' --os windows --arch amd64 --json +vfox plugin run . Detect --json ``` `vfox --debug ` shows what vfox does with a hook, which is how to find diff --git a/docs/managers.md b/docs/managers.md index 2d8f6cf..cf8f290 100644 --- a/docs/managers.md +++ b/docs/managers.md @@ -17,11 +17,29 @@ recognizes; on older vfox the plugin prints its own `Note:`. Unreleased: release 0.5.4 prints the `Note:`; on vfox 1.1.0 and later the warning comes from vfox itself with the plugin's hint. -Pick at most one manager for `node` in a shell. There are two roads. -Road A, the default below, hands `PATH` to vfox without removing the -other manager itself: its `node` stops shadowing, and vfox installs and -selects its own releases. Road B keeps the files where they are and -registers one of them with vfox: +Pick at most one manager for `node` in a shell. The fastest way out is +`vfox link --detect nodejs` (needs vfox 1.1.0 or later, which has +`vfox link`): it asks the plugin for the real installs on this machine +and links what you pick. Nothing is downloaded or copied: + +```sh +vfox link --detect nodejs +vfox use nodejs@22.14.0 +``` + +It finds the nvm, mise, asdf and Homebrew Cellar directories with a +working `node` in them (including Intel's `/usr/local/Cellar/node`), +and the winget directory on Windows, each under the plain release +number the binary reports, so `vfox use 22` keeps working. A release +found twice links once, and what vfox installed already is skipped. It +never reports shims, pnpm's global runtime or copies without a +versioned home (a manual `/usr/local/bin/node`): link those by hand, +or hand `PATH` to vfox as below. + +There are two roads per manager below. Road A, the default, hands +`PATH` to vfox without removing the other manager itself: its `node` +stops shadowing, and vfox installs and selects its own releases. Road B +keeps the files where they are and registers one of them with vfox: ```sh vfox link nodejs@ @@ -38,11 +56,11 @@ a linked shim selects nothing stable. A linked version stays its owner's: the other manager may upgrade or prune it underneath vfox (`brew upgrade` replaces the Cellar directory, mise/asdf prune installs), vfox only reports that, `vfox install --reinstall` does not -apply to it, and `vfox uninstall` removes the link only. The `` -is used as written (`24-system`, `brew-24`, `nvm-22`); use letters, -digits, `.`, `_`, `+` and `-`, never `latest`. The nodejs plugin has no -`Detect` hook, so `vfox link --detect nodejs` finds nothing; pass the -directory yourself. +apply to it, and `vfox uninstall` removes the link only. Name a linked +install after the plain release number the binary reports (`22.14.0`), +so `vfox use 22` keeps selecting it; if vfox installed that release +already, just `vfox use` it instead of linking. A name may use letters, +digits, `.`, `_`, `+` and `-`, and must not be `latest`. ## mise @@ -64,12 +82,13 @@ instead of the global one. Official reference: [uninstall](https://mise.jdx.dev/cli/uninstall.html) and [unuse](https://mise.jdx.dev/cli/unuse.html). -Or link one real install (road B); never the shims directory: +Or link one real install (road B); never the shims directory. +`vfox link --detect nodejs` finds these itself: ```sh mise ls node -vfox link nodejs@mise-22 ~/.local/share/mise/installs/node/22.0.0 -vfox use nodejs@mise-22 +vfox link nodejs@22.0.0 ~/.local/share/mise/installs/node/22.0.0 +vfox use nodejs@22.0.0 ``` ## asdf @@ -92,13 +111,14 @@ Then delete the `nodejs ...` line from `./.tool-versions` and [versions](https://asdf-vm.com/manage/versions.html) (uninstall and shims on one page). -Or link one real install (road B); never the shims directory: +Or link one real install (road B); never the shims directory. +`vfox link --detect nodejs` finds these itself: ```sh asdf list nodejs asdf where nodejs 22.13.0 -vfox link nodejs@asdf-22 ~/.asdf/installs/nodejs/22.13.0 -vfox use nodejs@asdf-22 +vfox link nodejs@22.13.0 ~/.asdf/installs/nodejs/22.13.0 +vfox use nodejs@22.13.0 ``` ## nvm @@ -119,12 +139,13 @@ also unloads the `nvm` function itself, so prefer `deactivate` for a handoff. Official reference: [restoring PATH](https://github.com/nvm-sh/nvm#restoring-path). -Or link one installed version (road B): +Or link one installed version (road B). +`vfox link --detect nodejs` finds these itself: ```sh nvm which 22 -vfox link nodejs@nvm-22 ~/.nvm/versions/node/v22.14.0 -vfox use nodejs@nvm-22 +vfox link nodejs@22.14.0 ~/.nvm/versions/node/v22.14.0 +vfox use nodejs@22.14.0 ``` ## Homebrew @@ -147,12 +168,13 @@ the same. Official reference: [manpage](https://docs.brew.sh/Manpage) (`unlink`, `uninstall`). Or link the Cellar directory (road B); a later `brew upgrade` replaces -it, so the link can dangle and need relinking: +it, so the link can dangle and need relinking. +`vfox link --detect nodejs` finds these itself: ```sh ls $(brew --cellar node) -vfox link nodejs@brew-24 /opt/homebrew/Cellar/node/24.5.0 -vfox use nodejs@brew-24 +vfox link nodejs@24.5.0 /opt/homebrew/Cellar/node/24.5.0 +vfox use nodejs@24.5.0 ``` ## winget and the official installer @@ -176,9 +198,11 @@ entry is gone. Official download page: [nodejs.org](https://nodejs.org/en/download). Or link the directory (road B); an MSI upgrade replaces the files -underneath, which vfox only reports: +underneath, which vfox only reports. +`vfox link --detect nodejs` finds it itself; by hand: ```powershell -vfox link nodejs@system "C:\Program Files\nodejs" -vfox use nodejs@system +& "C:\Program Files\nodejs\node.exe" --version +vfox link nodejs@22.14.0 "C:\Program Files\nodejs" +vfox use nodejs@22.14.0 ``` diff --git a/docs/zh-CN/changes.md b/docs/zh-CN/changes.md index 454487c..2b66dc9 100644 --- a/docs/zh-CN/changes.md +++ b/docs/zh-CN/changes.md @@ -7,6 +7,7 @@ ## 未发布 - 在 vfox 1.1.0 及以后的版本里,遮蔽 vfox 的 `node` 由 vfox 自己报告,并附带插件的提示,不再由插件打印 `Note:`。0.5.4 会在所有 vfox 上从 `PreUse` 打印 `Note: comes before vfox in PATH, ...`,所以在新 vfox 上,同一个遮蔽会同时打印核心警告和插件的提示。插件现在为 pnpm、mise、asdf、nvm、Homebrew 和 winget 声明 `PLUGIN.shadowHints`(各自的交接命令,以及指向 `docs/pnpm.md` 或新建 `docs/managers.md` 的链接),只在 1.1.0 之前的 vfox 上保留自己的 `Note:`,老 vfox 会忽略这个新的元数据字段。见 [pnpm.md](pnpm.md)、[managers.md](managers.md)和 [troubleshooting.md](troubleshooting.md)。 +- `vfox link --detect nodejs` 会把本机已有的 Node.js 链进来,不用再下载一份:装着可用 `node` 的 nvm、mise、asdf 和 Homebrew Cellar 目录,Windows 上还有 winget 目录,每个都用二进制自己报出的纯版本号命名。shim、pnpm 的全局运行时和没有版本化家目录的复制会被跳过。见 [managers.md](managers.md)。 ## 0.5.4(2026-11-04) diff --git a/docs/zh-CN/development.md b/docs/zh-CN/development.md index 7ed4031..7cab38c 100644 --- a/docs/zh-CN/development.md +++ b/docs/zh-CN/development.md @@ -12,6 +12,7 @@ hooks/post_install.lua musl:node 无法运行时报错,比如缺少 hooks/env_keys.lua PATH:Linux 和 macOS 上是 bin,Windows 上是安装目录 hooks/pre_use.lua vfox use:把 24 或 24.21 映射到已安装的版本(只在 vfox 1.1.0 之前,vfox 的 node 被遮蔽时给出提示) hooks/parse_legacy_file.lua .nvmrc 和 .node-version +hooks/detect.lua vfox link --detect:本机上的 nvm、mise、asdf、Homebrew 和 winget 安装 lib/util.lua 镜像 URL 与下载请求头、musl 检测与缺失的库、运行命令、版本比较与匹配、简写、PATH 探测、vfox 版本检查、插件学到的 LTS 版本线 .lts-cache 不在仓库里:插件学到的 LTS 版本线,写在插件旁边 tests/hooks_test.lua 离线的钩子测试 @@ -32,6 +33,7 @@ vfox 加载 `metadata.lua`,命令需要时调用相应的钩子,并向钩子 | `vfox use nodejs@24` | `PreUse` 返回要选中的已安装版本。`EnvKeys` 提供 `PATH` | | 已挂载到 Shell,`cd` 进有 `.nvmrc` 的目录 | `ParseLegacyFile`,然后是 `EnvKeys` | | `vfox uninstall nodejs@24.21.0` | 无,名字必须精确 | +| `vfox link --detect nodejs` | `Detect` | 几条容易踩坑的约定: @@ -113,11 +115,11 @@ line lithium 26 go run github.com/yuin/gopher-lua/cmd/glua@v1.1.1 tests/hooks_test.lua ``` -请在仓库根目录运行。`tests/hooks_test.lua` 自己模拟了 `http` 和 `json`(通过 `package.preload`),为每个用例设置 `RUNTIME`,请求的应答来自 `state.response`、`state.decoded` 和 `state.responder`。一个用例写作 `test("name", function() ... end)`,调用它会重置状态和 `available_result`。测试覆盖 `Available` 及其失败情况、各平台的包名和校验和请求失败时的错误、musl 与 `PostInstall`(通过 `with_util` 模拟了 `util.capture`、`util.file_exists` 和 `util.has_libstdcxx`)、各策略下的 `ParseLegacyFile`(包括 UTF-16 和带字节顺序标记的文件,以及删除 `env-state.json`,其中 `os.getenv` 和 `os.remove` 是模拟的)、插件如何学到 LTS 版本线(文件是临时的,每个用例开始时都有一个什么版本线也不认识的新文件)、新老 vfox 下有无遮蔽 `node` 时的 `PreUse`(`RUNTIME.version` 在 1.1.0 之前和之后)、`metadata.lua` 里的 `PLUGIN.shadowHints`,以及 `lib/util.lua` 里的辅助函数。改动需要的用例就加在那里。 +请在仓库根目录运行。`tests/hooks_test.lua` 自己模拟了 `http` 和 `json`(通过 `package.preload`),为每个用例设置 `RUNTIME`,请求的应答来自 `state.response`、`state.decoded` 和 `state.responder`。一个用例写作 `test("name", function() ... end)`,调用它会重置状态和 `available_result`。测试覆盖 `Available` 及其失败情况、各平台的包名和校验和请求失败时的错误、musl 与 `PostInstall`(通过 `with_util` 模拟了 `util.capture`、`util.file_exists` 和 `util.has_libstdcxx`)、各策略下的 `ParseLegacyFile`(包括 UTF-16 和带字节顺序标记的文件,以及删除 `env-state.json`,其中 `os.getenv` 和 `os.remove` 是模拟的)、插件如何学到 LTS 版本线(文件是临时的,每个用例开始时都有一个什么版本线也不认识的新文件)、新老 vfox 下有无遮蔽 `node` 时的 `PreUse`(`RUNTIME.version` 在 1.1.0 之前和之后)、`metadata.lua` 里的 `PLUGIN.shadowHints`、`Detect`(安装根目录、目录列表和 `node --version` 的输出都是模拟的:`os.getenv`、`list_dir`、`file_exists` 和 `capture`),以及 `lib/util.lua` 里的辅助函数。改动需要的用例就加在那里。 gopher-lua 1.1.1 会丢掉闭包对 `local x = nil` 的赋值:外层作用域读到的仍然是 `nil`。要在 `with_util` 或 `pcall` 内部收集值的测试,必须把值放在表的字段里。它还有一个毛病:`local file = path ~= nil and io.open(path, "r") or nil` 之后的 `if file == nil` 会以 Go 的“nil pointer dereference”结束,所以要分成两条语句,先判断 `path`,再打开文件。新代码要用 `glua` 跑一遍,不能只靠 LuaLS。 -想对着真实的服务器调用单个钩子,可以用 vfox 1.0.13 及以后版本的 `vfox plugin run`。它不会注册插件,`--os` 和 `--arch` 可以模拟其他平台,`--offline` 会拒绝 HTTP 请求,`VFOX_NODEJS_MIRROR` 同样生效: +想对着真实的服务器调用单个钩子,可以用 vfox 1.0.13 及以后版本的 `vfox plugin run`。它不会注册插件,`--os` 和 `--arch` 可以模拟其他平台,`--offline` 会拒绝 HTTP 请求,`VFOX_NODEJS_MIRROR` 同样生效(`Detect` 要认得这个钩子的 vfox 1.1.0 及以后): ```bash vfox plugin run . Available --json @@ -125,6 +127,7 @@ vfox plugin run . PreInstall --input '{"version":"24"}' --os windows --arch arm6 vfox plugin run . PreUse --input '{"version":"22","installedSdks":{"22.23.3":{"version":"22.23.3"}}}' --json vfox plugin run . ParseLegacyFile --input '{"filename":".nvmrc","filepath":"/tmp/p/.nvmrc","strategy":"latest_installed","installedVersions":["24.21.0"]}' --json vfox plugin run . EnvKeys --input '{"path":"C:\\vfox\\nodejs-24.21.0"}' --os windows --arch amd64 --json +vfox plugin run . Detect --json ``` `vfox --debug ` 会显示 vfox 对钩子做了什么,vfox 吞掉的错误可以靠它找出来,比如 `ParseLegacyFile` 里的错误。 diff --git a/docs/zh-CN/managers.md b/docs/zh-CN/managers.md index c5ba40b..62aa9b1 100644 --- a/docs/zh-CN/managers.md +++ b/docs/zh-CN/managers.md @@ -15,9 +15,27 @@ vfox 旁边常见的其他管理器:mise、asdf、nvm、Homebrew,以及 wing `Note:`。未发布:0.5.4 会由插件打印 `Note:`;在 vfox 1.1.0 及以后的 版本里,这条警告由 vfox 自己打印,并附带插件的提示。 -一个 Shell 里最多让一个管理器管 `node`。有两条路。路 A 是下面的默认 -做法,把 `PATH` 交给 vfox:对方的 `node` 不再遮挡,vfox 安装并选用 -自己的版本。路 B 是把对方的真实安装目录原地注册给 vfox: +一个 Shell 里最多让一个管理器管 `node`。最快的是 +`vfox link --detect nodejs`(要带 `vfox link` 的 vfox 1.1.0 及以后): +它直接问插件要本机上真实的安装,选一个链上就行,什么也不会下载或 +复制: + +```sh +vfox link --detect nodejs +vfox use nodejs@22.14.0 +``` + +它找的是装着可用 `node` 的 nvm、mise、asdf 和 Homebrew Cellar 目录 +(包括 Intel 的 `/usr/local/Cellar/node`),Windows 上还有 winget +目录,每个都用二进制自己报出的纯版本号命名,所以 `vfox use 22` +照常用。同一个版本找到两次只链一次,vfox 装过的会跳过。shim、 +pnpm 的全局运行时和没有版本化家目录的复制(比如手装的 +`/usr/local/bin/node`)一律不报:那些自己手动链,或者按下面的办法 +把 `PATH` 交给 vfox。 + +每个管理器下面有两条路。路 A 是默认做法,把 `PATH` 交给 vfox:对方 +的 `node` 不再遮挡,vfox 安装并选用自己的版本。路 B 是把对方的真实 +安装目录原地注册给 vfox: ```sh vfox link nodejs@ @@ -31,10 +49,10 @@ vfox use nodejs@ 版本的代理,pnpm 的 shim 还不带 npm,链过去选不中稳定版本。链过去的 版本还是人家管的:对方升级或清理会动 vfox 底下的文件(`brew upgrade` 换掉 Cellar 目录,mise/asdf 清理安装),vfox 只会提示, -`vfox install --reinstall` 对它无效,`vfox uninstall` 只删链接。 -`` 按写下的用(`24-system`、`brew-24`、`nvm-22`),只能用字母、 -数字、`.`、`_`、`+`、`-`,不能叫 `latest`。nodejs 插件没有 `Detect` -钩子,`vfox link --detect nodejs` 找不到东西,目录要自己传。 +`vfox install --reinstall` 对它无效,`vfox uninstall` 只删链接。链上 +的版本用二进制报出的纯版本号命名(`22.14.0`),这样 `vfox use 22` +照常用;vfox 装过的版本就直接 `vfox use` 它,不用再链。名字只能用 +字母、数字、`.`、`_`、`+`、`-`,不能叫 `latest`。 ## mise @@ -54,12 +72,13 @@ mise uninstall --all node 配置。官方文档:[uninstall](https://mise.jdx.dev/cli/uninstall.html) 和 [unuse](https://mise.jdx.dev/cli/unuse.html)。 -也可以链一个真实安装(路 B),不要链 shim 目录: +也可以链一个真实安装(路 B),不要链 shim 目录。 +`vfox link --detect nodejs` 自己会找到这些: ```sh mise ls node -vfox link nodejs@mise-22 ~/.local/share/mise/installs/node/22.0.0 -vfox use nodejs@mise-22 +vfox link nodejs@22.0.0 ~/.local/share/mise/installs/node/22.0.0 +vfox use nodejs@22.0.0 ``` ## asdf @@ -81,13 +100,14 @@ asdf uninstall nodejs [versions](https://asdf-vm.com/manage/versions.html)(卸载和 shim 在 同一页)。 -也可以链一个真实安装(路 B),不要链 shim 目录: +也可以链一个真实安装(路 B),不要链 shim 目录。 +`vfox link --detect nodejs` 自己会找到这些: ```sh asdf list nodejs asdf where nodejs 22.13.0 -vfox link nodejs@asdf-22 ~/.asdf/installs/nodejs/22.13.0 -vfox use nodejs@asdf-22 +vfox link nodejs@22.13.0 ~/.asdf/installs/nodejs/22.13.0 +vfox use nodejs@22.13.0 ``` ## nvm @@ -107,12 +127,13 @@ nvm deactivate 命令本身一起卸载,交接请用 `deactivate`。官方文档: [restoring PATH](https://github.com/nvm-sh/nvm#restoring-path)。 -也可以链一个已安装版本(路 B): +也可以链一个已安装版本(路 B)。 +`vfox link --detect nodejs` 自己会找到这些: ```sh nvm which 22 -vfox link nodejs@nvm-22 ~/.nvm/versions/node/v22.14.0 -vfox use nodejs@nvm-22 +vfox link nodejs@22.14.0 ~/.nvm/versions/node/v22.14.0 +vfox use nodejs@22.14.0 ``` ## Homebrew @@ -134,12 +155,12 @@ brew unlink node [manpage](https://docs.brew.sh/Manpage)(`unlink`、`uninstall`)。 也可以链 Cellar 目录(路 B),但 `brew upgrade` 会换掉它,链接可能悬 -空,到时重链: +空,到时重链。`vfox link --detect nodejs` 自己会找到这些: ```sh ls $(brew --cellar node) -vfox link nodejs@brew-24 /opt/homebrew/Cellar/node/24.5.0 -vfox use nodejs@brew-24 +vfox link nodejs@24.5.0 /opt/homebrew/Cellar/node/24.5.0 +vfox use nodejs@24.5.0 ``` ## winget 和官方安装程序 @@ -161,9 +182,11 @@ winget uninstall -e --id OpenJS.NodeJS `PATH` 条目才会消失。官方下载页: [nodejs.org](https://nodejs.org/en/download)。 -也可以链这个目录(路 B),MSI 升级会换掉底下的文件,vfox 只会提示: +也可以链这个目录(路 B),MSI 升级会换掉底下的文件,vfox 只会提示。 +`vfox link --detect nodejs` 自己会找到它;手动链: ```powershell -vfox link nodejs@system "C:\Program Files\nodejs" -vfox use nodejs@system +& "C:\Program Files\nodejs\node.exe" --version +vfox link nodejs@22.14.0 "C:\Program Files\nodejs" +vfox use nodejs@22.14.0 ``` diff --git a/hooks/detect.lua b/hooks/detect.lua new file mode 100644 index 0000000..9ff0db9 --- /dev/null +++ b/hooks/detect.lua @@ -0,0 +1,25 @@ +-- Copyright 2026 Han Li and contributors +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +local util = require("util") +--- Find the Node.js installs on this machine that vfox did not install, so +--- that `vfox link --detect nodejs` can link them: the nvm, mise, asdf and +--- Homebrew Cellar directories with a working `node` in them, and the winget +--- directory on Windows. Each entry carries the plain release number the +--- binary reports, and vfox skips what is installed or linked already. +--- Shims report nothing stable and are skipped. +--- @param ctx table Context information (unused) +function PLUGIN:Detect(ctx) + return util.detect_nodejs() +end diff --git a/lib/util.lua b/lib/util.lua index 4d67e79..d4d5e6a 100644 --- a/lib/util.lua +++ b/lib/util.lua @@ -565,6 +565,184 @@ function UTIL.supports_shadow_hints() return true end +--- Home directory of the user vfox runs as, nil when it cannot be told. +--- @return string|nil +function UTIL.home_dir() + local windows = RUNTIME ~= nil and RUNTIME.osType == "windows" + if windows then + local profile = os.getenv("USERPROFILE") + if profile ~= nil and profile ~= "" then + return profile + end + local drive, folder = os.getenv("HOMEDRIVE"), os.getenv("HOMEPATH") + if drive ~= nil and drive ~= "" and folder ~= nil and folder ~= "" then + return drive .. folder + end + return nil + end + local home = os.getenv("HOME") + if home == nil or home == "" then + return nil + end + return home +end + +--- Entry names of a directory, without `.` and `..`. A directory that cannot +--- be read gives nothing, so a manager that is not installed simply +--- contributes no candidates. +--- @param dir string|nil +--- @return string[] +function UTIL.list_dir(dir) + if dir == nil or dir == "" then + return {} + end + local windows = RUNTIME ~= nil and RUNTIME.osType == "windows" + local command + if windows then + command = 'dir /b "' .. dir:gsub('"', '""') .. '"' + else + command = "ls -1 " .. UTIL.shell_quote(dir) + end + local output, status = UTIL.capture(command) + if output == nil or (status ~= nil and status ~= 0) then + return {} + end + local names = {} + for line in string.gmatch(output, "[^\r\n]+") do + local name = line:gsub("%s+$", "") + if name ~= "" and name ~= "." and name ~= ".." then + table.insert(names, name) + end + end + return names +end + +--- Parent directories whose subdirectories are one installed Node.js each, +--- with the manager each belongs to. Shim directories are never listed: a +--- shim selects its version per directory, so linking one selects nothing +--- stable, and pnpm's shim ships no npm at all. +--- @return table[] { dir: string, source: string } +function UTIL.detect_roots() + local windows = RUNTIME ~= nil and RUNTIME.osType == "windows" + local roots = {} + local function add(dir, source) + if dir ~= nil and dir ~= "" then + table.insert(roots, { dir = dir, source = source }) + end + end + if windows then + local localappdata = os.getenv("LOCALAPPDATA") + if localappdata ~= nil and localappdata ~= "" then + add(localappdata .. "\\mise\\installs\\node", "mise") + end + local appdata = os.getenv("APPDATA") + if appdata ~= nil and appdata ~= "" then + add(appdata .. "\\nvm", "nvm") + end + local nvm_home = os.getenv("NVM_HOME") + if nvm_home ~= nil and nvm_home ~= "" then + add(nvm_home, "nvm") + end + else + local home = UTIL.home_dir() + if home ~= nil then + add(home .. "/.nvm/versions/node", "nvm") + local mise_data = os.getenv("MISE_DATA_DIR") + if mise_data ~= nil and mise_data ~= "" then + add(mise_data .. "/installs/node", "mise") + else + add(home .. "/.local/share/mise/installs/node", "mise") + end + local asdf_data = os.getenv("ASDF_DATA_DIR") + if asdf_data ~= nil and asdf_data ~= "" then + add(asdf_data .. "/installs/nodejs", "asdf") + else + add(home .. "/.asdf/installs/nodejs", "asdf") + end + end + add("/opt/homebrew/Cellar/node", "homebrew") + add("/usr/local/Cellar/node", "homebrew") + add("/home/linuxbrew/.linuxbrew/Cellar/node", "homebrew") + end + return roots +end + +--- Run ` --version` and read the release it prints, nil when it is not +--- a working `node` of one release. +--- @param node string path of the executable +--- @return string|nil version like "22.14.0" +local function node_release(node) + local windows = RUNTIME ~= nil and RUNTIME.osType == "windows" + local command + if windows then + command = '"' .. node:gsub('"', '""') .. '" --version' + else + command = UTIL.shell_quote(node) .. " --version" + end + local output = UTIL.capture(command) + if output == nil then + return nil + end + local version = tostring(output):match("v(%d+%.%d+%.%d+)") + if version ~= nil and UTIL.is_semver_simple(version) then + return version + end + return nil +end + +--- Node.js installs on this machine that vfox did not install, one entry per +--- release: `{ version, path, note }`. `path` is the directory EnvKeys +--- expects (holding `bin/` on Linux and macOS, `node.exe` on Windows) and +--- `version` is the plain release number the binary reports, so +--- `vfox use 22` keeps working and vfox skips what is installed already. +--- Shims, pnpm's global runtime and copies without a versioned home (such as +--- a manual `/usr/local/bin/node`) are not reported; link those by hand. +--- The winget directory reports its version by running it. +--- See https://github.com/version-fox/vfox/issues/737 +--- @return table[] +function UTIL.detect_nodejs() + local windows = RUNTIME ~= nil and RUNTIME.osType == "windows" + local found = {} + local seen = {} + local function consider(install_dir, source) + local path, node + if windows then + if UTIL.file_exists(install_dir .. "\\node.exe") then + path, node = install_dir, install_dir .. "\\node.exe" + elseif UTIL.file_exists(install_dir .. "\\bin\\node.exe") then + path, node = install_dir .. "\\bin", install_dir .. "\\bin\\node.exe" + else + return + end + else + if not UTIL.file_exists(install_dir .. "/bin/node") then + return + end + path, node = install_dir, install_dir .. "/bin/node" + end + local version = node_release(node) + if version == nil or seen[version] then + return + end + seen[version] = true + table.insert(found, { version = version, path = path, note = source }) + end + for _, root in ipairs(UTIL.detect_roots()) do + local sep = windows and "\\" or "/" + for _, name in ipairs(UTIL.list_dir(root.dir)) do + consider(root.dir .. sep .. name, root.source) + end + end + if windows then + local program_files = os.getenv("ProgramFiles") + if program_files == nil or program_files == "" then + program_files = "C:\\Program Files" + end + consider(program_files .. "\\nodejs", "winget") + end + return found +end + --- Whether a PATH entry belongs to vfox: like vfox's own check, one of its --- segments is `.vfox` (or the legacy `.version-fox`). local function is_vfox_path(dir) diff --git a/tests/hooks_test.lua b/tests/hooks_test.lua index 28f4810..05ee287 100644 --- a/tests/hooks_test.lua +++ b/tests/hooks_test.lua @@ -1054,7 +1054,274 @@ test("metadata declares shadowHints for known managers", function() end PLUGIN = saved_plugin end) --- lib/util.lua helpers. +-- Detect lists the installs of nvm, mise, asdf, Homebrew and winget, so +-- that `vfox link --detect nodejs` can link them. Shims are never listed. +dofile("hooks/detect.lua") +--- Run Detect with a stubbed home, directory listings, file checks and +--- `node --version` answers. `env` maps variable names to values, `false` +--- unsets one; `listing` maps a parent to its entry names; `existing` maps +--- a path to true; `outputs` maps a node binary to its `--version` output. +local function with_detect_fs(env, listing, existing, outputs, fn) + local saved_getenv, saved_list, saved_exists, saved_capture = + os.getenv, util.list_dir, util.file_exists, util.capture + os.getenv = function(key) + local value = env[key] + if value ~= nil then + if value == false then + return nil + end + return value + end + return saved_getenv(key) + end + util.list_dir = function(dir) + local names = listing[dir] + if names == nil then + return {} + end + return names + end + util.file_exists = function(path) + return existing[path] == true + end + util.capture = function(command) + for _, item in ipairs(outputs) do + if command:find(item.bin, 1, true) then + return item.out, item.status + end + end + return "", 1 + end + local ok, err = pcall(fn) + os.getenv, util.list_dir, util.file_exists, util.capture = saved_getenv, saved_list, saved_exists, saved_capture + assert(ok, err) +end +test("detect finds the installs of nvm, mise, asdf and homebrew", function() + RUNTIME = { osType = "linux", archType = "amd64" } + local home = "/home/u" + local nvm, mise, asdf = + home .. "/.nvm/versions/node", home .. "/.local/share/mise/installs/node", home .. "/.asdf/installs/nodejs" + local function bin(dir) + return dir .. "/bin/node" + end + local listing = { + [nvm] = { "v22.14.0", "lts-iron", "v20.19.0", "broken" }, + [mise] = { "22.0.0" }, + [asdf] = { "22.13.0" }, + ["/opt/homebrew/Cellar/node"] = { "24.5.0", "unreadable" }, + } + local existing = { + [bin(nvm .. "/v22.14.0")] = true, + [bin(nvm .. "/v20.19.0")] = true, + [bin(nvm .. "/broken")] = true, + [bin(mise .. "/22.0.0")] = true, + [bin(asdf .. "/22.13.0")] = true, + [bin("/opt/homebrew/Cellar/node/24.5.0")] = true, + [bin("/opt/homebrew/Cellar/node/unreadable")] = true, + } + local outputs = { + { bin = bin(nvm .. "/v22.14.0"), out = "v22.14.0\n", status = 0 }, + { bin = bin(nvm .. "/v20.19.0"), out = "v20.19.0\n", status = 0 }, + { bin = bin(nvm .. "/broken"), out = "Segmentation fault\n", status = 139 }, + { bin = bin(mise .. "/22.0.0"), out = "v22.0.0\n", status = 0 }, + { bin = bin(asdf .. "/22.13.0"), out = "v22.13.0\n", status = 0 }, + { bin = bin("/opt/homebrew/Cellar/node/24.5.0"), out = "v24.5.0\n", status = 0 }, + { bin = bin("/opt/homebrew/Cellar/node/unreadable"), out = "v24\n", status = 0 }, + } + with_detect_fs({ HOME = home }, listing, existing, outputs, function() + local found = PLUGIN:Detect({}) + local want = { + { version = "22.14.0", path = nvm .. "/v22.14.0", note = "nvm" }, + { version = "20.19.0", path = nvm .. "/v20.19.0", note = "nvm" }, + { version = "22.0.0", path = mise .. "/22.0.0", note = "mise" }, + { version = "22.13.0", path = asdf .. "/22.13.0", note = "asdf" }, + { version = "24.5.0", path = "/opt/homebrew/Cellar/node/24.5.0", note = "homebrew" }, + } + assert(#found == #want, "five installs, got " .. #found) + for i, item in ipairs(want) do + assert(found[i].version == item.version, tostring(found[i].version)) + assert(found[i].path == item.path, tostring(found[i].path)) + assert(found[i].note == item.note, tostring(found[i].note)) + end + end) +end) +test("detect looks in MISE_DATA_DIR and ASDF_DATA_DIR when set", function() + RUNTIME = { osType = "linux", archType = "amd64" } + local function bin(dir) + return dir .. "/bin/node" + end + local listing = { + ["/data/mise/installs/node"] = { "22.0.0" }, + ["/data/asdf/installs/nodejs"] = { "22.13.0" }, + } + local existing = { + [bin("/data/mise/installs/node/22.0.0")] = true, + [bin("/data/asdf/installs/nodejs/22.13.0")] = true, + } + local outputs = { + { bin = bin("/data/mise/installs/node/22.0.0"), out = "v22.0.0\n", status = 0 }, + { bin = bin("/data/asdf/installs/nodejs/22.13.0"), out = "v22.13.0\n", status = 0 }, + } + local env = { HOME = "/home/u", MISE_DATA_DIR = "/data/mise", ASDF_DATA_DIR = "/data/asdf" } + with_detect_fs(env, listing, existing, outputs, function() + local found = PLUGIN:Detect({}) + assert(#found == 2, "two installs, got " .. #found) + assert(found[1].path == "/data/mise/installs/node/22.0.0", tostring(found[1].path)) + assert(found[2].path == "/data/asdf/installs/nodejs/22.13.0", tostring(found[2].path)) + end) +end) +test("detect keeps the first of two installs of one release", function() + RUNTIME = { osType = "linux", archType = "amd64" } + local home = "/home/u" + local nvm = home .. "/.nvm/versions/node" + local function bin(dir) + return dir .. "/bin/node" + end + local listing = { + [nvm] = { "v22.14.0" }, + ["/opt/homebrew/Cellar/node"] = { "22.14.0" }, + } + local existing = { + [bin(nvm .. "/v22.14.0")] = true, + [bin("/opt/homebrew/Cellar/node/22.14.0")] = true, + } + local outputs = { + { bin = bin(nvm .. "/v22.14.0"), out = "v22.14.0\n", status = 0 }, + { bin = bin("/opt/homebrew/Cellar/node/22.14.0"), out = "v22.14.0\n", status = 0 }, + } + with_detect_fs({ HOME = home }, listing, existing, outputs, function() + local found = PLUGIN:Detect({}) + assert(#found == 1, "one entry, got " .. #found) + assert(found[1].version == "22.14.0", tostring(found[1].version)) + assert(found[1].path == nvm .. "/v22.14.0", tostring(found[1].path)) + assert(found[1].note == "nvm", tostring(found[1].note)) + end) +end) +test("detect finds mise, nvm and winget installs on windows", function() + RUNTIME = { osType = "windows", archType = "amd64" } + local localappdata, appdata, program_files = + "C:\\Users\\u\\AppData\\Local", "C:\\Users\\u\\AppData\\Roaming", "C:\\Program Files" + local listing = { + [localappdata .. "\\mise\\installs\\node"] = { "22.0.0", "empty" }, + [appdata .. "\\nvm"] = { "v20.19.0" }, + } + local existing = { + [localappdata .. "\\mise\\installs\\node\\22.0.0\\bin\\node.exe"] = true, + [appdata .. "\\nvm\\v20.19.0\\node.exe"] = true, + [program_files .. "\\nodejs\\node.exe"] = true, + } + local outputs = { + { bin = localappdata .. "\\mise\\installs\\node\\22.0.0\\bin\\node.exe", out = "v22.0.0\r\n", status = 0 }, + { bin = appdata .. "\\nvm\\v20.19.0\\node.exe", out = "v20.19.0\r\n", status = 0 }, + { bin = program_files .. "\\nodejs\\node.exe", out = "v24.5.0\r\n", status = 0 }, + } + local env = { LOCALAPPDATA = localappdata, APPDATA = appdata, ProgramFiles = program_files } + with_detect_fs(env, listing, existing, outputs, function() + local found = PLUGIN:Detect({}) + local want = { + { version = "22.0.0", path = localappdata .. "\\mise\\installs\\node\\22.0.0\\bin", note = "mise" }, + { version = "20.19.0", path = appdata .. "\\nvm\\v20.19.0", note = "nvm" }, + { version = "24.5.0", path = program_files .. "\\nodejs", note = "winget" }, + } + assert(#found == #want, "three installs, got " .. #found) + for i, item in ipairs(want) do + assert(found[i].version == item.version, tostring(found[i].version)) + assert(found[i].path == item.path, tostring(found[i].path)) + assert(found[i].note == item.note, tostring(found[i].note)) + end + end) +end) +test("detect finds nothing without installs", function() + RUNTIME = { osType = "linux", archType = "amd64" } + local env = { HOME = false, MISE_DATA_DIR = false, ASDF_DATA_DIR = false } + with_detect_fs(env, {}, {}, {}, function() + assert(#PLUGIN:Detect({}) == 0) + end) + RUNTIME = { osType = "windows", archType = "amd64" } + local wenv = { LOCALAPPDATA = false, APPDATA = false, NVM_HOME = false, ProgramFiles = "C:\\None" } + with_detect_fs(wenv, {}, {}, {}, function() + assert(#PLUGIN:Detect({}) == 0) + end) +end) +test("list_dir reads ls -1 and dir /b output", function() + RUNTIME = { osType = "linux", archType = "amd64" } + local result = {} + with_util({ + capture = function(command) + result.command = command + return "v22.14.0\nv20.19.0\n", 0 + end, + }, function() + result.names = util.list_dir("/home/u/.nvm/versions/node") + end) + assert(result.command == "ls -1 '/home/u/.nvm/versions/node'", tostring(result.command)) + assert(#result.names == 2 and result.names[2] == "v20.19.0", tostring(result.names[2])) + RUNTIME = { osType = "windows", archType = "amd64" } + with_util({ + capture = function(command) + result.command = command + return "v20.19.0\r\n22.0.0\r\n", 0 + end, + }, function() + result.names = util.list_dir("C:\\Users\\u\\AppData\\Roaming\\nvm") + end) + assert(result.command == 'dir /b "C:\\Users\\u\\AppData\\Roaming\\nvm"', tostring(result.command)) + assert(#result.names == 2 and result.names[1] == "v20.19.0", tostring(result.names[1])) + with_util({ + capture = function() + return nil, nil + end, + }, function() + assert(#util.list_dir("/no-such-dir") == 0) + end) + with_util({ + capture = function() + return "ls: cannot access '/no-such-dir': No such file\n", 2 + end, + }, function() + assert(#util.list_dir("/no-such-dir") == 0) + end) + assert(#util.list_dir("") == 0 and #util.list_dir(nil) == 0) +end) +test("home_dir follows the platform", function() + RUNTIME = { osType = "linux", archType = "amd64" } + local saved_getenv = os.getenv + os.getenv = function(key) + if key == "HOME" then + return "/home/u" + end + return saved_getenv(key) + end + assert(util.home_dir() == "/home/u") + os.getenv = function(key) + if key == "HOME" then + return "" + end + return saved_getenv(key) + end + assert(util.home_dir() == nil) + RUNTIME = { osType = "windows", archType = "amd64" } + os.getenv = function(key) + local values = { USERPROFILE = "C:\\Users\\u", HOMEDRIVE = "C:", HOMEPATH = "\\Users\\u" } + if values[key] ~= nil then + return values[key] + end + return saved_getenv(key) + end + assert(util.home_dir() == "C:\\Users\\u") + os.getenv = function(key) + if key == "USERPROFILE" then + return "" + end + local values = { HOMEDRIVE = "C:", HOMEPATH = "\\Users\\u" } + if values[key] ~= nil then + return values[key] + end + return saved_getenv(key) + end + assert(util.home_dir() == "C:\\Users\\u") + os.getenv = saved_getenv +end) test("version_prefix reads what a version file asks for", function() local accepted = { ["22"] = { 22 },