diff --git a/.gitignore b/.gitignore index 9309cf0..eac3e1d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,7 @@ # limitations under the License. .idea + +# What the plugin learns about LTS lines, when a checkout is used as the plugin directory +.lts-cache +.lts-cache.tmp diff --git a/README.md b/README.md index 028c6cc..c57455c 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,13 @@ and checks every download against the SHA-256 sums published next to it. Each release comes with its own npm. On Alpine, OpenWrt and other musl systems it installs the musl builds. vfox reads `.nvmrc` and `.node-version` through it. +It does for vfox what [nvm-sh/nvm](https://github.com/nvm-sh/nvm) does for POSIX +shells: several releases side by side, one chosen per shell, per project or as +the default, and `.nvmrc` read. nvm-sh/nvm is written for POSIX shells and does +not run in PowerShell. vfox does, so Windows PowerShell 5.1 gets the same work. +[docs/scenarios.md](docs/scenarios.md) shows it in concrete cases, with the +commands side by side. + ## Install After installing [vfox](https://github.com/version-fox/vfox), install the plugin by running: @@ -74,6 +81,13 @@ calls `libstdc++` the `libstdcpp` package). musl builds exist for x64 and arm64 only. musl details (OpenWrt, tarball matrix, mirror fallback, which releases have a build): see [docs/musl.md](docs/musl.md). +## Scenarios + +See [docs/scenarios.md](docs/scenarios.md) for concrete cases: the nvm-sh/nvm +commands next to vfox's, Windows PowerShell 5.1 from scratch, one Node.js for a +whole team, several projects in one shell, running a command on a given release, +mirrors and editors. + ## Versions and names A version is a release number: `24.21.0`, or the start of one, `24.21` and `24`, @@ -107,10 +121,16 @@ details and for mirrors that were checked. ## `.nvmrc` and `.node-version` -vfox reads these files when you enter a directory, and selects the release they -name if it is installed. What a file can say depends on vfox's +vfox reads these files at every prompt in a directory that has one, and selects +the release they name if it is installed, so a `cd` into the directory switches. +Unreleased: release 0.5.3 reads them when a shell starts in the directory, not +after a `cd` from a running shell. Neither switches back when you `cd` into a +directory with no config at all; a `.vfox.toml`, which `vfox use --project` +writes, does. What a file can say depends on vfox's `legacyVersionFile.strategy`: with the default, `specified`, only a full release -number selects something. See [docs/nvmrc.md](docs/nvmrc.md) for details. +number selects something. A file in UTF-16 or with a byte order mark, which +Windows PowerShell 5.1 writes, is read too (Unreleased). See +[docs/nvmrc.md](docs/nvmrc.md) for details. ## LTS diff --git a/README_CN.md b/README_CN.md index 2ac041b..aa56271 100644 --- a/README_CN.md +++ b/README_CN.md @@ -6,6 +6,8 @@ 它安装 [nodejs.org](https://nodejs.org/dist/) 或其[镜像](docs/zh-CN/mirror.md)上发布的 Node.js 版本压缩包,并对照一同发布的 SHA-256 校验和检查每次下载。每个版本都自带自己的 npm。在 Alpine、OpenWrt 和其他 musl 系统上,安装的是 musl 构建。vfox 通过它读取 `.nvmrc` 和 `.node-version`。 +它为 vfox 做的,正是 [nvm-sh/nvm](https://github.com/nvm-sh/nvm) 为 POSIX Shell 做的事:多个版本并存,可以按 Shell、按项目选定其中一个,或者设为默认,并读取 `.nvmrc`。nvm-sh/nvm 是为 POSIX Shell 写的,不能在 PowerShell 里运行;vfox 可以,所以 Windows PowerShell 5.1 也能做同样的事。[docs/zh-CN/scenarios.md](docs/zh-CN/scenarios.md) 用具体的场景展示这一点,并把两边的命令并排列出。 + ## 安装 先安装 [vfox](https://github.com/version-fox/vfox),然后执行下面的命令安装插件: @@ -56,6 +58,10 @@ vfox use --global nodejs@24 Node.js 的 musl 构建需要 `libstdc++`,26.8.0 及以后的构建还需要 `libatomic`。原版 Alpine 镜像两者都没有,而插件不会安装系统库。安装会中止,并给出一条提示,写明缺少哪个包以及安装它的命令(`apk add libatomic`;OpenWrt 把 `libstdc++` 称为 `libstdcpp` 包)。musl 构建只提供 x64 和 arm64。musl 的详细情况(OpenWrt、压缩包对照表、镜像回退、哪些版本有构建)见 [docs/zh-CN/musl.md](docs/zh-CN/musl.md)。 +## 使用场景 + +具体的场景见 [docs/zh-CN/scenarios.md](docs/zh-CN/scenarios.md):nvm-sh/nvm 与 vfox 的命令对照、从零开始使用 Windows PowerShell 5.1、全团队使用同一个 Node.js、一个 Shell 里的多个项目、用指定版本运行一条命令、镜像,以及编辑器。 + ## 版本与命名 版本就是版本号:`24.21.0`,也可以只写开头的部分,如 `24.21` 和 `24`,它们安装对应版本线的最新版本。`latest` 是所有版本线中最新的版本,不论是否属于 LTS。这里没有 `lts` 别名,请写明版本线(`nodejs@24`)。安装后的版本名就是版本号 `24.21.0`,`vfox uninstall` 需要写完整。`vfox use` 和 `vfox search` 怎样处理这些写法,详见 [docs/zh-CN/versions.md](docs/zh-CN/versions.md)。 @@ -74,7 +80,7 @@ Node.js 为 Linux(x64、arm64、ppc64le、s390x)、macOS(x64 和 arm64) ## `.nvmrc` 和 `.node-version` -进入目录时,vfox 会读取这些文件,并选中其中指定的版本(前提是已经安装)。文件里能写什么取决于 vfox 的 `legacyVersionFile.strategy`:默认值 `specified` 下,只有完整的版本号才能选中某个版本。详见 [docs/zh-CN/nvmrc.md](docs/zh-CN/nvmrc.md)。 +每个提示符出现在带有这些文件的目录里时,vfox 都会读取它们,并选中其中指定的版本(前提是已经安装),所以 `cd` 进这个目录就会切换。未发布:0.5.3 在 Shell 于该目录里启动时读取,已经在运行的 Shell 里 `cd` 进来则不读取。两者在 `cd` 到完全没有配置的目录时都不会切换回来;`vfox use --project` 写出的 `.vfox.toml` 则会。文件里能写什么取决于 vfox 的 `legacyVersionFile.strategy`:默认值 `specified` 下,只有完整的版本号才能选中某个版本。UTF-16 的文件或带字节顺序标记的文件(Windows PowerShell 5.1 会写出这样的文件)也能读取(未发布)。详见 [docs/zh-CN/nvmrc.md](docs/zh-CN/nvmrc.md)。 ## LTS diff --git a/docs/changes.md b/docs/changes.md index a4f4cbe..89eb397 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -25,6 +25,38 @@ ahead of it by everything under "Unreleased". requests, `vfox/1.0.13 vfox-nodejs/0.5.3` under vfox 1.0.13, and all of the mirrors of [mirror.md](mirror.md#mirrors-that-work) install 24.21.0. China Telecom Cloud, `https://mirrors.ctyun.cn/nodejs-release`, is in that list now. +- The plugin learns the LTS lines from the version index, so a line that + Node.js promotes later needs no release of the plugin. It keeps what it + learned in `.lts-cache` next to the plugin. It writes the file when + `vfox search`, or an install of `latest` or of a major, reads the index, and + when an install of an exact release finds the file missing or a day old. It + reads it for `lts/`, `lts` and `lts/*` under `specified` and + `latest_installed`, without any request. Before it has learned anything, or + where it cannot write the file, it uses a table of the codenames, which now + ends at Lithium (26): Node.js 26 entered long-term support on 2026-10-28. + Release 0.5.3 has the table only, and it ends at Krypton (24), so there + `lts/lithium` selects nothing under `latest_installed`, and `lts` and `lts/*` + select a 24.x. See [lts.md](lts.md#what-this-plugin-understands). +- A `.nvmrc` or `.node-version` in UTF-16, or in UTF-8 with a byte order mark, + is read. Windows PowerShell 5.1 writes the first with `>` and `Out-File`, and + the second with `-Encoding UTF8`. Release 0.5.3 reads the bytes as they are, + so no release matches the text and vfox says nothing. See + [nvmrc.md](nvmrc.md#what-the-file-may-hold). +- `.nvmrc` and `.node-version` are read at every prompt in their directory, so a + `cd` into it switches, and a change of the file takes effect at the next + prompt. Release 0.5.3 reads them when a shell starts in the directory and not + after a `cd`, because vfox 1.0.13 keeps the first result it worked out for the + shell and does not track version files. The plugin now removes that result + whenever vfox asks it to read such a file. A `cd` out of the directory into + one with no config at all does not switch back yet, and a `.vfox.toml` always + did. See [nvmrc.md](nvmrc.md#when-the-file-is-read). The cache is vfox's, and + vfox has been asked to fix it + ([version-fox/vfox#745](https://github.com/version-fox/vfox/issues/745), + tracked here as [#46](https://github.com/version-fox/vfox-nodejs/issues/46)). +- The documentation has a page of scenarios: the nvm-sh/nvm commands next to + vfox's, Windows PowerShell 5.1 from scratch, one Node.js for a whole team, + several projects in one shell, `vfox exec`, mirrors and editors. See + [scenarios.md](scenarios.md). ## 0.5.3 (2026-10-20) diff --git a/docs/development.md b/docs/development.md index 51d35a5..848ccd1 100644 --- a/docs/development.md +++ b/docs/development.md @@ -12,7 +12,8 @@ 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, 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 +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 +.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 Injection.lua type hints for what vfox injects, never loaded @@ -123,12 +124,24 @@ check that cannot work never blocks an install. See ## Legacy files -`ParseLegacyFile` reads the file, removes all whitespace, and gives the text to +`ParseLegacyFile` first removes `env-state.json` from the session directory +(`util.forget_env_state`). vfox keeps the result of `vfox env` there and does +not track version files, so without this a `cd` into a directory that only has +one is not noticed. vfox calls the hook before it loads that file, which is what +makes the workaround work, and it is why the hook cannot help on the way out of +the directory: it is not called where there is nothing to read. See +[nvmrc.md](nvmrc.md#when-the-file-is-read). + +It then reads the file and decodes it (`util.decode_text` turns UTF-16 with a +byte order mark into UTF-8, and drops the UTF-8 mark, because Windows PowerShell +5.1 writes both). It removes all whitespace, and gives the text to `resolve_version(query, strategy)`: it drops a leading `v`, lowers the case, turns `lts-x` into `lts/x`, and maps `lts/` to a major, `lts` and -`lts/*` to the highest major in `nodejs_codenames`. Under `latest_available` the -codename comes from the index first (`discover_lts`), and the table is the -fallback when that finds nothing or the request fails. +`lts/*` to the newest major of an LTS line. Under `latest_available` the +codename comes from the index first (`discover_lts`). Everywhere else, and when +that finds nothing or the request fails, `util.known_lts_lines` answers without +a request: what the plugin learned from the index over the built-in +`util.LtsLines`, see [below](#what-the-plugin-learns-about-lts-lines). `resolve_legacy_version(strategy, query)` then reads the query with `util.version_prefix`: `22` is `{ 22 }`, `22.23` is `{ 22, 23 }`, and `22.x`, @@ -145,21 +158,64 @@ fallback when that finds nothing or the request fails. `vfox` then selects the answer if it is installed. User-facing rules and examples are in [nvmrc.md](nvmrc.md) and [lts.md](lts.md). +## What the plugin learns about LTS lines + +`ParseLegacyFile` runs at every prompt in a directory with a version file, so it +cannot fetch `index.json` to find out what `lts/*` is. The plugin learns it when +it has the index in hand anyway, and keeps it in `.lts-cache`, a text file next +to the plugin: + +```text +# What vfox-nodejs learned from index.json about LTS lines. Safe to delete. +updated 1793000000 +newest 26 +line argon 4 +... +line lithium 26 +``` + +- `Available` calls `util.remember_lts` with the index it has decoded. + `util.lts_summary` finds the major of each named line and the newest major + that is LTS at all (a release whose `lts` is `true`, a line without a name, + counts for the newest only). It runs when vfox's own cache of the answer has + run out. +- `PreInstall` calls `util.refresh_lts_cache` once the install has its checksum, + so an install that fails early, such as the one for an architecture without a + package, fetches nothing. vfox installs an exact release without asking + `Available`, so this is where such an install learns. It calls `Available` + when the file is missing or older than a day, and only if the file can be + written, so that a plugin directory that is read-only does not cost an index + at every install. +- `ParseLegacyFile` reads the file through `util.known_lts_lines`, which puts + what was learned over the built-in `util.LtsLines` and takes the newest major + of the two for `lts` and `lts/*`. It never fetches. +- vfox gives a plugin no path of its own, so `util.plugin_dir` finds the + directory where `package.path` has `hooks/?.lua` and `lib/?.lua`. The new + file is written beside the old one and renamed over it. Every failure, no + directory, nothing writable, a damaged file, leaves the plugin with the table. +- In the checkout `package.path` starts with `./lib/?.lua`, so the directory + would be the repository. The hook tests point `util.lts_cache_path` at a file + of their own. + ## When Node.js promotes a line to LTS Each October an even-numbered line gets a codename, see -[lts.md](lts.md#when-nodejs-promotes-a-line). To teach the plugin the new one: - -1. Add ` = ` to `nodejs_codenames` in - `hooks/parse_legacy_file.lua`. -2. Add a case next to `legacy lts krypton maps to its major` in - `tests/hooks_test.lua`. -3. Add a row to the table in [lts.md](lts.md#what-this-plugin-understands), and - change the "24 at the time of writing" sentences in [lts.md](lts.md) and - [nvmrc.md](nvmrc.md). -4. `DefaultLTSVersion` in `tests/e2e/common/common.go` may stay: it only has to +[lts.md](lts.md#when-nodejs-promotes-a-line). The plugin learns the line from +the index and needs no release for it. This is housekeeping, so that a plugin +that has learned nothing yet knows it too, and that the documents are right: + +1. Add ` = ` to `UTIL.LtsLines` in `lib/util.lua`. +2. Add a case next to `legacy lts lithium maps to its major` in + `tests/hooks_test.lua`, and change the major that `legacy bare lts is the + newest line of the table` expects. +3. Add the codename to `LTSCodenames` in `tests/e2e/common/common.go`, and a + case to `TestLTSCodename`. The e2e suites compare that table with the file + the plugin wrote from the live index. +4. Add a row to the table in [lts.md](lts.md#what-this-plugin-understands), and + change the "26 at the time of writing" sentences in [lts.md](lts.md) and + [nvmrc.md](nvmrc.md), and the examples of [lts.md](lts.md#examples). +5. `DefaultLTSVersion` in `tests/e2e/common/common.go` may stay: it only has to stay inside the table. -5. Release. ## Tests @@ -176,12 +232,18 @@ itself (`package.preload`), sets `RUNTIME` per case, and answers requests from It covers `Available` and its failures, the package name per platform and the errors of a failed checksum request, musl and `PostInstall` (with `util.capture`, `util.file_exists` and `util.has_libstdcxx` stubbed through `with_util`), -`ParseLegacyFile` under each strategy, `PreUse` with the shadowing `node`, and -the helpers of `lib/util.lua`. Add the case a change needs there. +`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. 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 -`pcall` has to keep it in a table field. +`pcall` has to keep it in a table field. It also ends in a Go "nil pointer +dereference" at `if file == nil` after `local file = path ~= nil and +io.open(path, "r") or nil`: test `path` and open the file in two statements. +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 @@ -244,10 +306,23 @@ have no package. For the musl table run the same program against - The e2e Alpine image gets `libatomic` through `go`, which depends on `gcc`, so the suite does not cover a stock Alpine without it. The hook tests do. - `.nvmrc` aliases (`node`, `stable`, `lts/-1`), ranges and comments are not - understood, and `lts` follows a hand-made table, see - [nvmrc.md](nvmrc.md#not-understood). + understood, see [nvmrc.md](nvmrc.md#not-understood). +- A plugin directory that cannot be written, or a plugin that has not had the + index in hand since it was installed, leaves `specified` and + `latest_installed` with the built-in table, which ends at Lithium. +- A `cd` out of a directory with a version file into one with no config at all + does not switch back. vfox does not track version files and keeps the result + of its last calculation, and the plugin is not called where there is no file + to read, so `util.forget_env_state` only covers the way in. A fix belongs in + vfox: [version-fox/vfox#745](https://github.com/version-fox/vfox/issues/745), + tracked here as [#46](https://github.com/version-fox/vfox-nodejs/issues/46). + Drop `util.forget_env_state` when vfox tracks version files. +- Every prompt in a directory with a version file calculates the environment + again, because of `util.forget_env_state`. It took about a millisecond with + one SDK installed. - `latest_available` downloads `index.json` at every prompt in a directory with a legacy file. +- UTF-32, and UTF-16 without a byte order mark, are not decoded. - The unofficial site is not covered by `VFOX_NODEJS_MIRROR`. - macOS has no e2e job. diff --git a/docs/e2e.md b/docs/e2e.md index 226e4da..febac34 100644 --- a/docs/e2e.md +++ b/docs/e2e.md @@ -87,13 +87,23 @@ driven from Go through `docker exec`-style calls): 5. The major and major.minor shorthands resolve through `PreUse` against the installed versions. 6. `.nvmrc` and `.node-version` drive `vfox env` selection: the exact - pinned version under the default `specified` strategy, and an LTS - codename (`lts/`, see [lts](lts.md)) resolving to the LTS - release under `latest_installed`. Each legacy probe activates - inside its own shell so the selection is recomputed rather than answered - from the `vfox env` state cache of an earlier probe. + pinned version under the default `specified` strategy (on Windows also in + UTF-16 and in UTF-8 with a byte order mark, the bytes that Windows + PowerShell 5.1 writes), and an LTS codename (`lts/`, see + [lts](lts.md)) resolving to the LTS release under `latest_installed`. Each + legacy probe activates inside its own shell so the selection is recomputed + rather than answered from the `vfox env` state cache of an earlier probe. The + plugin removes that cache file when it reads a legacy file, and the Windows + probes rely on that instead of removing it themselves. 7. `vfox search nodejs` lists the installed release with the same npm version the binary bundles. +8. After the installs and the search, the plugin directory holds `.lts-cache` + (see [lts](lts.md#what-this-plugin-understands)) with every codename of the + plugin's table at the same major, and a newest LTS major of 26 or more. The + index names all of them, so a table that is wrong shows here. The Windows + containers keep vfox in `C:\Users\John Doe\.vfox`, so there the file also + shows that the plugin finds its directory with a space in the path and + writes to it. The default matrix is: @@ -132,6 +142,11 @@ Scope it down while iterating the same way: VFOX_VERSION=latest MIRROR=default E2E_MAX_JOBS=1 go test -v -timeout 55m -run TestE2E ./linux ``` +Add `-count=1` after you edit `hooks/` or `lib/`. Go replays the result of an +earlier run when what the test opened looks unchanged, and a second run after a +change to `lib/util.lua` still printed `ok ... (cached)`, with the output and +the timings of the first. + On a native host the same command works with two prerequisites — Docker Engine (rootful or rootless) and Go 1.27.1 — which is exactly what CI provisions. Rootless Docker users point the Docker client at their socket @@ -186,7 +201,11 @@ plugin files to stock servercore. Baking the tools into an image layer instead costs minutes of layer commit on the hosted runners (they are tens of thousands of files). The servercore pull — five to seven minutes on a fresh CI runner — runs in the background while the tools are fetched and the Go -code is compiled. +code is compiled. `run.ps1` first waits, for up to five minutes, until the +Docker engine answers `docker version`. A hosted runner can begin the job before +its engine listens: on 2026-11-03 the pull failed at once with `failed to +connect to the docker API at npipe:////./pipe/docker_engine`, and the job ended +with it, although the two runs before had passed on the same runner image. The `windows-11-arm` runner has no Windows Hypervisor Platform feature, so Hyper-V isolation is unavailable; process isolation needs only the @@ -276,8 +295,9 @@ Windows host directly through `wsl.exe` — no containers. It exists to reproduce [issue #32](https://github.com/version-fox/vfox-nodejs/issues/32): inside WSL, `vfox install` reported success but `node` was missing from `PATH`. On top of the shared flow (install with checksum, toolchain, -shorthands, search, legacy files) it asserts that `command -v node` resolves -under `VFOX_HOME` and that `vfox env -s bash` exports the SDK bin directory. +shorthands, search, learned LTS lines, legacy files) it asserts that +`command -v node` resolves under `VFOX_HOME` and that `vfox env -s bash` +exports the SDK bin directory. The pinned release defaults to `20.16.0` (the issue's version); `NODE_VERSION` overrides it. The LTS release (`LTS_NODE_VERSION`, default per diff --git a/docs/lts.md b/docs/lts.md index 8b9c5c8..1a2dcdc 100644 --- a/docs/lts.md +++ b/docs/lts.md @@ -5,7 +5,7 @@ Node.js majors with even numbers become **LTS** (long-term support) lines: they keep receiving fixes long after odd releases go end-of-life, so most production setups pin an LTS line. Each LTS line has a codename -(`argon` for 4, `boron` for 6, …, `krypton` for 24). +(`argon` for 4, `boron` for 6, …, `lithium` for 26). ## Installing an LTS line @@ -18,14 +18,14 @@ vfox install nodejs@24 ``` `vfox search nodejs` marks the releases of LTS lines with `(LTS)`, so the newest -LTS release is the first line that has the mark: +LTS release is the first line that has the mark (on 2026-11-03): ```text $ vfox search nodejs | grep -m1 '(LTS)' - - 24.22.0 (LTS) [npm 11.19.1] + - 26.13.0 (LTS) [npm 11.20.0] ``` -Install it with `vfox install nodejs@24.22.0`, or with `nodejs@24` to follow the +Install it with `vfox install nodejs@26.13.0`, or with `nodejs@26` to follow the line. The mark is the `lts` field of the version index, which Node.js sets from the day a line enters long-term support: 24.11.0 is the first release of Krypton with it, 24.10.0 and the ones before have none. `lts/` is @@ -34,14 +34,28 @@ understood in `.nvmrc` and `.node-version` files only, see ## What this plugin understands -Under the `latest_available` strategy the plugin discovers LTS codenames -dynamically from the version index (`index.json` carries the codename in -each release's `lts` field), so codenames that postdate this plugin keep -working there. Everywhere else — and as the offline fallback — a hardcoded -table in `hooks/parse_legacy_file.lua` is used, because `specified` and -`latest_installed` must resolve without network access. "First LTS release" and -"Last release" are from the index on 2026-10-21, and Krypton is still receiving -releases: +`index.json` carries the codename of an LTS line in the `lts` field of each +release (`"lts": "Lithium"`, and `false` for a release outside long-term +support). The plugin reads the codenames from there, and not from a list that a +maintainer has to keep up: + +- Under the `latest_available` strategy it asks the index when it reads the + file, at every prompt in the directory, so a codename that postdates the + plugin works on the day it appears. +- Under `specified` and `latest_installed` the plugin runs at every prompt too, + and must not fetch the index each time. It uses what it learned the last time + it had the index in hand: when `vfox search`, or an install of `latest` or of + a major such as `nodejs@26`, asks it for the list of releases (vfox keeps the + answer for a while, so not every search does), and when it installs a release + and the file is missing or a day old. It keeps what it learned in `.lts-cache` + next to the plugin, where vfox keeps its `.available.cache`. Delete the file + to make it forget, and it learns again at the next install. +- Before it has learned anything, or where it cannot write the file, it uses + the table below. The table is a start: a line that is missing from it is + learned as soon as the plugin has had the index in hand. + +"First LTS release" and "Last release" are from the index on 2026-11-03, and +Jod, Krypton and Lithium are still receiving releases: | Codename | Major | First LTS release | Last release | | -------- | ----- | ----------------- | ------------ | @@ -54,32 +68,40 @@ releases: | gallium | 16 | 16.13.0 | 16.20.2 | | hydrogen | 18 | 18.12.0 | 18.20.8 | | iron | 20 | 20.9.0 | 20.20.2 | -| jod | 22 | 22.11.0 | 22.23.3 | -| krypton | 24 | 24.11.0 | 24.22.0 | +| jod | 22 | 22.11.0 | 22.23.4 | +| krypton | 24 | 24.11.0 | 24.22.2 | +| lithium | 26 | 26.13.0 | 26.13.0 | ## When Node.js promotes a line An even-numbered line enters long-term support each October and gets its -codename then. The [release schedule](https://github.com/nodejs/Release) puts -Node.js 26 on 2026-10-28, with no codename announced yet. +codename then. Node.js 26 did on 2026-10-28, as Lithium: 26.13.0 is the first +release with the `lts` mark in the index. What a promotion looks like in +practice: - Under `latest_available` the plugin finds the new codename in the index the day it appears. `lts` and `lts/*` then name the newest release of the new - line. -- Under `specified` and `latest_installed` the table above decides, and the - table changes with a release of the plugin. Until then `lts/` - selects nothing, and `lts` and `lts/*` keep meaning the highest line in the - table, 24 at the time of writing. + line, and `lts/lithium` worked there at once, with release 0.5.3 as well. +- Under `specified` and `latest_installed` the plugin knows the line from the + first time it has the index in hand after the promotion: the `vfox search` or + the install that brings you the new line. `latest_installed` selects nothing + until a release of the line is installed, so that is early enough. Until then + `lts/` selects nothing, and `lts` and `lts/*` mean the newest + line that the plugin knows. - `(LTS)` in `vfox search` follows the index, so it changes at once. -The table is in `hooks/parse_legacy_file.lua`, see -[development.md](development.md#when-nodejs-promotes-a-line-to-lts). +Unreleased: release 0.5.3 has the table only, and no file. The table was +published before Lithium and ends at Krypton, so with 22.23.4, 24.22.2 and +26.13.0 installed on 2026-11-03 it selected 24.22.2 for `lts/*` and nothing for +`lts/lithium` under `latest_installed`, and only a release of the plugin adds a +line there. How the plugin learns is in +[development.md](development.md#what-the-plugin-learns-about-lts-lines). ## Examples Given an `.nvmrc` (the same applies to `.node-version`) and -`22.23.3` plus `24.21.0` installed. Examples 1 to 4 run under the -`latest_installed` strategy that example 1 sets, see +`22.23.4`, `24.21.0` and `26.13.0` installed, as on 2026-11-03. Examples 1 to 4 +run under the `latest_installed` strategy that example 1 sets, see [nvmrc.md](nvmrc.md#the-strategy-decides) for the strategies: ```sh @@ -88,34 +110,38 @@ $ cat .nvmrc lts/jod $ vfox config legacyVersionFile.strategy latest_installed $ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version -v22.23.3 +v22.23.4 ``` ```sh -# 2. Bare `lts` (or `lts/*`) means "the highest mapped major", 24 at the time -# of writing. +# 2. Bare `lts` (or `lts/*`) means "the newest LTS line that the plugin knows", +# 26 at the time of writing (Unreleased: 24 in release 0.5.3). $ printf 'lts\n' > .nvmrc $ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version -v24.21.0 +v26.13.0 ``` ```sh -# 3. An exact version skips the codename table entirely. -$ printf '22.23.3\n' > .nvmrc +# 3. An exact version needs no codename. +$ printf '22.23.4\n' > .nvmrc $ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version -v22.23.3 +v22.23.4 ``` ```sh -# 4. The current LTS line works the same way. +# 4. The current LTS line works the same way, and so does the one before it. +$ printf 'lts/lithium\n' > .nvmrc +$ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version +v26.13.0 $ printf 'lts/krypton\n' > .nvmrc $ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version v24.21.0 ``` ```sh -# 5. Under latest_available the index is consulted, so even a codename -# newer than the table above resolves (offline it falls back to the table). +# 5. Under latest_available the index is consulted, so even a codename that +# the plugin has not learned yet resolves (offline it falls back to what the +# plugin learned, then to the table above). $ vfox config legacyVersionFile.strategy latest_available $ printf 'lts/\n' > .nvmrc $ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version @@ -126,5 +152,5 @@ $ vfox config legacyVersionFile.strategy specified How example 1 resolves, step by step (`latest_installed`): ``` -.nvmrc "lts/jod" --resolve_version--> "22" --prefix-match installed--> "22.23.3" +.nvmrc "lts/jod" --resolve_version--> "22" --prefix-match installed--> "22.23.4" ``` diff --git a/docs/nvmrc.md b/docs/nvmrc.md index b1b63b3..efc76b4 100644 --- a/docs/nvmrc.md +++ b/docs/nvmrc.md @@ -22,8 +22,12 @@ cd my-project # holds the .nvmrc above node -v # v22.23.3 ``` -vfox's shell hook reads the file when you enter the directory and selects the -release it names. `vfox use` is not needed. The hook is vfox's own, see its +vfox's shell hook reads the file at every prompt in the directory and selects +the release it names, so a `cd` into the directory switches, and so does a +change of the file. `vfox use` is not needed. Unreleased: release 0.5.3 reads +the file when a shell starts in the directory, not after a `cd` from a running +shell, where the example prints the global release. See +[below](#when-the-file-is-read). The hook is vfox's own, see its [quick start](https://vfox.dev/guides/quick-start.html) for zsh, fish, PowerShell and the others. @@ -45,6 +49,81 @@ PowerShell and the others. - `vfox config legacyVersionFile.enable false` turns all legacy files off, for every plugin. The default is `true`. +## When the file is read + +vfox works out the environment of a shell at its first prompt and keeps the +result in `env-state.json` in the session directory. It works it out again only +when the `.vfox.toml` of the current directory is another one than before, when +`PATH` is not what it was, or when a config file of vfox is newer, which a +`vfox use` causes. A `.nvmrc` or `.node-version` is not among these (vfox +1.0.13, and `main` of vfox on 2026-11-03), so vfox does not notice that the +shell is in a directory that has one. + +Unreleased: the plugin makes up for that. vfox runs the plugin's +`ParseLegacyFile` before it looks at the kept result, and the plugin removes +`env-state.json` there (`util.forget_env_state`), so vfox works the environment +out again at every prompt in a directory with such a file. A `cd` into the +directory switches, a `cd` from one such directory to another does, and a +change of the file does at the next prompt. The extra calculation took about a +millisecond per prompt with one SDK installed, and `--debug` shows it as +`Config changed: true`. It is a workaround for vfox, to drop when vfox tracks +these files. + +It does nothing for a `cd` out of the directory into one that has no config at +all. The plugin is not called there, so vfox goes on with the result it made for +the directory you left (`Using cached output`), and the shell keeps that +release. A directory with a `.vfox.toml` or with a version file of its own does +switch, and so does a new shell. + +Release 0.5.3 does not remove the file. It reads a `.nvmrc` when the shell +starts in the directory, and again after a `vfox use`, and not after a `cd`, +in either direction. This is what `node -v` printed in one shell, step by step, +with vfox 1.0.13 and 26.13.0 as the global release (bash 5.3, and PowerShell +7.6.9 agrees): + +| Step | Release 0.5.3 | Unreleased | +| --- | --- | --- | +| A shell that starts in a directory with a `.nvmrc` that names 18.20.8 | v18.20.8 | v18.20.8 | +| A shell that starts in a directory with no config at all | v26.13.0 | v26.13.0 | +| `cd` into the directory with the `.nvmrc` for 18.20.8 | v26.13.0 | v18.20.8 | +| `cd` into a directory with a `.node-version` that names 24.22.2 | v26.13.0 | v24.22.2 | +| Edit that `.node-version` to name 22.23.4 | v26.13.0 | v22.23.4 | +| `cd` into a directory with no config at all | v26.13.0 | v22.23.4 | +| `cd` into a directory with a `.vfox.toml` that names 18.20.8 | v18.20.8 | v18.20.8 | +| `cd` into a directory with no config at all | v26.13.0 | v26.13.0 | + +The first row is a shell of its own, the other seven follow one another in a +second shell. `vfox --debug env -s bash` shows both sides of the unreleased +column. In a directory with a `.nvmrc`, the plugin parses the file and vfox +calculates: + +```text +Parsing legacy file /home/me/projects/legacy-api/.nvmrc +CallFunction: ParseLegacyFile +Found nodejs@18.20.8 +Config changed: true, configPaths: map[global:/home/me/.vfox/.vfox.toml] +CallFunction: EnvKeys +``` + +The first prompt after a `cd` into a directory with no config, where the plugin +has nothing to read: + +```text +Config changed: false, configPaths: map[global:/home/me/.vfox/.vfox.toml] +Using cached output +``` + +Release 0.5.3 prints the second block in the directory with the `.nvmrc` as +well, after `Found nodejs@18.20.8`. A `.vfox.toml` is tracked in both, so +`vfox use --project nodejs@18.20.8` is the way to get the switching in a +release. See [scenarios.md](scenarios.md#several-projects-in-one-shell). + +The cache is vfox's, and so is the fix. It is reported as +[version-fox/vfox#745](https://github.com/version-fox/vfox/issues/745), and +[#46](https://github.com/version-fox/vfox-nodejs/issues/46) tracks it here until +a vfox release has it. The workaround goes when a vfox release notices version +files itself. + ## The strategy decides What the file's text can name depends on `legacyVersionFile.strategy`, a @@ -66,25 +145,35 @@ network at every prompt, and a prompt in such a directory waits for it. ## What the file may hold -Before it looks at the text, the plugin removes all whitespace, drops a leading -`v`, makes the rest lower case and turns `lts-jod` into `lts/jod`. So -`v22.23.3`, a CRLF file and blanks around the version are fine. A name such as -`lts/jod` becomes the major of its line (22) with the table in -[lts.md](lts.md), except under `latest_available`, where the index is asked for -the newest release with that codename. A release number may end in `x`, `X` or -`*` for the parts it leaves open: `22.x`, `22.x.x` and `22.*` all mean 22. - -The results on 2026-10-21 with 20.20.2, 22.23.3, 24.21.0, 25.9.0 and 26.12.0 -installed, when 24.22.0 is the newest 24.x and 22.23.3 the newest 22.x: +Before it looks at the text, the plugin decodes it, removes all whitespace, +drops a leading `v`, makes the rest lower case and turns `lts-jod` into +`lts/jod`. So `v22.23.3`, a CRLF file and blanks around the version are fine. A +name such as `lts/jod` becomes the major of its line (22), which the plugin +knows from the index, see [lts.md](lts.md), except under `latest_available`, +where the index is asked for the newest release with that codename. A release +number may end in `x`, `X` or `*` for the parts it leaves open: `22.x`, `22.x.x` +and `22.*` all mean 22. + +Decoding means that a file in UTF-16, or in UTF-8 with a byte order mark, reads +like plain text. Windows PowerShell 5.1 writes such files with `>`, `Out-File` +and `-Encoding UTF8`, see +[scenarios.md](scenarios.md#writing-the-file-from-windows-powershell-51). +Unreleased: release 0.5.3 reads the bytes as they are, so the mark or the zero +bytes of UTF-16 become part of the text, no release matches it, and vfox says +nothing. + +The results on 2026-11-03 with 20.20.2, 22.23.4, 24.21.0, 25.9.0 and 26.13.0 +installed, when 24.22.2 is the newest 24.x and 22.23.4 the newest 22.x: | In the file | `specified` | `latest_installed` | `latest_available` | | --- | --- | --- | --- | -| `22.23.3`, `v22.23.3` | 22.23.3 | 22.23.3 | 22.23.3 | -| `22`, `v22`, `22.x`, `22.*` | nothing | 22.23.3 | 22.23.3 | -| `22.23` | nothing | 22.23.3 | 22.23.3 | -| `lts/jod`, `lts-jod`, `LTS/Jod` | nothing | 22.23.3 | 22.23.3 | -| `lts/krypton` | nothing | 24.21.0 | nothing: 24.22.0 is the newest and is not installed | -| `lts/*`, `lts` | nothing | 24.21.0 | nothing: 24.22.0 is the newest LTS release and is not installed | +| `22.23.4`, `v22.23.4` | 22.23.4 | 22.23.4 | 22.23.4 | +| `22`, `v22`, `22.x`, `22.*` | nothing | 22.23.4 | 22.23.4 | +| `22.23` | nothing | 22.23.4 | 22.23.4 | +| `lts/jod`, `lts-jod`, `LTS/Jod` | nothing | 22.23.4 | 22.23.4 | +| `lts/krypton` | nothing | 24.21.0 | nothing: 24.22.2 is the newest and is not installed | +| `lts/lithium` | nothing | 26.13.0 | 26.13.0 | +| `lts/*`, `lts` | nothing | 26.13.0 | 26.13.0 | | `18.20.8` or any release that is not installed | nothing | nothing | nothing | `latest_available` does not fall back to an older installed release. When the @@ -92,10 +181,13 @@ index names a newer release than the one you have, nothing is selected. Use `latest_installed` if you want the newest one you have. `lts` and `lts/*` mean the newest LTS line. Under `specified` and -`latest_installed` that is the highest major in the codename table (24 at the -time of writing), under `latest_available` the newest release that carries any -codename in the index. See [lts.md](lts.md) for the table and what happens -when Node.js promotes a new line. +`latest_installed` that is the newest line that the plugin knows (26 at the time +of writing), under `latest_available` the newest release that carries any +codename in the index. See [lts.md](lts.md) for how the plugin knows lines and +what happens when Node.js promotes a new one. Unreleased: release 0.5.3 knows +only a table that ends at Krypton (24), because it was published before Node.js +26 became an LTS line. There `lts/lithium` selects nothing under +`latest_installed`, and `lts/*` and `lts` select 24.21.0 in the table above. ## Not understood @@ -110,6 +202,9 @@ strategy: - Comments. `# pin` on its own line, or `22.23.3 # pin`, becomes part of the text, as all whitespace is removed before the text is read. - An empty file. +- A file in UTF-32, or in UTF-16 without a byte order mark. Only the mark tells + the plugin that a file is UTF-16, and without it the zero bytes stay in the + text. ## How a prefix is matched @@ -129,13 +224,22 @@ If the release does not switch: - The shell hook is not active. `eval "$(vfox activate bash)"` has to run in the shell, not only once somewhere else. +- Release 0.5.3 only: the shell was already running when you `cd`'d into the + directory, see [When the file is read](#when-the-file-is-read). Open a new + terminal there, or install the plugin of `main` + ([source-build.md](source-build.md)). +- The shell came from a directory with a version file and went into one with no + config at all, and it keeps the release of the directory it left. Open a new + terminal. - The shell is in a subdirectory of the one with the file. - The release is not installed. `vfox install nodejs@22.23.3`. - The strategy is `specified` and the file holds `22` or `lts/jod`. - Under `latest_available`, a newer release than the installed one is out. - A `.vfox.toml` or `.tool-versions` is in the way, or `legacyVersionFile.enable` is `false`. -- The text is on the list above, or has a comment in it. +- The text is on the list above, or has a comment in it, or the file is in an + encoding the plugin does not read (release 0.5.3: anything but plain ASCII + text). `vfox --debug env -s bash` prints what the plugin returned and what vfox did with it: @@ -153,4 +257,7 @@ SDK nodejs@22 from project scope not installed, trying lower-priority config it. vfox only selects among what is installed, and you install with `vfox install nodejs@`. A line of an `.nvmrc` is not always a spec that `vfox install` takes: `22.23.3` is, `22` is, `lts/jod` is not (see -[lts.md](lts.md#installing-an-lts-line)). +[lts.md](lts.md#installing-an-lts-line)). nvm reads the file when you type the +command, vfox at every prompt in the directory, see +[above](#when-the-file-is-read). The commands side by side are in +[scenarios.md](scenarios.md#coming-from-nvm). diff --git a/docs/platforms.md b/docs/platforms.md index f05bd27..43e7466 100644 --- a/docs/platforms.md +++ b/docs/platforms.md @@ -120,6 +120,9 @@ no `bin` directory as on Linux and macOS. - `VFOX_NODEJS_MIRROR` is read from the environment of the process that runs vfox. In PowerShell set it with `$env:VFOX_NODEJS_MIRROR = '...'` for the session, see [mirror.md](mirror.md#setting-it). +- For Windows PowerShell 5.1 from scratch (the execution policy, the profile, + the encoding of `.nvmrc`), see + [scenarios.md](scenarios.md#windows-powershell-51). ## vfox's own platforms @@ -141,7 +144,9 @@ pinned LTS release and `nodejs@latest` on: | Windows containers, amd64 and arm64 | `windows-latest`, `windows-11-arm` | | Ubuntu 24.04 on WSL | `windows-2025` | -macOS, 32-bit systems, ppc64le and s390x are not covered. The hook tests +macOS, 32-bit systems, ppc64le and s390x are not covered. Nor is Windows +PowerShell 5.1 (`powershell.exe`): the Windows jobs drive PowerShell 7 (`pwsh`), +and Windows PowerShell only runs the build of their images. The hook tests (`tests/hooks_test.lua`) run offline and check the package name for `windows` on amd64, 386 and arm64, `linux` on amd64 and arm, musl on amd64 and arm64, and `darwin` on arm64. diff --git a/docs/scenarios.md b/docs/scenarios.md new file mode 100644 index 0000000..e11d8e2 --- /dev/null +++ b/docs/scenarios.md @@ -0,0 +1,412 @@ +# Scenarios + +[中文](zh-CN/scenarios.md) | [English](scenarios.md) + +This plugin gives vfox what [nvm-sh/nvm](https://github.com/nvm-sh/nvm) gives a +POSIX shell: several Node.js releases side by side, one of them chosen for a +shell, for a project or as the default, and `.nvmrc` files read. nvm-sh/nvm is a +set of shell functions. Its README names sh, dash, ksh, zsh and bash on Unix, +macOS and WSL, adds Git Bash and Cygwin for some cases, and points other Windows +users to other tools. PowerShell is not in that list. vfox is a program with a +hook for each shell, so the same work runs in Windows PowerShell 5.1, which +ships with Windows 10 and 11, as well as in PowerShell 7, bash, zsh and fish. + +Each case below says what you want, what to type and what you see. The outputs +are from 2026-11-03 on Linux x64, with vfox 1.0.13 and this plugin as on `main`, +and the home directory is shown as `/home/me`. The PowerShell lines were run in +PowerShell 7.6.9 on Linux, except those that exist only on Windows: +`Set-ExecutionPolicy` and the user-level +`[Environment]::SetEnvironmentVariable`. Windows PowerShell 5.1 itself is not +part of the test suite ([platforms.md](platforms.md#what-ci-covers)), so a case +marks what rests on the documentation of PowerShell or on vfox's source, not on +a run there. + +## Coming from nvm + +| nvm-sh/nvm | vfox | Notes | +| --- | --- | --- | +| `nvm install 22` | `vfox install nodejs@22` | The newest 22.x, checked against its SHA-256 sum. See [versions.md](versions.md) | +| `nvm install node` | `vfox install nodejs@latest` | Unreleased: release 0.5.3 fails while the index lists a pre-release, as it has since 2026-10-29. `nodejs@26` works everywhere | +| `nvm install --lts`, `nvm install 'lts/*'` | `vfox install nodejs@26` | There is no `lts` name, name the line. 26 is the newest LTS line on 2026-11-03, and `vfox search nodejs \| grep -m1 '(LTS)'` shows the newest LTS release. See [lts.md](lts.md) | +| `nvm use 22` | `vfox use nodejs@22` | This shell only. `22` is the newest installed 22.x | +| `nvm alias default 22` | `vfox use --global nodejs@22` | Every new shell | +| `nvm ls` | `vfox list nodejs` | | +| `nvm ls-remote` | `vfox search nodejs` | Every release, with its npm | +| `nvm current` | `vfox current nodejs` | | +| `nvm exec 22.23.4 node app.js`, `nvm run 22.23.4 app.js` | `vfox exec nodejs@22.23.4 -- node app.js` | The whole release number. See [below](#run-a-command-on-a-given-release) | +| `nvm uninstall 22.23.4` | `vfox uninstall nodejs@22.23.4` | The whole release number | +| `nvm install-latest-npm` | `npm install -g npm@latest` | In the release in use only. See [npm.md](npm.md#updating-npm) | +| `nvm install --reinstall-packages-from=20 22` | nothing | Global packages belong to a release. See [npm.md](npm.md#global-packages-belong-to-a-release) | +| `.nvmrc` | `.nvmrc`, `.node-version`, `.vfox.toml` | See [nvmrc.md](nvmrc.md) | +| `NVM_NODEJS_ORG_MIRROR` | `VFOX_NODEJS_MIRROR` | See [mirror.md](mirror.md) | + +Four things differ from what nvm taught you: + +- `vfox use` changes the environment of your shell, which only vfox's hook can + do (`eval "$(vfox activate bash)"`, or the profile line in + [Windows PowerShell 5.1](#windows-powershell-51)). Without it `vfox use` stops + on Linux and macOS with `vfox requires hook support. Please ensure vfox is + properly initialized with 'vfox activate'`. `vfox install` and `vfox exec` + need no hook. +- There is no `nvm use` without an argument and no `nvm install` that reads + `.nvmrc`. vfox reads `.nvmrc` and `.node-version` itself, selects among the + installed releases and installs nothing. When it reads them is the point of + [Several projects in one shell](#several-projects-in-one-shell). +- There are no aliases. `lts`, `node` and `stable` are not names for `vfox + install` or `vfox use`. Only `.nvmrc` files understand `lts/*` and + `lts/`, and what they select depends on a setting, see + [nvmrc.md](nvmrc.md#the-strategy-decides). +- Global packages stay with their release, and vfox does not copy them to the + next one. + +## Windows PowerShell 5.1 + +You have a Windows PC with `powershell.exe`, which is Windows PowerShell 5.1, +and the README of the project says `nvm use 22`. `nvm` is not a command there. +Once vfox is installed (its +[quick start](https://vfox.dev/guides/quick-start.html) lists Scoop, winget and +an installer), four steps give you the same workflow. + +1. **Let PowerShell run your profile.** On a Windows client where nobody has set + an execution policy, Windows PowerShell 5.1 uses `Restricted`, which also + stops `$PROFILE` + ([about_Execution_Policies](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_execution_policies)), + and the profile is where the hook goes. The `CurrentUser` scope needs no + administrator: + + ```powershell + Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned + ``` + + A policy from Group Policy overrides this. `Get-ExecutionPolicy -List` shows + it as `MachinePolicy` or `UserPolicy`. + +2. **Hook vfox into PowerShell.** This is the line from vfox's quick start: + + ```powershell + if (-not (Test-Path -Path $PROFILE)) { New-Item -Type File -Path $PROFILE -Force }; Add-Content -Path $PROFILE -Value 'Invoke-Expression "$(vfox activate pwsh)"' + ``` + + Windows PowerShell 5.1 and PowerShell 7 read different profile files, so run + it in each one you use. The hook that `vfox activate pwsh` prints (vfox + 1.0.13) is plain PowerShell: it wraps the `prompt` function and sets + environment variables such as `$env:PATH`, and nothing in it needs + PowerShell 7. + +3. **Open a new window** and install Node.js: + + ```powershell + vfox add nodejs + vfox install nodejs@24 + vfox use --global nodejs@24 + node -v + ``` + +4. **Use it** the way the table above says. Spell out the scope of `vfox use`: + `--global`, `--project` or `--session` (`-g`, `-p`, `-s`). + +What is different on Windows: + +- `vfox use --global` also writes the link of the global release to your user + `Path` (`HKEY_CURRENT_USER\Environment`) and tells the running programs about + it, and prints `Applied global environment variables to Windows registry.` + (vfox 1.0.13, from its source). Programs that start afterwards, such as + cmd.exe or an editor opened from the Start menu, find the same `node` without + running a hook. Terminals and programs that were open before keep the `Path` + they started with. +- Everything stays per user. The releases are in `%USERPROFILE%\.vfox`, vfox + links them with junctions, which need no administrator, and the `Path` it + changes is your own (also from vfox's source). +- vfox's documentation lists a bare `vfox use` as global on Windows and session + elsewhere. In a shell without the hook there is no session, and vfox says so + instead of stopping as it does on Linux: + + ```text + Warning: The current shell lacks hook support. Switching to global scope automatically. + ``` + + If `vfox use --session` prints this, the profile did not run: the execution + policy of step 1, or the hook line is in the profile of the other PowerShell. +- Only the first `node` in `PATH` runs. One from the installer of nodejs.org, + from winget or Scoop, or from another version manager can sit in front of + vfox's. `vfox use` then prints a note that names it and says that it comes + before vfox in `PATH`, see [pnpm.md](pnpm.md). Uninstall the other one. +- A `.nvmrc` that Windows PowerShell 5.1 writes with `>` is UTF-16. + Unreleased: release 0.5.3 does not read it, see + [below](#writing-the-file-from-windows-powershell-51). + +## The same Node.js for the whole team + +Two files can say which release a project wants. Pick by who is on the team. + +**Everyone uses vfox: `.vfox.toml`.** `vfox use --project nodejs@22.23.4` writes +it, and `--project` is also how you change it: + +```toml +[tools] +nodejs = "22.23.4" +``` + +When the repository has a `.gitignore`, vfox adds `.vfox/` to it and says +`Added '.vfox/' to .gitignore`. Without one, `git status` lists `.vfox/` and +`.vfox.toml`, and you ignore `.vfox/` yourself. Commit `.vfox.toml`. A new +teammate installs what it names: + +```text +$ vfox install --all -y +Install the following plugins and SDKs: +SDK: + nodejs@22.23.4 + [1/1] SDK: nodejs@22.23.4 installing... + [1/1] Installation completed. +Green indicates successful installation, while red indicates installation failure. +SDK: + nodejs@22.23.4 +$ vfox install --all -y +All plugins and SDKs are already installed +``` + +(The spinner frames are left out. Without `-y`, vfox asks first, and a script +stops with `Use the -y flag to automatically confirm installation in +non-interactive environments`.) + +**Part of the team uses nvm-sh/nvm: `.nvmrc`.** Keep the file they have. +`.node-version` works the same way. With vfox's default setting a full release +number (`22.23.4`) is what selects something, and `22` or `lts/*` need +`latest_installed`, see [nvmrc.md](nvmrc.md#the-strategy-decides). vfox does not +install what the file names, so install it once: + +```bash +vfox install "nodejs@$(tr -d '[:space:]' < .nvmrc)" +``` + +```powershell +vfox install "nodejs@$((Get-Content .nvmrc -TotalCount 1).Trim())" +``` + +`v22.23.4` works, as vfox drops the `v`, and so does `22`, which installs the +newest 22.x. `lts/*` and the other `.nvmrc` names do not: the install ends with +`version not found for provided version lts/*`. + +The `.nvmrc` is applied at every prompt in the project directory, so a `cd` into +it switches. Unreleased: release 0.5.3 applies it when a shell starts there, for +instance in the terminal that your editor opens in the folder, and not after a +`cd` from a running shell. Leaving the directory for one with no config at all +does not switch back in either, see +[Several projects in one shell](#several-projects-in-one-shell). If the +repository should not get a vfox file, `vfox use --project` still gives you a +`.vfox.toml` for yourself, which switches in every release and in both +directions. Put `.vfox.toml` and `.vfox/` in `.git/info/exclude`, and run `vfox +config gitignore.enable false` (for all projects) so that vfox does not add +`.vfox/` to the `.gitignore` that the repository tracks. + +### Writing the file from Windows PowerShell 5.1 + +Windows PowerShell 5.1 writes UTF-16 for `>`, `>>` and `Out-File`, UTF-8 with a +byte order mark for `-Encoding UTF8`, and the ANSI code page for `Set-Content` +without `-Encoding`, which is plain text for a release number +([about_Character_Encoding](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_character_encoding)). +The plugin decodes UTF-16 and drops the mark before it reads the text. +Unreleased: release 0.5.3 reads the bytes as they are, so the mark or the zero +bytes of UTF-16 become part of the text, no release matches it, and vfox says +nothing about it. The table gives the first bytes of each file and what a +directory holding it selected, with 22.23.4 installed and the default strategy: + +| The file starts with | Written by Windows PowerShell 5.1 with | Release 0.5.3 | Unreleased | +| --- | --- | --- | --- | +| `32 32 2e 32 33 2e 34 0d 0a` | `Set-Content .nvmrc 22.23.4 -Encoding ascii`, or `Set-Content .nvmrc 22.23.4` | 22.23.4 | 22.23.4 | +| `ef bb bf 32 32 2e ...` | `Set-Content .nvmrc 22.23.4 -Encoding UTF8` | nothing | 22.23.4 | +| `ff fe 32 00 32 00 2e 00 ...` | `'22.23.4' > .nvmrc`, `'22.23.4' \| Out-File .nvmrc` | nothing | 22.23.4 | + +The bytes were written on Linux and tried there, with both versions of the +plugin. What Windows PowerShell writes is from Microsoft's documentation. +PowerShell 7 writes UTF-8 without a mark for all of these, which every release +reads. This one works everywhere: + +```powershell +Set-Content -Path .nvmrc -Value '22.23.4' -Encoding ascii +``` + +## Several projects in one shell + +A legacy API on Node.js 18, a web application on 24, and everything else on the +global 26. In a hooked shell, `cd` between them. This is what `node -v` printed +at each step of one shell, with 18.20.8, 24.22.2 and 26.13.0 installed and +26.13.0 as the global release (bash 5.3, and PowerShell 7.6.9 agrees): + +| Step | Release 0.5.3 | Unreleased | +| --- | --- | --- | +| The shell starts in a directory with no config at all | v26.13.0 | v26.13.0 | +| `cd` into a directory with a `.nvmrc` that names 18.20.8 | v26.13.0 | v18.20.8 | +| `cd` into a directory with a `.node-version` that names 24.22.2 | v26.13.0 | v24.22.2 | +| `cd` into a directory with no config at all | v26.13.0 | v24.22.2 | +| `cd` into a directory with a `.vfox.toml` that names 18.20.8 | v18.20.8 | v18.20.8 | +| `cd` into a directory with a `.vfox.toml` that names 24.22.2 | v24.22.2 | v24.22.2 | +| `cd` into a directory with no config at all | v26.13.0 | v26.13.0 | + +- **`.vfox.toml`** switches on every `cd`, and back to the global release when + you leave for a directory with no config. It does in every release. +- **`.nvmrc` and `.node-version`** are read at every prompt in their directory + (Unreleased). A `cd` from one such directory into another switches, and + editing the file takes effect at the next prompt. Release 0.5.3 reads them + only when the shell starts in the directory, so none of the `cd` steps above + switched. +- **Leaving** a directory with such a file for one with no config at all does + not switch back, in either: the shell keeps the release it had, as the fourth + step shows. The plugin is not called in a directory with nothing to read, and + vfox goes on with the result it kept. A new terminal, or a directory with a + `.vfox.toml` or a version file of its own, switches. That is a limit of vfox's + cache, reported as + [version-fox/vfox#745](https://github.com/version-fox/vfox/issues/745) and + tracked here as [#46](https://github.com/version-fox/vfox-nodejs/issues/46). + +Why is in [nvmrc.md](nvmrc.md#when-the-file-is-read): vfox keeps the result of +its first calculation for the shell and does not track version files (vfox +1.0.13), and the plugin makes vfox calculate again whenever it reads one. So: + +- For projects that you move between in one shell, `.vfox.toml` is the one that + works in every release and in both directions: `vfox use --project + nodejs@18.20.8` in each of them. +- A `.nvmrc` is enough where a shell starts in the directory: the terminal of + your editor, a new tab in the same folder, a script. This is also why the + Windows e2e suite of this repository starts a new shell for each directory. + +## Another release in this window only + +You want to run the tests on 26 and leave everything else as it is: + +```powershell +vfox use --session nodejs@26 +node -v # v26.13.0 +vfox current nodejs # -> v26.13.0 +``` + +The choice takes effect at the next prompt, which is right after the command. +Close the window and it is gone, and a new window has the global release again. +Nothing is written in the project. In a directory whose `.vfox.toml` names +`nodejs`, that file wins over `--session`, and a `.nvmrc` wins over a plain +`vfox use`, see [nvmrc.md](nvmrc.md#using-it). + +## Run a command on a given release + +`vfox exec` runs one command with a release you name. It changes no scope, +writes no file in the project and needs no hook. That makes it the tool for a +shell without the hook, such as a Dockerfile, a CI job or a scheduled task, +which vfox's own [FAQ](https://vfox.dev/guides/faq.html) recommends. + +```bash +vfox exec nodejs@22.23.4 -- node -v # v22.23.4 +vfox exec nodejs@22.23.4 -- npm test +for v in 20.20.2 22.23.4 24.22.2; do vfox exec "nodejs@$v" -- node -v; done +``` + +```powershell +foreach ($v in '20.20.2', '22.23.4', '24.22.2') { vfox exec "nodejs@$v" -- node -v } +``` + +The three releases printed `v20.20.2`, `v22.23.4` and `v24.22.2`, in bash and in +PowerShell 7. What to know: + +- **Give the whole release number.** `vfox exec` does not read `22` the way + `vfox use` does, and fails: + + ```text + $ vfox exec nodejs@22 -- node -v + SDK nodejs@22 not found, installing... + nodejs@22.23.4 is already installed + failed to get runtime package for nodejs@22: open /home/me/.vfox/cache/nodejs/v-22: no such file or directory + ``` + +- **A release that is not installed is installed first**, with the same check of + its SHA-256 sum, and then runs the command. A typo gets the same treatment and + ends where an install does: `nodejs@22.99.0` answers `get checksum failed: + HTTP 404 from https://nodejs.org/dist/v22.99.0/SHASUMS256.txt`. +- **The exit status of the command is vfox's.** `vfox exec nodejs@22.23.4 -- + node -e "process.exit(3)"` prints `exit status 3` and exits with 3, so a loop + or a CI step fails when a test fails. +- **No release means the configured one.** `vfox exec nodejs -- node -v` uses + what the project, the session and the global scope say. +- **On Windows, name the program the way it is on disk**, `node.exe` or + `npm.cmd`. `vfox exec` looks for the name you give in the directory of the + release. This is from vfox's source and was not run on Windows. + +## Follow a line or pin a release + +Node.js ships fixes as new releases of a line, 24.22.1 and then 24.22.2. A +project either stays on a release or follows its line. + +- **Pin.** A full release in `.vfox.toml` or `.nvmrc`. Everyone runs the same + Node.js until someone changes the file. To move on: `vfox install nodejs@24` + installs the newest 24.x (`nodejs@24.22.2 is already installed` when you have + it), and `vfox use --project nodejs@24` writes the release it chose into + `.vfox.toml`, `nodejs = "24.22.2"`. +- **Follow.** `24` in a `.nvmrc`, with `vfox config legacyVersionFile.strategy + latest_installed`. A new 24.x is picked up on every machine that installs it, + and machines can differ for a while. See + [nvmrc.md](nvmrc.md#the-strategy-decides). + +Global packages do not move to the new release. List them before you switch, +and install them again afterwards. Each release has its own: + +```text +$ npm ls -g --depth=0 +/home/me/.vfox/cache/nodejs/v-22.23.4/nodejs-22.23.4/lib +├── corepack@0.36.0 +├── npm@10.9.9 +└── semver@7.8.6 +$ vfox exec nodejs@24.22.2 -- npm ls -g --depth=0 +/home/me/.vfox/cache/nodejs/v-24.22.2/nodejs-24.22.2/lib +├── corepack@0.36.0 +└── npm@11.19.1 +``` + +`vfox uninstall nodejs@24.22.1` removes the old release with its global +packages. On Linux a release takes 157 to 225 MB, from 18.20.8 to 26.13.0. See +[versions.md](versions.md#installed-names) for the names `uninstall` takes. + +## Behind a firewall or a corporate proxy + +Node.js archives and npm packages come from two places, and each has its own +setting: + +```bash +export VFOX_NODEJS_MIRROR="https://npmmirror.com/mirrors/node" +npm config set registry https://registry.npmmirror.com +``` + +```powershell +[Environment]::SetEnvironmentVariable("VFOX_NODEJS_MIRROR", "https://npmmirror.com/mirrors/node", "User") +npm config set registry https://registry.npmmirror.com +``` + +The first line of PowerShell sets the variable for your user, for the terminals +that you open afterwards. A mirror that your company serves works the same way +if it has the layout of `nodejs.org/dist`. A proxy is set with `vfox config +proxy.enable true` and `vfox config proxy.url
`. Both are explained in +[mirror.md](mirror.md) and [troubleshooting.md](troubleshooting.md#network). + +A mirror can be behind its own `index.json`. On 2026-11-03 npmmirror listed +26.13.0 and had no `SHASUMS256.txt` for it, so `nodejs@26` failed there while +`nodejs@24` worked: + +```text +get checksum failed: HTTP 404 from https://npmmirror.com/mirrors/node/v26.13.0/SHASUMS256.txt +``` + +Install the release before it (`nodejs@26.12.1`), or try again later. + +## Editors and IDEs + +- To follow your global choice, point the editor or the build tool at + `~/.vfox/sdks/nodejs/bin/node`, on Windows at + `%USERPROFILE%\.vfox\sdks\nodejs\node.exe`. For a fixed release use the path + of the release, `~/.vfox/cache/nodejs/v-22.23.4/nodejs-22.23.4/bin/node`. See + [npm.md](npm.md#where-the-files-are). +- An editor that you start from the Start menu runs no hook. On Windows it still + finds the global `node` through the `Path` that `vfox use --global` wrote, see + [Windows PowerShell 5.1](#windows-powershell-51). The terminal inside the + editor runs your profile, so the hook, a `.vfox.toml` and the `.nvmrc` of the + folder it opens in apply there. +- For a task that has to run on one release, use `vfox exec`. In + `.vscode/tasks.json` that is `"command": "vfox"` with `"args": ["x", + "nodejs@22.23.4", "--", "node", "${file}"]`. diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index ae8131a..532c5a5 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -211,8 +211,10 @@ after `get checksum failed:`: release, see [npm.md](npm.md#global-packages-belong-to-a-release). - **`corepack` is missing.** Node.js 25 and later does not ship it, see [npm.md](npm.md#yarn-pnpm-and-corepack). -- **`.nvmrc` selects nothing.** Check the strategy and what the file holds, see - [nvmrc.md](nvmrc.md#not-finding-it). +- **`.nvmrc` selects nothing.** Check the strategy and what the file holds, and + that the shell did not come from a directory with a version file into one with + none (it keeps the release it had). Release 0.5.3 also does not read the file + after a `cd` from a running shell. See [nvmrc.md](nvmrc.md#not-finding-it). - **`git status` lists `.vfox/`.** vfox links the release into the project as `.vfox/sdks/nodejs`. Ignore `.vfox/` in `.gitignore`, and keep `.vfox.toml`. - **The installed name has no `v`.** Releases are named `24.21.0`, and a `v` in diff --git a/docs/zh-CN/changes.md b/docs/zh-CN/changes.md index 81b0b5d..2c12c9d 100644 --- a/docs/zh-CN/changes.md +++ b/docs/zh-CN/changes.md @@ -8,6 +8,10 @@ - `vfox install nodejs@latest` 重新安装最新的版本,`vfox search nodejs` 也不再列出预发布版本。2026-10-29 起,nodejs.org 的索引里最新的一条是 `27.0.0-alpha.0.0.0`。0.5.3 把它排在最前面,所以 `latest` 解析成它,安装以 `version not found for provided version 27.0.0-alpha.0.0.0` 告终。`nodejs@26` 等按编号安装的写法不受影响。现在只列出由三段数字组成的版本。见 [versions.md](versions.md#安装)。 - 华为云、TUNA、USTC 和北外现在可以用作镜像。0.5.3 把安装包的下载交给 vfox,而 vfox 请求时使用 Go HTTP 客户端的 `User-Agent`,这四个镜像对它返回的是错误页面或者空内容,而不是文件(依次是 HTTP 418、403、204 和 403),安装以 `checksum error` 结束。插件现在让 vfox 下载时使用它自己请求时的 `User-Agent`(vfox 1.0.13 下是 `vfox/1.0.13 vfox-nodejs/0.5.3`),[mirror.md](mirror.md#可用的镜像) 里的所有镜像都能安装 24.21.0。天翼云 `https://mirrors.ctyun.cn/nodejs-release` 也加进了这份列表。 +- 插件从版本索引里学到 LTS 版本线,所以 Node.js 以后再升的版本线,不需要为插件发布新版本。它把学到的内容放在插件旁边的 `.lts-cache` 里。`vfox search`,或者安装 `latest` 或某个大版本时读到索引,以及安装某个确切版本、而这个文件不存在或者已有一天时,它会写这个文件;在 `specified` 和 `latest_installed` 下,`lts/`、`lts` 和 `lts/*` 读这个文件,不发任何请求。在学到任何内容之前,或者文件无法写入的地方,它使用一张代号表,这张表现在到 Lithium(26)为止:Node.js 26 在 2026-10-28 进入长期支持。0.5.3 只有这张表,而且到 Krypton(24)为止,所以在那里 `latest_installed` 下 `lts/lithium` 什么也选不中,`lts` 和 `lts/*` 选中某个 24.x。见 [lts.md](lts.md#插件认识哪些代号)。 +- UTF-16 的 `.nvmrc` 或 `.node-version`,以及带字节顺序标记的 UTF-8 文件,现在可以读取了。Windows PowerShell 5.1 用 `>` 和 `Out-File` 写出前者,用 `-Encoding UTF8` 写出后者。0.5.3 按原样读取字节,所以没有任何版本能匹配这段文本,而 vfox 不会有任何提示。见 [nvmrc.md](nvmrc.md#文件里可以写什么)。 +- `.nvmrc` 和 `.node-version` 在每个提示符出现在它们所在的目录里时都会读取,所以 `cd` 进去就会切换,改动文件则在下一个提示符生效。0.5.3 在 Shell 于该目录里启动时读取,`cd` 之后不读取,因为 vfox 1.0.13 会保留它为 Shell 第一次算出的结果,并且不跟踪版本文件。现在,每当 vfox 请插件读取这样的文件时,插件就会删除那个结果。从这个目录 `cd` 到完全没有配置的目录时还不会切换回来,而 `.vfox.toml` 一直都会。见 [nvmrc.md](nvmrc.md#什么时候读取这个文件)。这个缓存是 vfox 的,已经请 vfox 修复([version-fox/vfox#745](https://github.com/version-fox/vfox/issues/745),这里用 [#46](https://github.com/version-fox/vfox-nodejs/issues/46) 跟踪)。 +- 文档新增了一页使用场景:nvm-sh/nvm 与 vfox 的命令对照、从零开始使用 Windows PowerShell 5.1、全团队使用同一个 Node.js、一个 Shell 里的多个项目、`vfox exec`、镜像和编辑器。见 [scenarios.md](scenarios.md)。 ## 0.5.3(2026-10-20) diff --git a/docs/zh-CN/development.md b/docs/zh-CN/development.md index 7ce7a7f..f31e728 100644 --- a/docs/zh-CN/development.md +++ b/docs/zh-CN/development.md @@ -12,7 +12,8 @@ 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 的 node 被遮蔽时给出提示 hooks/parse_legacy_file.lua .nvmrc 和 .node-version -lib/util.lua 镜像 URL 与下载请求头、musl 检测与缺失的库、运行命令、版本比较与匹配、简写、PATH 探测 +lib/util.lua 镜像 URL 与下载请求头、musl 检测与缺失的库、运行命令、版本比较与匹配、简写、PATH 探测、插件学到的 LTS 版本线 +.lts-cache 不在仓库里:插件学到的 LTS 版本线,写在插件旁边 tests/hooks_test.lua 离线的钩子测试 tests/e2e/ 容器测试,见 e2e.md Injection.lua vfox 注入对象的类型提示,不会被加载 @@ -63,7 +64,9 @@ vfox 加载 `metadata.lua`,命令需要时调用相应的钩子,并向钩子 ## 兼容版本文件 -`ParseLegacyFile` 读取文件,去掉所有空白字符,再把文本交给 `resolve_version(query, strategy)`:它去掉开头的 `v`,转成小写,把 `lts-x` 变成 `lts/x`,把 `lts/` 映射到一个大版本,把 `lts` 和 `lts/*` 映射到 `nodejs_codenames` 里最高的大版本。在 `latest_available` 策略下,代号先从索引里取(`discover_lts`),索引里没找到或请求失败时,回退到代号表。 +`ParseLegacyFile` 首先从会话目录里删掉 `env-state.json`(`util.forget_env_state`)。vfox 把 `vfox env` 的结果保存在那里,并且不跟踪版本文件,没有这一步的话,`cd` 进只有版本文件的目录就注意不到。vfox 在加载那个文件之前就调用这个钩子,这就是这个权宜之计能起作用的原因,也是它在离开目录时帮不上忙的原因:没有东西可读的地方不会调用它。见 [nvmrc.md](nvmrc.md#什么时候读取这个文件)。 + +接着它读取文件并解码(`util.decode_text` 把带标记的 UTF-16 变成 UTF-8,并去掉 UTF-8 的标记,因为 Windows PowerShell 5.1 两种都会写出来)。去掉所有空白字符,再把文本交给 `resolve_version(query, strategy)`:它去掉开头的 `v`,转成小写,把 `lts-x` 变成 `lts/x`,把 `lts/` 映射到一个大版本,把 `lts` 和 `lts/*` 映射到最新的 LTS 版本线的大版本。在 `latest_available` 策略下,代号先从索引里取(`discover_lts`)。其他情况下,以及索引里没找到或请求失败时,由 `util.known_lts_lines` 作答,不发请求:插件从索引里学到的内容,加上内置的 `util.LtsLines`,见[下文](#插件如何学到-lts-版本线)。 接着 `resolve_legacy_version(strategy, query)` 用 `util.version_prefix` 解析查询:`22` 得到 `{ 22 }`,`22.23` 得到 `{ 22, 23 }`,`22.x`、`22.x.x` 和 `22.*` 都得到 `{ 22 }`。不是这类前缀的文本(`node`、`lts/unknown`、`>=20`)原样返回。对于前缀: @@ -73,15 +76,34 @@ vfox 加载 `metadata.lua`,命令需要时调用相应的钩子,并向钩子 `vfox` 随后会选中这个结果,前提是它已安装。面向用户的规则和示例见 [nvmrc.md](nvmrc.md) 和 [lts.md](lts.md)。 +## 插件如何学到 LTS 版本线 + +`ParseLegacyFile` 在带有版本文件的目录里每个提示符都会运行,所以它不能去取 `index.json` 来弄清楚 `lts/*` 是什么。插件在本来就拿到索引的时候学到这些,并把它们放在插件旁边的 `.lts-cache` 里,这是个文本文件: + +```text +# What vfox-nodejs learned from index.json about LTS lines. Safe to delete. +updated 1793000000 +newest 26 +line argon 4 +... +line lithium 26 +``` + +- `Available` 解码出索引之后调用 `util.remember_lts`。`util.lts_summary` 找出每条有名字的版本线的大版本,以及最新的 LTS 大版本(`lts` 为 `true` 的版本,也就是没有名字的版本线,只算进最新的那一项)。它在 vfox 自己对答案的缓存过期时运行。 +- `PreInstall` 在安装拿到校验和之后调用 `util.refresh_lts_cache`,所以像不支持的架构这样提前失败的安装,什么也不会去取。vfox 安装确切的版本时不会去问 `Available`,所以这样的安装靠这里学到。文件不存在或者超过一天,并且文件可以写入时,它才调用 `Available`,这样只读的插件目录就不会让每次安装都白取一次索引。 +- `ParseLegacyFile` 通过 `util.known_lts_lines` 读这个文件,它把学到的内容放在内置的 `util.LtsLines` 之上,`lts` 和 `lts/*` 取两者里最新的大版本。它从不发请求。 +- vfox 没有给插件提供它自己的路径,所以 `util.plugin_dir` 在 `package.path` 里有 `hooks/?.lua` 和 `lib/?.lua` 的地方找到目录。新文件写在旧文件旁边,再重命名覆盖过去。任何失败,无论是没有目录、不可写还是文件损坏,都只会让插件退回那张表。 +- 在仓库里,`package.path` 以 `./lib/?.lua` 开头,所以这个目录会是仓库本身。钩子测试把 `util.lts_cache_path` 指向它们自己的文件。 + ## Node.js 把一条版本线升为 LTS 时 -每年十月,一条偶数版本线会得到代号,见 [lts.md](lts.md#nodejs-升级版本线时)。要让插件认识新的代号: +每年十月,一条偶数版本线会得到代号,见 [lts.md](lts.md#nodejs-升级版本线时)。插件从索引里学到这条版本线,不需要为此发布新版本。下面是顺手要做的事,让还没学到任何内容的插件也认识它,也让文档保持正确: -1. 在 `hooks/parse_legacy_file.lua` 的 `nodejs_codenames` 里加上 ` = `。 -2. 在 `tests/hooks_test.lua` 里,挨着 `legacy lts krypton maps to its major` 加一个用例。 -3. 在 [lts.md](lts.md#插件认识哪些代号) 的表里加一行,并修改 [lts.md](lts.md) 和 [nvmrc.md](nvmrc.md) 里写着“撰写本文时是 24”的句子。 -4. `tests/e2e/common/common.go` 里的 `DefaultLTSVersion` 可以不动:它只要仍在表内即可。 -5. 发布。 +1. 在 `lib/util.lua` 的 `UTIL.LtsLines` 里加上 ` = `。 +2. 在 `tests/hooks_test.lua` 里,挨着 `legacy lts lithium maps to its major` 加一个用例,并修改 `legacy bare lts is the newest line of the table` 所期望的大版本。 +3. 把代号加进 `tests/e2e/common/common.go` 的 `LTSCodenames`,并给 `TestLTSCodename` 加一个用例。E2E 套件会拿这张表和插件根据线上索引写出的文件作比较。 +4. 在 [lts.md](lts.md#插件认识哪些代号) 的表里加一行,并修改 [lts.md](lts.md) 和 [nvmrc.md](nvmrc.md) 里写着“撰写本文时为 26”的句子,以及 [lts.md](lts.md#示例) 里的示例。 +5. `tests/e2e/common/common.go` 里的 `DefaultLTSVersion` 可以不动:它只要仍在表内即可。 ## 测试 @@ -91,9 +113,9 @@ vfox 加载 `metadata.lua`,命令需要时调用相应的钩子,并向钩子 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`、有 `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 版本线(文件是临时的,每个用例开始时都有一个什么版本线也不认识的新文件)、有 `node` 遮蔽 vfox 时的 `PreUse`,以及 `lib/util.lua` 里的辅助函数。改动需要的用例就加在那里。 -gopher-lua 1.1.1 会丢掉闭包对 `local x = nil` 的赋值:外层作用域读到的仍然是 `nil`。要在 `with_util` 或 `pcall` 内部收集值的测试,必须把值放在表的字段里。 +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` 同样生效: @@ -133,8 +155,12 @@ curl -s https://nodejs.org/dist/index.json | jq -r ' - riscv64 和 loong64 只在 Unofficial Builds 上有包,而插件只把它用于 musl。 - `PostInstall` 只在 musl 上运行 `node --version`。在 glibc 上,缺少 `libatomic.so.1`(Node.js 25 及以后)要等到 `node` 运行时才会暴露:README 把系统库留给用户自己处理。 - E2E 的 Alpine 镜像通过依赖 `gcc` 的 `go` 得到 `libatomic`,所以套件没有覆盖不带它的原版 Alpine,钩子测试覆盖了。 -- `.nvmrc` 里的别名(`node`、`stable`、`lts/-1`)、范围和注释都不认识,`lts` 则依据一张手工维护的表,见 [nvmrc.md](nvmrc.md#不认识的写法)。 +- `.nvmrc` 里的别名(`node`、`stable`、`lts/-1`)、范围和注释都不认识,见 [nvmrc.md](nvmrc.md#不认识的写法)。 +- 插件目录不可写,或者插件装好之后从没拿到过索引,`specified` 和 `latest_installed` 就只有内置的表,它到 Lithium 为止。 +- 从带有版本文件的目录 `cd` 到完全没有配置的目录时,不会切换回来。vfox 不跟踪版本文件,并沿用它上一次计算的结果,而没有文件可读的地方不会调用插件,所以 `util.forget_env_state` 只覆盖了进入的方向。修复应当放在 vfox 里:[version-fox/vfox#745](https://github.com/version-fox/vfox/issues/745),这里用 [#46](https://github.com/version-fox/vfox-nodejs/issues/46) 跟踪。等 vfox 跟踪版本文件之后,就去掉 `util.forget_env_state`。 +- 在带有版本文件的目录里,每个提示符都会重新计算一次环境,这是 `util.forget_env_state` 造成的。只装了一个 SDK 时,大约多花一毫秒。 - `latest_available` 在带有兼容版本文件的目录里,每次出现提示符都会下载 `index.json`。 +- UTF-32,以及没有字节顺序标记的 UTF-16,都不会解码。 - `VFOX_NODEJS_MIRROR` 对非官方站点不起作用。 - macOS 没有 E2E 作业。 diff --git a/docs/zh-CN/e2e.md b/docs/zh-CN/e2e.md index 3c38aa6..5421152 100644 --- a/docs/zh-CN/e2e.md +++ b/docs/zh-CN/e2e.md @@ -35,8 +35,9 @@ Windows 容器的覆盖(`tests/e2e/windows`)无法在开发容器里运行 3. 固定的 Node.js 版本安装时会验证校验和,并且能正常运行(`node --version`、`npm --version`、`process.platform`、`process.arch`),因此 `linux-x64` / `linux-arm64` / `win-x64` / `win-arm64` 的包选择是实际检验过的。在 Windows 上,vfox 挂载之后,Git Bash 还会运行 `node`、`npm.cmd` 和不带扩展名的 `npm`,以覆盖 [issue #293](https://github.com/version-fox/vfox/issues/293)。LTS 版本(`LTS_NODE_VERSION`)也会安装,供下面的代号检查使用。 4. `nodejs@latest` 通过插件的 `Available` 列表解析(取第一项,也就是最新的一项),并在该平台的二进制已经发布时完成安装。Alpine ARM64 只在上游对应的非官方 musl 构建缺失期间才跳过这项检查。经由镜像运行时,如果镜像的索引里列出了最新版本却没有它的 `SHASUMS256.txt`(插件会以 `get checksum failed: HTTP 404 from ...` 结束),而 nodejs.org 有这个文件,也会跳过这项检查。这两种情况下,固定版本和 LTS 版本仍会验证。 5. 只写大版本,或只写大版本和小版本的简写,通过 `PreUse` 对照已安装的版本解析。 -6. `.nvmrc` 和 `.node-version` 决定 `vfox env` 选中哪个版本:在默认的 `specified` 策略下,选中文件里固定的那个确切版本;在 `latest_installed` 策略下,LTS 代号(`lts/`,见 [lts](lts.md))会解析为对应的 LTS 版本。每项兼容版本文件的检查都在自己的 Shell 里挂载 vfox,这样选择结果是重新计算出来的,而不是由前一次检查留下的 `vfox env` 状态缓存直接给出。 +6. `.nvmrc` 和 `.node-version` 决定 `vfox env` 选中哪个版本:在默认的 `specified` 策略下,选中文件里固定的那个确切版本;在 `latest_installed` 策略下,LTS 代号(`lts/`,见 [lts](lts.md))会解析为对应的 LTS 版本。在 Windows 上,固定的版本还会以 UTF-16 和带字节顺序标记的 UTF-8 写入文件检查一遍,这是 Windows PowerShell 5.1 会写出的字节。每项兼容版本文件的检查都在自己的 Shell 里挂载 vfox,这样选择结果是重新计算出来的,而不是由前一次检查留下的 `vfox env` 状态缓存直接给出。插件读取兼容版本文件时会删除那个缓存文件,Windows 上的检查依赖这一点,而不是自己去删除它。 7. `vfox search nodejs` 列出已安装的版本,给出的 npm 版本与该二进制自带的 npm 版本相同。 +8. 安装和搜索之后,插件目录里有 `.lts-cache`(见 [lts](lts.md#插件认识哪些代号)),插件那张表里的每个代号都在,大版本也相同,最新的 LTS 大版本不低于 26。索引里有所有这些代号,所以表要是错了,会在这里暴露出来。Windows 容器把 vfox 放在 `C:\Users\John Doe\.vfox` 里,所以在那里,这个文件还说明插件在路径带空格的情况下能找到自己的目录,并且能写入。 默认矩阵是: @@ -74,6 +75,8 @@ go test -v -timeout 55m -run TestE2E ./linux VFOX_VERSION=latest MIRROR=default E2E_MAX_JOBS=1 go test -v -timeout 55m -run TestE2E ./linux ``` +改动 `hooks/` 或 `lib/` 之后要加 `-count=1`。Go 认为测试打开的东西没有变化时,会重放上一次的结果;改过 `lib/util.lua` 之后再跑一次,仍然打印 `ok ... (cached)`,输出和耗时都是第一次的。 + 在原生宿主上,同一条命令也能用,只需要两个前提:Docker Engine(rootful 或 rootless)和 Go 1.27.1,这正是 CI 准备的环境。使用 rootless Docker 的用户要把 Docker 客户端指向自己的 socket(`export DOCKER_HOST="unix://$XDG_RUNTIME_DIR/docker.sock"`);开发容器挂载的 socket 已经替你处理了这种情况。ARM64 只运行默认镜像。 ## Alpine(musl) @@ -97,7 +100,7 @@ Windows 容器无法跨架构模拟,所以 ARM64 套件需要 ARM64 宿主(C 这套测试没有开发容器,因为 Dev Containers 规范拒绝 Windows 容器镜像(见[开发容器](#开发容器)),所以它通过 `tests/e2e/windows/run.ps1` 从 Windows 宿主运行。对宿主的要求是 Windows 容器模式的 Docker;除了 Windows 自带的 `curl.exe` 和 `tar.exe`,脚本不需要别的东西。它会把固定版本的 PowerShell、PortableGit(含 Git Bash 的 Git for Windows)和 Go 下载到按架构区分的缓存里(`$env:E2E_CACHE_DIR`,默认是 `%LOCALAPPDATA%\vfox-nodejs-e2e\`,多次运行之间复用),用这个 Go 构建一次 `vfox@main`,编译套件,然后运行。这些工具都不会被安装,也不会放进用户的 `PATH`,所以不会与 vfox 管理的工具链冲突。 -缓存的工具以只读方式 bind mount 到每个组合容器的 `C:\tools`,所以 `tests/e2e/windows/Dockerfile` 只需要在原版 servercore 上加 `PATH` 条目和插件文件。如果改为把工具构建进镜像层,托管的 runner 上仅提交镜像层就要花几分钟(这些工具共有几万个文件)。拉取 servercore 在全新的 CI runner 上要五到七分钟,它在后台进行,同时获取工具、编译 Go 代码。 +缓存的工具以只读方式 bind mount 到每个组合容器的 `C:\tools`,所以 `tests/e2e/windows/Dockerfile` 只需要在原版 servercore 上加 `PATH` 条目和插件文件。如果改为把工具构建进镜像层,托管的 runner 上仅提交镜像层就要花几分钟(这些工具共有几万个文件)。拉取 servercore 在全新的 CI runner 上要五到七分钟,它在后台进行,同时获取工具、编译 Go 代码。`run.ps1` 会先等待 Docker 引擎应答 `docker version`,最多五分钟。托管的 runner 可能在引擎开始监听之前就开始执行作业:2026-11-03 那次拉取立刻失败了,报 `failed to connect to the docker API at npipe:////./pipe/docker_engine`,作业也随之结束,而在同一个 runner 镜像上,之前的两次运行都通过了。 `windows-11-arm` runner 没有 Windows Hypervisor Platform 功能,所以无法使用 Hyper-V 隔离;进程隔离只需要 Containers 功能。 @@ -162,7 +165,7 @@ pwsh -NoProfile -File .\tests\e2e\windows\run.ps1 ## WSL -WSL 套件(`tests/e2e/wsl/`)在 Windows 宿主上通过 `wsl.exe` 直接驱动 Ubuntu 发行版,不使用容器。它是为了复现 [issue #32](https://github.com/version-fox/vfox-nodejs/issues/32) 而存在的:在 WSL 里,`vfox install` 报告成功,`node` 却不在 `PATH` 上。在共用的流程(带校验和验证的安装、工具链、简写、搜索、兼容版本文件)之外,它还断言 `command -v node` 解析到 `VFOX_HOME` 之下,并且 `vfox env -s bash` 导出了 SDK 的 bin 目录。 +WSL 套件(`tests/e2e/wsl/`)在 Windows 宿主上通过 `wsl.exe` 直接驱动 Ubuntu 发行版,不使用容器。它是为了复现 [issue #32](https://github.com/version-fox/vfox-nodejs/issues/32) 而存在的:在 WSL 里,`vfox install` 报告成功,`node` 却不在 `PATH` 上。在共用的流程(带校验和验证的安装、工具链、简写、搜索、学到的 LTS 版本线、兼容版本文件)之外,它还断言 `command -v node` 解析到 `VFOX_HOME` 之下,并且 `vfox env -s bash` 导出了 SDK 的 bin 目录。 固定的版本默认是 `20.16.0`(issue 里的版本);`NODE_VERSION` 可以覆盖它。LTS 版本(`LTS_NODE_VERSION`,默认值以 `tests/e2e/common` 为准)和其他套件一样,用于 `lts/` 检查。各组合共用一个发行版并依次运行,每个组合使用各自独立的 `HOME`/`VFOX_HOME`,彼此隔离。`MIRROR` 的默认值照常适用;CI 固定 `MIRROR=default`,使运行只覆盖 `latest`/`main` 这两个 vfox 渠道。 diff --git a/docs/zh-CN/lts.md b/docs/zh-CN/lts.md index cfcd48d..f3c4d0b 100644 --- a/docs/zh-CN/lts.md +++ b/docs/zh-CN/lts.md @@ -2,7 +2,7 @@ [中文](lts.md) | [English](../lts.md) -Node.js 中编号为偶数的大版本会成为 **LTS**(长期支持)版本线:奇数版本结束生命周期很久之后,它们仍会继续收到修复,所以大多数生产环境都会固定使用某条 LTS 版本线。每条 LTS 版本线都有一个代号(4 是 `argon`,6 是 `boron`,……,24 是 `krypton`)。 +Node.js 中编号为偶数的大版本会成为 **LTS**(长期支持)版本线:奇数版本结束生命周期很久之后,它们仍会继续收到修复,所以大多数生产环境都会固定使用某条 LTS 版本线。每条 LTS 版本线都有一个代号(4 是 `argon`,6 是 `boron`,……,26 是 `lithium`)。 ## 安装一条 LTS 版本线 @@ -12,18 +12,24 @@ Node.js 中编号为偶数的大版本会成为 **LTS**(长期支持)版本 vfox install nodejs@24 ``` -`vfox search nodejs` 会用 `(LTS)` 标出 LTS 版本线的版本,所以最新的 LTS 版本就是第一个带这个标记的行: +`vfox search nodejs` 会用 `(LTS)` 标出 LTS 版本线的版本,所以最新的 LTS 版本就是第一个带这个标记的行(2026-11-03 的结果): ```text $ vfox search nodejs | grep -m1 '(LTS)' - - 24.22.0 (LTS) [npm 11.19.1] + - 26.13.0 (LTS) [npm 11.20.0] ``` -用 `vfox install nodejs@24.22.0` 安装它,或者用 `nodejs@24` 跟随这条版本线。这个标记来自版本索引的 `lts` 字段,Node.js 从一条版本线进入长期支持的那天起设置它:24.11.0 是 Krypton 中第一个带它的版本,24.10.0 及更早的版本没有。`lts/` 只在 `.nvmrc` 和 `.node-version` 文件里才会被识别,见 [nvmrc.md](nvmrc.md)。 +用 `vfox install nodejs@26.13.0` 安装它,或者用 `nodejs@26` 跟随这条版本线。这个标记来自版本索引的 `lts` 字段,Node.js 从一条版本线进入长期支持的那天起设置它:24.11.0 是 Krypton 中第一个带它的版本,24.10.0 及更早的版本没有。`lts/` 只在 `.nvmrc` 和 `.node-version` 文件里才会被识别,见 [nvmrc.md](nvmrc.md)。 ## 插件认识哪些代号 -在 `latest_available` 策略下,插件从版本索引里动态获取 LTS 代号(`index.json` 在每个版本的 `lts` 字段里写明了代号),所以插件发布之后才出现的代号,在这个策略下仍然有效。其他情况下,以及离线时的回退,用的是 `hooks/parse_legacy_file.lua` 里硬编码的表,因为 `specified` 和 `latest_installed` 必须在没有网络的情况下完成解析。“首个 LTS 版本”和“最后一个版本”取自 2026-10-21 的索引,Krypton 仍在发布新版本: +`index.json` 在每个版本的 `lts` 字段里写明了 LTS 版本线的代号(`"lts": "Lithium"`,不在长期支持内的版本是 `false`)。插件从那里读取代号,而不是靠维护者手工维护的一份清单: + +- 在 `latest_available` 策略下,插件读取文件时向索引查询,也就是带有这个文件的目录里每个提示符都查一次,所以插件发布之后才出现的代号,当天就能用。 +- 在 `specified` 和 `latest_installed` 下,插件同样在每个提示符运行,不能每次都去取索引。它使用上一次拿到索引时学到的内容:`vfox search`,或者安装 `latest` 或某个大版本(比如 `nodejs@26`),向它要版本列表时(vfox 会把答案保留一段时间,所以不是每次搜索都会这样),以及安装某个版本、而这个文件不存在或者已有一天的时候。它把学到的内容放在插件旁边的 `.lts-cache` 里,vfox 自己的 `.available.cache` 也在那里。删掉这个文件就等于让它忘掉,下一次安装时会重新学到。 +- 在学到任何内容之前,或者文件无法写入的地方,它使用下面的表。这张表只是个起点:表里没有的版本线,插件一拿到索引就会学到。 + +“首个 LTS 版本”和“最后一个版本”取自 2026-11-03 的索引,Jod、Krypton 和 Lithium 仍在发布新版本: | 代号 | 大版本 | 首个 LTS 版本 | 最后一个版本 | | --- | --- | --- | --- | @@ -36,22 +42,23 @@ $ vfox search nodejs | grep -m1 '(LTS)' | gallium | 16 | 16.13.0 | 16.20.2 | | hydrogen | 18 | 18.12.0 | 18.20.8 | | iron | 20 | 20.9.0 | 20.20.2 | -| jod | 22 | 22.11.0 | 22.23.3 | -| krypton | 24 | 24.11.0 | 24.22.0 | +| jod | 22 | 22.11.0 | 22.23.4 | +| krypton | 24 | 24.11.0 | 24.22.2 | +| lithium | 26 | 26.13.0 | 26.13.0 | ## Node.js 升级版本线时 -编号为偶数的版本线每年十月进入长期支持,并在那时得到代号。根据[发布计划](https://github.com/nodejs/Release),Node.js 26 定于 2026-10-28 进入长期支持,目前还没有公布代号。 +编号为偶数的版本线每年十月进入长期支持,并在那时得到代号。Node.js 26 在 2026-10-28 就是这样,代号是 Lithium:26.13.0 是索引里第一个带 `lts` 标记的版本。升级版本线在实践中是这样的: -- 在 `latest_available` 下,新代号一出现在索引里,插件当天就能找到。这时 `lts` 和 `lts/*` 指的是新版本线的最新版本。 -- 在 `specified` 和 `latest_installed` 下由上面的表决定,而这张表只有随着插件发布新版本才会改变。在那之前,`lts/` 什么都选不中,`lts` 和 `lts/*` 仍然指表里最高的版本线,撰写本文时为 24。 +- 在 `latest_available` 下,新代号一出现在索引里,插件当天就能找到。这时 `lts` 和 `lts/*` 指的是新版本线的最新版本,`lts/lithium` 在那里当天就能用,0.5.3 也一样。 +- 在 `specified` 和 `latest_installed` 下,插件从升级之后第一次拿到索引起就认识这条版本线:也就是把新版本线带给你的那次 `vfox search` 或者安装。`latest_installed` 在没有安装这条版本线的任何版本之前什么也选不中,所以这已经够早了。在那之前,`lts/` 什么都选不中,`lts` 和 `lts/*` 指的是插件认识的最新的版本线。 - `vfox search` 里的 `(LTS)` 跟随索引,所以会立刻变化。 -这张表在 `hooks/parse_legacy_file.lua` 里,见 [development.md](development.md#nodejs-把一条版本线升为-lts-时)。 +未发布:0.5.3 只有那张表,没有这个文件。这张表发布时 Lithium 还没有出现,到 Krypton 为止,所以 2026-11-03,已安装 22.23.4、24.22.2 和 26.13.0 时,它在 `latest_installed` 下 `lts/*` 选中 24.22.2,`lts/lithium` 什么也选不中,要让它认识一条版本线,只有发布插件的新版本。插件如何学到这些,见 [development.md](development.md#插件如何学到-lts-版本线)。 ## 示例 -假设有一个 `.nvmrc`(`.node-version` 同理),并且已安装 `22.23.3` 和 `24.21.0`。示例 1 到 4 都在 `latest_installed` 策略下运行,这个策略由示例 1 设置,各个策略见 [nvmrc.md](nvmrc.md#由策略决定): +假设有一个 `.nvmrc`(`.node-version` 同理),并且已安装 `22.23.4`、`24.21.0` 和 `26.13.0`,情形是 2026-11-03 的。示例 1 到 4 都在 `latest_installed` 策略下运行,这个策略由示例 1 设置,各个策略见 [nvmrc.md](nvmrc.md#由策略决定): ```sh # 1. LTS 代号 -> 大版本,再由策略选出具体的版本。 @@ -59,34 +66,37 @@ $ cat .nvmrc lts/jod $ vfox config legacyVersionFile.strategy latest_installed $ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version -v22.23.3 +v22.23.4 ``` ```sh -# 2. 单独的 `lts`(或 `lts/*`)表示“映射表里最高的大版本”, -# 撰写本文时为 24。 +# 2. 单独的 `lts`(或 `lts/*`)表示“插件认识的最新的 LTS 版本线”, +# 撰写本文时为 26(未发布:0.5.3 里是 24)。 $ printf 'lts\n' > .nvmrc $ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version -v24.21.0 +v26.13.0 ``` ```sh -# 3. 写全版本号时,完全不经过代号表。 -$ printf '22.23.3\n' > .nvmrc +# 3. 写全版本号时,不需要代号。 +$ printf '22.23.4\n' > .nvmrc $ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version -v22.23.3 +v22.23.4 ``` ```sh -# 4. 当前的 LTS 版本线也是同样的做法。 +# 4. 当前的 LTS 版本线也是同样的做法,它的上一条也一样。 +$ printf 'lts/lithium\n' > .nvmrc +$ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version +v26.13.0 $ printf 'lts/krypton\n' > .nvmrc $ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version v24.21.0 ``` ```sh -# 5. 在 latest_available 下会查询索引,所以连比上面的表更新的代号 -# 也能解析(离线时回退到这张表)。 +# 5. 在 latest_available 下会查询索引,所以连插件还没有学到的代号 +# 也能解析(离线时回退到插件学到的内容,再回退到上面的表)。 $ vfox config legacyVersionFile.strategy latest_available $ printf 'lts/\n' > .nvmrc $ cd && eval "$(vfox activate bash)" && eval "$(vfox env -s bash)" && node --version @@ -97,5 +107,5 @@ $ vfox config legacyVersionFile.strategy specified 示例 1 是怎样一步步解析出来的(`latest_installed`): ``` -.nvmrc "lts/jod" --resolve_version--> "22" --prefix-match installed--> "22.23.3" +.nvmrc "lts/jod" --resolve_version--> "22" --prefix-match installed--> "22.23.4" ``` diff --git a/docs/zh-CN/nvmrc.md b/docs/zh-CN/nvmrc.md index 92738d4..9914fe9 100644 --- a/docs/zh-CN/nvmrc.md +++ b/docs/zh-CN/nvmrc.md @@ -19,7 +19,7 @@ cd my-project # 里面有上面那个 .nvmrc node -v # v22.23.3 ``` -vfox 挂载到 Shell 之后,进入目录时会读取这个文件,并选中其中指定的版本,不需要 `vfox use`。挂载由 vfox 自己完成,zsh、fish、PowerShell 等其他 Shell 的挂载方式见它的[快速入门](https://vfox.dev/zh-hans/guides/quick-start.html)。 +vfox 挂载到 Shell 之后,每个提示符出现在这个目录里时都会读取这个文件,并选中其中指定的版本,所以 `cd` 进这个目录就会切换,改动文件也一样。不需要 `vfox use`。未发布:0.5.3 在 Shell 于这个目录里启动时读取,在已经运行的 Shell 里 `cd` 进来则不读取,上面的示例打印的是全局版本,见[下文](#什么时候读取这个文件)。挂载由 vfox 自己完成,zsh、fish、PowerShell 等其他 Shell 的挂载方式见它的[快速入门](https://vfox.dev/zh-hans/guides/quick-start.html)。 - 只读取当前目录下的文件。进入项目的子目录就找不到它了,所以 `cd packages/app` 之后会回到全局版本。 - 同一目录里两个文件都有时,`.node-version` 优先。 @@ -29,6 +29,48 @@ vfox 挂载到 Shell 之后,进入目录时会读取这个文件,并选中 - 版本必须已经安装。没有任何已安装的版本匹配时,这个文件会被忽略,也不会有任何提示。`vfox list nodejs` 可以看到已有哪些。 - `vfox config legacyVersionFile.enable false` 会关闭所有兼容版本文件,对每个插件都有效。默认值是 `true`。 +## 什么时候读取这个文件 + +vfox 在 Shell 出现第一个提示符时算出它的环境,并把结果保存在会话目录的 `env-state.json` 里。只有当前目录的 `.vfox.toml` 与上次不是同一个、`PATH` 与上次不同,或者 vfox 的某个配置文件更新了(`vfox use` 就会造成这一点),它才会重新计算。`.nvmrc` 或 `.node-version` 不在其中(vfox 1.0.13,以及 2026-11-03 的 vfox `main`),所以 vfox 注意不到 Shell 进入了带有这类文件的目录。 + +未发布:插件弥补了这一点。vfox 在查看保存的结果之前,会先运行插件的 `ParseLegacyFile`,插件就在这里删掉 `env-state.json`(`util.forget_env_state`),于是在带有这类文件的目录里,vfox 在每个提示符都会重新算一遍。`cd` 进这个目录会切换,从一个这样的目录 `cd` 到另一个也会,改动文件则在下一个提示符生效。多算这一次,在只装了一个 SDK 时每个提示符大约多花一毫秒,`--debug` 里显示为 `Config changed: true`。这是针对 vfox 的权宜之计,等 vfox 跟踪这些文件之后就该去掉。 + +从这个目录 `cd` 到完全没有配置的目录时,它帮不上忙。插件在那里不会被调用,所以 vfox 沿用它为你离开的那个目录算出的结果(`Using cached output`),Shell 也就停在那个版本上。带有 `.vfox.toml` 的目录,或者带有它自己的版本文件的目录,会切换,新开的 Shell 也一样。 + +0.5.3 不会删除这个文件。它在 Shell 于该目录里启动时读取 `.nvmrc`,执行 `vfox use` 之后会再读一次,而 `cd` 之后不读,进出都一样。下面是在 vfox 1.0.13 里,同一个 Shell 一步一步执行时 `node -v` 打印的结果,全局版本是 26.13.0(bash 5.3,PowerShell 7.6.9 的结果一致): + +| 步骤 | 0.5.3 | 未发布 | +| --- | --- | --- | +| 在 `.nvmrc` 写着 18.20.8 的目录里启动一个 Shell | v18.20.8 | v18.20.8 | +| 在完全没有配置的目录里启动一个 Shell | v26.13.0 | v26.13.0 | +| `cd` 进有 18.20.8 那个 `.nvmrc` 的目录 | v26.13.0 | v18.20.8 | +| `cd` 进 `.node-version` 写着 24.22.2 的目录 | v26.13.0 | v24.22.2 | +| 把那个 `.node-version` 改成 22.23.4 | v26.13.0 | v22.23.4 | +| `cd` 进完全没有配置的目录 | v26.13.0 | v22.23.4 | +| `cd` 进 `.vfox.toml` 写着 18.20.8 的目录 | v18.20.8 | v18.20.8 | +| `cd` 进完全没有配置的目录 | v26.13.0 | v26.13.0 | + +第一行是单独的一个 Shell,其余七行是在第二个 Shell 里依次执行的。`vfox --debug env -s bash` 能看到“未发布”这一列的两种情形。在带有 `.nvmrc` 的目录里,插件解析了这个文件,vfox 重新计算: + +```text +Parsing legacy file /home/me/projects/legacy-api/.nvmrc +CallFunction: ParseLegacyFile +Found nodejs@18.20.8 +Config changed: true, configPaths: map[global:/home/me/.vfox/.vfox.toml] +CallFunction: EnvKeys +``` + +`cd` 进没有配置的目录之后的第一个提示符,插件无事可读: + +```text +Config changed: false, configPaths: map[global:/home/me/.vfox/.vfox.toml] +Using cached output +``` + +0.5.3 在带有 `.nvmrc` 的目录里,在 `Found nodejs@18.20.8` 之后打印的也是第二段。`.vfox.toml` 在两边都是被跟踪的,所以在发布版里想要 `cd` 时自动切换,用 `vfox use --project nodejs@18.20.8`。见 [scenarios.md](scenarios.md#一个-shell-里的多个项目)。 + +这个缓存是 vfox 的,修复也要由 vfox 来做。它已经作为 [version-fox/vfox#745](https://github.com/version-fox/vfox/issues/745) 报告,在有发布版带上修复之前,这里用 [#46](https://github.com/version-fox/vfox-nodejs/issues/46) 跟踪。等 vfox 自己能注意到版本文件之后,这个应对办法就可以去掉。 + ## 由策略决定 文件里的文本能指明什么,取决于 vfox 的设置 `legacyVersionFile.strategy`: @@ -47,23 +89,26 @@ vfox config legacyVersionFile.strategy latest_installed ## 文件里可以写什么 -插件在处理文本之前,会先去掉所有空白,去掉开头的 `v`,把其余部分转成小写,并把 `lts-jod` 改成 `lts/jod`。所以 `v22.23.3`、CRLF 换行的文件,以及版本前后的空白,都没有问题。`lts/jod` 这样的名字,会按 [lts.md](lts.md) 里的表变成它所在版本线的大版本(22);`latest_available` 除外,它会向索引查询带有这个代号的最新版本。版本号末尾可以用 `x`、`X` 或 `*` 代替不作限定的部分:`22.x`、`22.x.x` 和 `22.*` 都表示 22。 +插件在处理文本之前,会先对它解码,去掉所有空白,去掉开头的 `v`,把其余部分转成小写,并把 `lts-jod` 改成 `lts/jod`。所以 `v22.23.3`、CRLF 换行的文件,以及版本前后的空白,都没有问题。`lts/jod` 这样的名字,会变成它所在版本线的大版本(22),这是插件从索引里知道的,见 [lts.md](lts.md);`latest_available` 除外,它会向索引查询带有这个代号的最新版本。版本号末尾可以用 `x`、`X` 或 `*` 代替不作限定的部分:`22.x`、`22.x.x` 和 `22.*` 都表示 22。 + +解码的意思是,UTF-16 的文件,或者带字节顺序标记的 UTF-8 文件,读起来和纯文本一样。Windows PowerShell 5.1 的 `>`、`Out-File` 和 `-Encoding UTF8` 写出的就是这样的文件,见 [scenarios.md](scenarios.md#在-windows-powershell-51-里写这个文件)。未发布:0.5.3 按原样读取字节,所以标记或者 UTF-16 的零字节会成为文本的一部分,没有任何版本能匹配它,而 vfox 不会有任何提示。 -下面是 2026-10-21 在已安装 20.20.2、22.23.3、24.21.0、25.9.0 和 26.12.0 的情况下得到的结果,此时 24.22.0 是最新的 24.x,22.23.3 是最新的 22.x: +下面是 2026-11-03 在已安装 20.20.2、22.23.4、24.21.0、25.9.0 和 26.13.0 的情况下得到的结果,此时 24.22.2 是最新的 24.x,22.23.4 是最新的 22.x: | 文件里写 | `specified` | `latest_installed` | `latest_available` | | --- | --- | --- | --- | -| `22.23.3`、`v22.23.3` | 22.23.3 | 22.23.3 | 22.23.3 | -| `22`、`v22`、`22.x`、`22.*` | 无 | 22.23.3 | 22.23.3 | -| `22.23` | 无 | 22.23.3 | 22.23.3 | -| `lts/jod`、`lts-jod`、`LTS/Jod` | 无 | 22.23.3 | 22.23.3 | -| `lts/krypton` | 无 | 24.21.0 | 无:最新的是 24.22.0,而它没有安装 | -| `lts/*`、`lts` | 无 | 24.21.0 | 无:最新的 LTS 版本是 24.22.0,而它没有安装 | +| `22.23.4`、`v22.23.4` | 22.23.4 | 22.23.4 | 22.23.4 | +| `22`、`v22`、`22.x`、`22.*` | 无 | 22.23.4 | 22.23.4 | +| `22.23` | 无 | 22.23.4 | 22.23.4 | +| `lts/jod`、`lts-jod`、`LTS/Jod` | 无 | 22.23.4 | 22.23.4 | +| `lts/krypton` | 无 | 24.21.0 | 无:最新的是 24.22.2,而它没有安装 | +| `lts/lithium` | 无 | 26.13.0 | 26.13.0 | +| `lts/*`、`lts` | 无 | 26.13.0 | 26.13.0 | | `18.20.8` 或任何没有安装的版本 | 无 | 无 | 无 | `latest_available` 不会回退到较旧的已安装版本。索引里列出的版本比你已安装的更新时,什么都不会选中。想选中已安装的最新版本,请用 `latest_installed`。 -`lts` 和 `lts/*` 指最新的 LTS 版本线。在 `specified` 和 `latest_installed` 下,指的是代号表里最高的大版本(撰写本文时为 24);在 `latest_available` 下,指的是索引里带有任意代号的最新版本。代号表,以及 Node.js 把一条新版本线升为 LTS 时会发生什么,见 [lts.md](lts.md)。 +`lts` 和 `lts/*` 指最新的 LTS 版本线。在 `specified` 和 `latest_installed` 下,指的是插件认识的最新的版本线(撰写本文时为 26);在 `latest_available` 下,指的是索引里带有任意代号的最新版本。插件如何认识版本线,以及 Node.js 把一条新版本线升为 LTS 时会发生什么,见 [lts.md](lts.md)。未发布:0.5.3 只认识一张到 Krypton(24)为止的表,因为它发布时 Node.js 26 还不是 LTS 版本线。在那里,`latest_installed` 下 `lts/lithium` 什么也选不中,`lts` 和 `lts/*` 在上面的表里选中 24.21.0。 ## 不认识的写法 @@ -74,6 +119,7 @@ vfox config legacyVersionFile.strategy latest_installed - 版本范围:`>=20`、`^22.0.0`、`~22.23`,以及 `x` 后面还有数字的写法(`22.x.1`)。 - 注释。单独一行的 `# pin`,或者 `22.23.3 # pin`,会成为文本的一部分,因为读取文本之前已经去掉了所有空白。 - 空文件。 +- UTF-32 的文件,或者没有字节顺序标记的 UTF-16 文件。只有标记才能告诉插件这个文件是 UTF-16,没有它,零字节就会留在文本里。 ## 前缀怎样匹配 @@ -87,12 +133,14 @@ vfox config legacyVersionFile.strategy latest_installed 版本没有切换时: - vfox 没有挂载到当前的 Shell。`eval "$(vfox activate bash)"` 要在当前 Shell 里执行过,只在别处执行过不算。 +- 仅限 0.5.3:你 `cd` 进这个目录时,Shell 已经在运行,见[什么时候读取这个文件](#什么时候读取这个文件)。请在那里打开一个新终端,或者安装 `main` 的插件([source-build.md](source-build.md))。 +- Shell 从带有版本文件的目录进入了完全没有配置的目录,它会保持离开的那个目录的版本。请打开一个新终端。 - 当前目录是文件所在目录的子目录。 - 版本没有安装,用 `vfox install nodejs@22.23.3` 安装。 - 策略是 `specified`,而文件里写的是 `22` 或 `lts/jod`。 - 在 `latest_available` 下,已经发布了比已安装版本更新的版本。 - 被 `.vfox.toml` 或 `.tool-versions` 挡住了,或者 `legacyVersionFile.enable` 是 `false`。 -- 文本属于上面列出的不认识的写法,或者里面带有注释。 +- 文本属于上面列出的不认识的写法,或者里面带有注释,或者文件的编码是插件读不了的(0.5.3:只能读纯 ASCII 文本)。 `vfox --debug env -s bash` 会打印插件返回了什么,以及 vfox 怎样处理它: @@ -105,4 +153,4 @@ SDK nodejs@22 from project scope not installed, trying lower-priority config ## 与 nvm 的区别 -`nvm install` 会安装 `.nvmrc` 里指明的版本,不带参数的 `nvm use` 会读取它。vfox 只在已安装的版本里选择,安装要用 `vfox install nodejs@`。`.nvmrc` 里的一行不一定是 `vfox install` 接受的写法:`22.23.3` 可以,`22` 可以,`lts/jod` 不行(见 [lts.md](lts.md#安装一条-lts-版本线))。 +`nvm install` 会安装 `.nvmrc` 里指明的版本,不带参数的 `nvm use` 会读取它。vfox 只在已安装的版本里选择,安装要用 `vfox install nodejs@`。`.nvmrc` 里的一行不一定是 `vfox install` 接受的写法:`22.23.3` 可以,`22` 可以,`lts/jod` 不行(见 [lts.md](lts.md#安装一条-lts-版本线))。nvm 在你输入命令时读取这个文件,vfox 则在每个提示符出现在该目录里时读取,见[上文](#什么时候读取这个文件)。并排的命令见 [scenarios.md](scenarios.md#从-nvm-迁移)。 diff --git a/docs/zh-CN/platforms.md b/docs/zh-CN/platforms.md index ab26d17..4a27ebe 100644 --- a/docs/zh-CN/platforms.md +++ b/docs/zh-CN/platforms.md @@ -73,6 +73,7 @@ E2E 套件里没有 macOS 作业,见[下文](#ci-覆盖什么)。钩子测试 - 用户主目录里带空格(`C:\Users\John Doe`)时,需要 vfox 1.0.13 及以后的版本([vfox#712](https://github.com/version-fox/vfox/pull/712))。E2E 套件对每种组合都用这样的主目录跑一遍。 - 在 Git Bash 里,vfox 挂载到 Shell 之后,`node`、`npm.cmd` 和不带扩展名的 `npm` 都能运行([vfox#293](https://github.com/version-fox/vfox/issues/293))。 - `VFOX_NODEJS_MIRROR` 从运行 vfox 的进程的环境里读取。在 PowerShell 里用 `$env:VFOX_NODEJS_MIRROR = '...'` 为当前会话设置,见 [mirror.md](mirror.md#怎样设置)。 +- 从零开始使用 Windows PowerShell 5.1(执行策略、配置文件、`.nvmrc` 的编码),见 [scenarios.md](scenarios.md#windows-powershell-51)。 ## vfox 自己支持的平台 @@ -90,4 +91,4 @@ vfox 1.0.13 提供这些平台的二进制:Linux 的 x86_64、aarch64、armv7 | Windows 容器,amd64 和 arm64 | `windows-latest`、`windows-11-arm` | | WSL 上的 Ubuntu 24.04 | `windows-2025` | -macOS、32 位系统、ppc64le 和 s390x 没有覆盖。钩子测试(`tests/hooks_test.lua`)离线运行,检查以下平台的包名:`windows` 的 amd64、386 和 arm64,`linux` 的 amd64 和 arm,musl 的 amd64 和 arm64,以及 `darwin` 的 arm64。 +macOS、32 位系统、ppc64le 和 s390x 没有覆盖,Windows PowerShell 5.1(`powershell.exe`)也没有:Windows 作业驱动的是 PowerShell 7(`pwsh`),Windows PowerShell 只用来构建它们的镜像。钩子测试(`tests/hooks_test.lua`)离线运行,检查以下平台的包名:`windows` 的 amd64、386 和 arm64,`linux` 的 amd64 和 arm,musl 的 amd64 和 arm64,以及 `darwin` 的 arm64。 diff --git a/docs/zh-CN/scenarios.md b/docs/zh-CN/scenarios.md new file mode 100644 index 0000000..d3a9f2b --- /dev/null +++ b/docs/zh-CN/scenarios.md @@ -0,0 +1,255 @@ +# 使用场景 + +[中文](scenarios.md) | [English](../scenarios.md) + +这个插件给 vfox 提供的,正是 [nvm-sh/nvm](https://github.com/nvm-sh/nvm) 给 POSIX Shell 提供的东西:多个 Node.js 版本并存,可以为一个 Shell、一个项目选定其中一个,或者设为默认,并且能读取 `.nvmrc` 文件。nvm-sh/nvm 是一组 Shell 函数。它的 README 列出的是 Unix、macOS 和 WSL 上的 sh、dash、ksh、zsh 和 bash,另外在某些情况下也可以用于 Git Bash 和 Cygwin,其他 Windows 用户则被指向别的工具。PowerShell 不在这个列表里。vfox 是一个程序,为每种 Shell 都提供挂载脚本,所以同样的事情既可以在 Windows 10 和 11 自带的 Windows PowerShell 5.1 里做,也可以在 PowerShell 7、bash、zsh 和 fish 里做。 + +下面每个场景都写明你想做什么、要输入什么、会看到什么。输出取自 2026-11-03 的 Linux x64,使用 vfox 1.0.13 和 `main` 上的本插件,主目录一律写成 `/home/me`。PowerShell 的命令在 Linux 上的 PowerShell 7.6.9 里运行过,只有 Windows 独有的除外:`Set-ExecutionPolicy`,以及写入用户级环境变量的 `[Environment]::SetEnvironmentVariable`。Windows PowerShell 5.1 本身不在测试套件里(见 [platforms.md](platforms.md#ci-覆盖什么)),所以凡是依据 PowerShell 的文档或 vfox 的源码、而没有在那里实际运行过的内容,场景里都会注明。 + +## 从 nvm 迁移 + +| nvm-sh/nvm | vfox | 说明 | +| --- | --- | --- | +| `nvm install 22` | `vfox install nodejs@22` | 最新的 22.x,并对照 SHA-256 校验和检查。见 [versions.md](versions.md) | +| `nvm install node` | `vfox install nodejs@latest` | 未发布:0.5.3 在索引里有预发布版本时会失败,2026-10-29 起就是这样。`nodejs@26` 在哪里都能用 | +| `nvm install --lts`、`nvm install 'lts/*'` | `vfox install nodejs@26` | 没有 `lts` 这个名字,请写明版本线。2026-11-03 时 26 是最新的 LTS 版本线,`vfox search nodejs \| grep -m1 '(LTS)'` 会显示最新的 LTS 版本。见 [lts.md](lts.md) | +| `nvm use 22` | `vfox use nodejs@22` | 只对当前 Shell 有效。`22` 是已安装的最新的 22.x | +| `nvm alias default 22` | `vfox use --global nodejs@22` | 之后的每个新 Shell | +| `nvm ls` | `vfox list nodejs` | | +| `nvm ls-remote` | `vfox search nodejs` | 所有版本及其 npm | +| `nvm current` | `vfox current nodejs` | | +| `nvm exec 22.23.4 node app.js`、`nvm run 22.23.4 app.js` | `vfox exec nodejs@22.23.4 -- node app.js` | 要写完整的版本号。见[下文](#用指定版本运行一条命令) | +| `nvm uninstall 22.23.4` | `vfox uninstall nodejs@22.23.4` | 要写完整的版本号 | +| `nvm install-latest-npm` | `npm install -g npm@latest` | 只对正在使用的版本有效。见 [npm.md](npm.md#更新-npm) | +| `nvm install --reinstall-packages-from=20 22` | 没有 | 全局包属于某个版本。见 [npm.md](npm.md#全局包属于某个版本) | +| `.nvmrc` | `.nvmrc`、`.node-version`、`.vfox.toml` | 见 [nvmrc.md](nvmrc.md) | +| `NVM_NODEJS_ORG_MIRROR` | `VFOX_NODEJS_MIRROR` | 见 [mirror.md](mirror.md) | + +和 nvm 教给你的习惯相比,有四点不同: + +- `vfox use` 要修改当前 Shell 的环境,只有 vfox 的挂载才能做到(`eval "$(vfox activate bash)"`,或者 [Windows PowerShell 5.1](#windows-powershell-51) 里的那行配置)。没有挂载时,`vfox use` 在 Linux 和 macOS 上会停下来,报 `vfox requires hook support. Please ensure vfox is properly initialized with 'vfox activate'`。`vfox install` 和 `vfox exec` 不需要挂载。 +- 没有不带参数的 `nvm use`,也没有会读取 `.nvmrc` 的 `nvm install`。vfox 自己读取 `.nvmrc` 和 `.node-version`,只在已安装的版本里选择,什么也不安装。它什么时候读取,正是[一个 Shell 里的多个项目](#一个-shell-里的多个项目)要说的。 +- 没有别名。`lts`、`node` 和 `stable` 都不是 `vfox install` 或 `vfox use` 认识的名字。只有 `.nvmrc` 文件认识 `lts/*` 和 `lts/`,选中什么还取决于一项设置,见 [nvmrc.md](nvmrc.md#由策略决定)。 +- 全局包留在它所属的版本里,vfox 不会把它们复制到下一个版本。 + +## Windows PowerShell 5.1 + +你有一台 Windows 电脑和 `powershell.exe`,也就是 Windows PowerShell 5.1,而项目的 README 里写着 `nvm use 22`。在那里 `nvm` 不是一个命令。装好 vfox 之后(它的[快速入门](https://vfox.dev/zh-hans/guides/quick-start.html)列出了 Scoop、winget 和安装程序),四步就能得到同样的工作流程。 + +1. **让 PowerShell 运行你的配置文件。** 在没有人设置过执行策略的 Windows 客户端上,Windows PowerShell 5.1 用的是 `Restricted`,它也会拦住 `$PROFILE`([about_Execution_Policies](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_execution_policies)),而挂载就放在配置文件里。`CurrentUser` 范围不需要管理员权限: + + ```powershell + Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned + ``` + + 组策略设置的策略会覆盖它。如果有的话,`Get-ExecutionPolicy -List` 会把它列为 `MachinePolicy` 或 `UserPolicy`。 + +2. **把 vfox 挂载到 PowerShell。** 这是 vfox 快速入门里的那一行: + + ```powershell + if (-not (Test-Path -Path $PROFILE)) { New-Item -Type File -Path $PROFILE -Force }; Add-Content -Path $PROFILE -Value 'Invoke-Expression "$(vfox activate pwsh)"' + ``` + + Windows PowerShell 5.1 和 PowerShell 7 读取的是不同的配置文件,所以你用到哪个,就在哪个里面执行一次。`vfox activate pwsh` 打印的挂载脚本(vfox 1.0.13)是普通的 PowerShell:它包装了 `prompt` 函数,设置 `$env:PATH` 之类的环境变量,其中没有任何东西需要 PowerShell 7。 + +3. **打开一个新窗口**,安装 Node.js: + + ```powershell + vfox add nodejs + vfox install nodejs@24 + vfox use --global nodejs@24 + node -v + ``` + +4. **按上面的对照表使用它。** `vfox use` 的作用域要明确写出来:`--global`、`--project` 或 `--session`(`-g`、`-p`、`-s`)。 + +在 Windows 上有哪些不同: + +- `vfox use --global` 还会把全局版本的链接写进你这个用户的 `Path`(`HKEY_CURRENT_USER\Environment`),并通知正在运行的程序,同时打印 `Applied global environment variables to Windows registry.`(vfox 1.0.13,来自它的源码)。之后启动的程序,比如 cmd.exe 或者从开始菜单打开的编辑器,不运行任何挂载也能找到同一个 `node`。之前就已经打开的终端和程序,仍然用它们启动时的 `Path`。 +- 一切都只属于当前用户。各个版本在 `%USERPROFILE%\.vfox` 里,vfox 用 junction 链接它们,junction 不需要管理员权限,它修改的 `Path` 也是你自己的(同样来自 vfox 的源码)。 +- vfox 的文档把不带作用域的 `vfox use` 列为:Windows 上是全局,其他系统上是会话。在没有挂载的 Shell 里没有会话,这时 vfox 不像在 Linux 上那样停下来,而是这样提示: + + ```text + Warning: The current shell lacks hook support. Switching to global scope automatically. + ``` + + 如果 `vfox use --session` 打印了这一行,说明配置文件没有运行:要么是第 1 步的执行策略,要么是挂载那一行写在了另一个 PowerShell 的配置文件里。 +- 只有 `PATH` 里的第一个 `node` 会运行。nodejs.org 的安装程序、winget 或 Scoop 装的,或者另一个版本管理器的,都可能排在 vfox 前面。这时 `vfox use` 会打印一条提示,指出是哪一个,并说明它排在 vfox 前面,见 [pnpm.md](pnpm.md)。请卸载另一个。 +- Windows PowerShell 5.1 用 `>` 写出的 `.nvmrc` 是 UTF-16。未发布:0.5.3 读不了它,见[下文](#在-windows-powershell-51-里写这个文件)。 + +## 全团队使用同一个 Node.js + +有两个文件可以说明一个项目要哪个版本。按团队里的人怎么用来选。 + +**所有人都用 vfox:`.vfox.toml`。** `vfox use --project nodejs@22.23.4` 会写出它,修改它也用 `--project`: + +```toml +[tools] +nodejs = "22.23.4" +``` + +仓库里有 `.gitignore` 时,vfox 会把 `.vfox/` 加进去,并提示 `Added '.vfox/' to .gitignore`。没有的话,`git status` 会列出 `.vfox/` 和 `.vfox.toml`,需要你自己忽略 `.vfox/`。把 `.vfox.toml` 提交上去。新来的同事执行下面的命令,装上它指定的版本: + +```text +$ vfox install --all -y +Install the following plugins and SDKs: +SDK: + nodejs@22.23.4 + [1/1] SDK: nodejs@22.23.4 installing... + [1/1] Installation completed. +Green indicates successful installation, while red indicates installation failure. +SDK: + nodejs@22.23.4 +$ vfox install --all -y +All plugins and SDKs are already installed +``` + +(转动的进度提示行省略了。不带 `-y` 时 vfox 会先询问,在脚本里则会停下来,报 `Use the -y flag to automatically confirm installation in non-interactive environments`。) + +**团队里有一部分人用 nvm-sh/nvm:`.nvmrc`。** 保留他们已有的文件。`.node-version` 的用法相同。vfox 的默认设置下,只有写全的版本号(`22.23.4`)才能选中版本,而 `22` 或 `lts/*` 需要 `latest_installed`,见 [nvmrc.md](nvmrc.md#由策略决定)。vfox 不会安装文件里指明的版本,所以要自己装一次: + +```bash +vfox install "nodejs@$(tr -d '[:space:]' < .nvmrc)" +``` + +```powershell +vfox install "nodejs@$((Get-Content .nvmrc -TotalCount 1).Trim())" +``` + +`v22.23.4` 可以,因为 vfox 会去掉 `v`;`22` 也可以,它会安装最新的 22.x。`lts/*` 和 `.nvmrc` 里的其他名字则不行:安装会以 `version not found for provided version lts/*` 结束。 + +每个提示符出现在项目目录里时都会应用 `.nvmrc`,所以 `cd` 进去就会切换。未发布:0.5.3 在 Shell 于该目录里启动时应用它,比如编辑器在这个文件夹里打开的终端;从已经运行的 Shell 里 `cd` 进来则不应用。离开这个目录进入完全没有配置的目录时,两边都不会切换回来,见[一个 Shell 里的多个项目](#一个-shell-里的多个项目)。如果仓库里不该出现 vfox 的文件,`vfox use --project` 仍然可以让你自己得到一个 `.vfox.toml`,它在每个版本里、两个方向上都会切换。把 `.vfox.toml` 和 `.vfox/` 写进 `.git/info/exclude`,再执行 `vfox config gitignore.enable false`(对所有项目生效),这样 vfox 就不会把 `.vfox/` 加进仓库所跟踪的 `.gitignore`。 + +### 在 Windows PowerShell 5.1 里写这个文件 + +Windows PowerShell 5.1 的 `>`、`>>` 和 `Out-File` 写出 UTF-16,`-Encoding UTF8` 写出带字节顺序标记的 UTF-8,不带 `-Encoding` 的 `Set-Content` 写的是 ANSI 代码页,对版本号来说就是纯文本([about_Character_Encoding](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_character_encoding))。插件在读取文本之前,会先对 UTF-16 解码并去掉标记。未发布:0.5.3 按原样读取字节,所以标记或者 UTF-16 的零字节会成为文本的一部分,没有任何版本能匹配它,而 vfox 对此不会有任何提示。下表给出每种文件开头的字节,以及已安装 22.23.4、使用默认策略时,带有该文件的目录选中了什么: + +| 文件开头的字节 | Windows PowerShell 5.1 用什么写出 | 0.5.3 | 未发布 | +| --- | --- | --- | --- | +| `32 32 2e 32 33 2e 34 0d 0a` | `Set-Content .nvmrc 22.23.4 -Encoding ascii`,或者 `Set-Content .nvmrc 22.23.4` | 22.23.4 | 22.23.4 | +| `ef bb bf 32 32 2e ...` | `Set-Content .nvmrc 22.23.4 -Encoding UTF8` | 无 | 22.23.4 | +| `ff fe 32 00 32 00 2e 00 ...` | `'22.23.4' > .nvmrc`、`'22.23.4' \| Out-File .nvmrc` | 无 | 22.23.4 | + +这些字节是在 Linux 上写出的,两个版本的插件都在那里试过。Windows PowerShell 写出什么,依据的是微软的文档。PowerShell 7 对上面这些写法都写出不带标记的 UTF-8,每个版本都能读。下面这样写在哪里都可以: + +```powershell +Set-Content -Path .nvmrc -Value '22.23.4' -Encoding ascii +``` + +## 一个 Shell 里的多个项目 + +一个用 Node.js 18 的旧 API,一个用 24 的 Web 应用,其余的都用全局的 26。在挂载了 vfox 的 Shell 里,在它们之间 `cd`。下面是同一个 Shell 里每一步 `node -v` 的输出,已安装 18.20.8、24.22.2 和 26.13.0,全局版本是 26.13.0(bash 5.3,PowerShell 7.6.9 的结果一致): + +| 步骤 | 0.5.3 | 未发布 | +| --- | --- | --- | +| Shell 在完全没有配置的目录里启动 | v26.13.0 | v26.13.0 | +| `cd` 进 `.nvmrc` 写着 18.20.8 的目录 | v26.13.0 | v18.20.8 | +| `cd` 进 `.node-version` 写着 24.22.2 的目录 | v26.13.0 | v24.22.2 | +| `cd` 进完全没有配置的目录 | v26.13.0 | v24.22.2 | +| `cd` 进 `.vfox.toml` 写着 18.20.8 的目录 | v18.20.8 | v18.20.8 | +| `cd` 进 `.vfox.toml` 写着 24.22.2 的目录 | v24.22.2 | v24.22.2 | +| `cd` 进完全没有配置的目录 | v26.13.0 | v26.13.0 | + +- **`.vfox.toml`** 在每次 `cd` 时都会切换,离开去没有配置的目录时也会切回全局版本。每个版本都是这样。 +- **`.nvmrc` 和 `.node-version`** 在每个提示符出现在它所在的目录里时都会读取(未发布)。从一个这样的目录 `cd` 到另一个会切换,改动文件则在下一个提示符生效。0.5.3 只在 Shell 于该目录里启动时读取,所以上面的 `cd` 步骤一个都没有切换。 +- **离开**带有这类文件的目录、进入完全没有配置的目录时,两边都不会切换回来:Shell 保持它原有的版本,第四步就是这样。在没有东西可读的目录里不会调用插件,vfox 沿用它保存的结果。新开的终端,或者带有 `.vfox.toml` 或它自己的版本文件的目录,会切换。这是 vfox 缓存的限制,已作为 [version-fox/vfox#745](https://github.com/version-fox/vfox/issues/745) 报告,这里用 [#46](https://github.com/version-fox/vfox-nodejs/issues/46) 跟踪。 + +原因在 [nvmrc.md](nvmrc.md#什么时候读取这个文件):vfox 把 Shell 第一次计算的结果保存下来,并不跟踪版本文件(vfox 1.0.13),而插件在每次读取版本文件时都会让 vfox 重新计算。所以: + +- 对需要在同一个 Shell 里来回切换的项目,`.vfox.toml` 在每个版本里、两个方向上都管用:在每个项目里执行 `vfox use --project nodejs@18.20.8` 这样的命令。 +- 在该目录里启动的东西,`.nvmrc` 就够了:编辑器里的终端、同一文件夹里新开的标签页、脚本。这也是本仓库的 Windows E2E 套件为每个目录都新启动一个 Shell 的原因。 + +## 只在这个窗口里换一个版本 + +你想用 26 跑一遍测试,其他的都保持原样: + +```powershell +vfox use --session nodejs@26 +node -v # v26.13.0 +vfox current nodejs # -> v26.13.0 +``` + +这个选择从下一个提示符起生效,也就是命令一结束就生效。关掉窗口它就没了,新开的窗口又是全局版本。项目里不会写入任何东西。在 `.vfox.toml` 写了 `nodejs` 的目录里,那个文件优先于 `--session`;`.nvmrc` 则优先于不带 `--project` 的 `vfox use`,见 [nvmrc.md](nvmrc.md#使用方法)。 + +## 用指定版本运行一条命令 + +`vfox exec` 用你指定的版本运行一条命令。它不改变任何作用域,不在项目里写文件,也不需要挂载。所以它适合没有挂载的 Shell,比如 Dockerfile、CI 作业或计划任务,vfox 自己的 [FAQ](https://vfox.dev/zh-hans/guides/faq.html) 也推荐这样做。 + +```bash +vfox exec nodejs@22.23.4 -- node -v # v22.23.4 +vfox exec nodejs@22.23.4 -- npm test +for v in 20.20.2 22.23.4 24.22.2; do vfox exec "nodejs@$v" -- node -v; done +``` + +```powershell +foreach ($v in '20.20.2', '22.23.4', '24.22.2') { vfox exec "nodejs@$v" -- node -v } +``` + +三个版本分别打印了 `v20.20.2`、`v22.23.4` 和 `v24.22.2`,在 bash 和 PowerShell 7 里都是这样。需要知道的是: + +- **要写完整的版本号。** `vfox exec` 不像 `vfox use` 那样理解 `22`,会失败: + + ```text + $ vfox exec nodejs@22 -- node -v + SDK nodejs@22 not found, installing... + nodejs@22.23.4 is already installed + failed to get runtime package for nodejs@22: open /home/me/.vfox/cache/nodejs/v-22: no such file or directory + ``` + +- **没有安装的版本会先安装**,同样会检查它的 SHA-256 校验和,然后再运行命令。写错的版本号也一样处理,并且在安装失败的地方结束:`nodejs@22.99.0` 会报 `get checksum failed: HTTP 404 from https://nodejs.org/dist/v22.99.0/SHASUMS256.txt`。 +- **命令的退出状态就是 vfox 的退出状态。** `vfox exec nodejs@22.23.4 -- node -e "process.exit(3)"` 会打印 `exit status 3`,并以 3 退出,所以测试失败时,循环或者 CI 步骤也会失败。 +- **不写版本就用已配置的那个。** `vfox exec nodejs -- node -v` 使用项目、会话和全局作用域里设置的版本。 +- **在 Windows 上,请按磁盘上的名字写出程序**,即 `node.exe` 或 `npm.cmd`。`vfox exec` 会在该版本的目录里查找你写的这个名字。这一点来自 vfox 的源码,没有在 Windows 上运行过。 + +## 跟随版本线或固定版本 + +Node.js 以同一条版本线的新版本发布修复,比如先是 24.22.1,然后是 24.22.2。一个项目要么停在某个版本上,要么跟随它的版本线。 + +- **固定。** 在 `.vfox.toml` 或 `.nvmrc` 里写完整的版本号。在有人改这个文件之前,所有人运行的 Node.js 都相同。要升级:`vfox install nodejs@24` 会安装最新的 24.x(已经有的话会提示 `nodejs@24.22.2 is already installed`),再用 `vfox use --project nodejs@24` 把它选定的版本写进 `.vfox.toml`,即 `nodejs = "24.22.2"`。 +- **跟随。** 在 `.nvmrc` 里写 `24`,并执行 `vfox config legacyVersionFile.strategy latest_installed`。新的 24.x 在每台安装了它的机器上都会生效,各机器在一段时间内可能不一致。见 [nvmrc.md](nvmrc.md#由策略决定)。 + +全局包不会移到新版本里。切换之前先列出来,之后再装一遍。每个版本都有自己的全局包: + +```text +$ npm ls -g --depth=0 +/home/me/.vfox/cache/nodejs/v-22.23.4/nodejs-22.23.4/lib +├── corepack@0.36.0 +├── npm@10.9.9 +└── semver@7.8.6 +$ vfox exec nodejs@24.22.2 -- npm ls -g --depth=0 +/home/me/.vfox/cache/nodejs/v-24.22.2/nodejs-24.22.2/lib +├── corepack@0.36.0 +└── npm@11.19.1 +``` + +`vfox uninstall nodejs@24.22.1` 会把旧版本连同它的全局包一起删掉。在 Linux 上,一个版本占用 157 到 225 MB(从 18.20.8 到 26.13.0)。`uninstall` 接受哪些名字,见 [versions.md](versions.md#安装后的版本名)。 + +## 防火墙或公司代理之后 + +Node.js 压缩包和 npm 包来自两个地方,各有各的设置: + +```bash +export VFOX_NODEJS_MIRROR="https://npmmirror.com/mirrors/node" +npm config set registry https://registry.npmmirror.com +``` + +```powershell +[Environment]::SetEnvironmentVariable("VFOX_NODEJS_MIRROR", "https://npmmirror.com/mirrors/node", "User") +npm config set registry https://registry.npmmirror.com +``` + +PowerShell 的第一行为你这个用户设置变量,之后打开的终端里才有。你们公司自己提供的镜像,只要目录结构和 `nodejs.org/dist` 一样,用法也相同。代理用 `vfox config proxy.enable true` 和 `vfox config proxy.url
` 设置。这两项在 [mirror.md](mirror.md) 和 [troubleshooting.md](troubleshooting.md#网络) 里都有说明。 + +镜像可能落后于它自己的 `index.json`。2026-11-03,npmmirror 列出了 26.13.0,却没有它的 `SHASUMS256.txt`,所以在那里 `nodejs@26` 失败了,而 `nodejs@24` 正常: + +```text +get checksum failed: HTTP 404 from https://npmmirror.com/mirrors/node/v26.13.0/SHASUMS256.txt +``` + +可以安装它前一个版本(`nodejs@26.12.1`),或者稍后再试。 + +## 编辑器和 IDE + +- 想跟随你的全局选择,就让编辑器或构建工具指向 `~/.vfox/sdks/nodejs/bin/node`,在 Windows 上指向 `%USERPROFILE%\.vfox\sdks\nodejs\node.exe`。要固定某个版本,就用该版本的路径,即 `~/.vfox/cache/nodejs/v-22.23.4/nodejs-22.23.4/bin/node`。见 [npm.md](npm.md#文件在哪里)。 +- 从开始菜单启动的编辑器不会运行任何挂载。在 Windows 上,它仍然能通过 `vfox use --global` 写入的 `Path` 找到全局的 `node`,见 [Windows PowerShell 5.1](#windows-powershell-51)。编辑器里的终端会运行你的配置文件,所以挂载、`.vfox.toml`,以及它所打开文件夹里的 `.nvmrc`,在那里都生效。 +- 需要固定在某个版本上运行的任务,用 `vfox exec`。在 `.vscode/tasks.json` 里就是 `"command": "vfox"`,加上 `"args": ["x", "nodejs@22.23.4", "--", "node", "${file}"]`。 diff --git a/docs/zh-CN/troubleshooting.md b/docs/zh-CN/troubleshooting.md index 66752cc..f7f890f 100644 --- a/docs/zh-CN/troubleshooting.md +++ b/docs/zh-CN/troubleshooting.md @@ -123,7 +123,7 @@ vfox config proxy.url http://127.0.0.1:7890 - **`vfox current nodejs` 和 `node -v` 的结果不一致。** 原因同上。前者给出的是会话或作用域里的选择,后者是 `PATH` 实际找到的。 - **`vfox use` 之后,全局安装的 npm 包不见了。** 全局包属于某个版本,见 [npm.md](npm.md#全局包属于某个版本)。 - **找不到 `corepack`。** Node.js 25 及以后的版本不自带它,见 [npm.md](npm.md#yarnpnpm-与-corepack)。 -- **`.nvmrc` 什么也没选中。** 请检查策略和文件里的内容,见 [nvmrc.md](nvmrc.md#没有生效)。 +- **`.nvmrc` 什么也没选中。** 请检查策略和文件里的内容,以及 Shell 是不是从带有版本文件的目录进入了完全没有配置的目录(它会保持原有的版本)。0.5.3 在已经运行的 Shell 里 `cd` 之后也不会读取这个文件。见 [nvmrc.md](nvmrc.md#没有生效)。 - **`git status` 列出了 `.vfox/`。** vfox 会把这个版本以 `.vfox/sdks/nodejs` 的形式链接到项目里。请在 `.gitignore` 里忽略 `.vfox/`,并保留 `.vfox.toml`。 - **安装后的版本名里没有 `v`。** 版本的名字形如 `24.21.0`,你输入时带的 `v` 会被去掉。 diff --git a/hooks/available.lua b/hooks/available.lua index 008eb52..8dfa143 100644 --- a/hooks/available.lua +++ b/hooks/available.lua @@ -36,6 +36,9 @@ function PLUGIN:Available(ctx) error("Failed to fetch Node.js versions: HTTP " .. tostring(resp.status_code)) end local body = json.decode(resp.body) + -- The index names the LTS lines. Keep what it says for ParseLegacyFile, which + -- runs at every prompt and must not fetch it. See docs/lts.md. + util.remember_lts(body) local result = {} for _, v in ipairs(body) do diff --git a/hooks/parse_legacy_file.lua b/hooks/parse_legacy_file.lua index dc6c93c..8b7d45c 100644 --- a/hooks/parse_legacy_file.lua +++ b/hooks/parse_legacy_file.lua @@ -18,11 +18,12 @@ local http = require("http") local json = require("json") local util = require("util") ---- Discover LTS codenames from the version index instead of the hardcoded ---- table below. Only used under the `latest_available` strategy, which is ---- network-bound anyway; `specified` and `latest_installed` must keep ---- working offline. Returns nil when the index cannot be fetched, and the ---- caller falls back to the hardcoded table. +--- Discover LTS codenames from the version index itself, for the newest +--- release of a line. Only used under the `latest_available` strategy, which +--- is network-bound anyway; `specified` and `latest_installed` run at every +--- prompt and must keep working offline, with what the plugin has learned from +--- the index (UTIL.remember_lts). Returns nil when the index cannot be fetched, +--- and the caller falls back to that. --- @return table|nil { by_codename: map, newest: string|nil } local function discover_lts() local resp, err = http.get({ @@ -60,7 +61,11 @@ function PLUGIN:ParseLegacyFile(ctx) end local content = file:read("*a") or "" file:close() - content = content:gsub("%s+", "") + -- vfox does not notice this file when it decides whether the result it kept + -- for the shell is stale, so make it work out the environment again. + util.forget_env_state() + -- Windows PowerShell 5.1 writes UTF-16, or UTF-8 with a byte order mark. + content = util.decode_text(content):gsub("%s+", "") if content == "" then return {} end @@ -125,40 +130,21 @@ function resolve_version(query, strategy) end end - -- Offline fallback below: the hardcoded table keeps `specified` and - -- `latest_installed` working without network. It also covers - -- `latest_available` when discovery finds no match. - -- https://nodejs.org/zh-cn/about/previous-releases - local nodejs_codenames = { - argon = 4, - boron = 6, - carbon = 8, - dubnium = 10, - erbium = 12, - fermium = 14, - gallium = 16, - hydrogen = 18, - iron = 20, - jod = 22, - krypton = 24, - } - - for codename, version_number in pairs(nodejs_codenames) do - if query == "lts/" .. codename then - query = tostring(version_number) - break - end - end - - if query == "lts" or query == "lts/*" then - -- Find the latest LTS version (highest version number) - local latest_version = 0 - for codename, version_number in pairs(nodejs_codenames) do - if version_number > latest_version then - latest_version = version_number + -- Offline, without fetching anything: `specified` and `latest_installed` run + -- at every prompt in a directory with this file. The LTS lines are what the + -- plugin learned from the index (UTIL.remember_lts) and, before it learned + -- any, the built-in table. This also covers `latest_available` when + -- discovery found no match. See docs/lts.md. + if lts_name ~= nil or is_bare_lts then + local lines, newest = util.known_lts_lines() + if is_bare_lts then + -- The newest LTS line. + if newest > 0 then + query = tostring(newest) end + elseif lines[lts_name] ~= nil then + query = tostring(lines[lts_name]) end - query = tostring(latest_version) end return query diff --git a/hooks/pre_install.lua b/hooks/pre_install.lua index 043a167..80b6d9a 100644 --- a/hooks/pre_install.lua +++ b/hooks/pre_install.lua @@ -17,11 +17,10 @@ local util = require("util") --- @class PreInstallContext --- @field version string User-input version ---- Returns some pre-installed information, such as version number, download address, local files, etc. ---- If checksum is provided, vfox will automatically check it for you. ---- @param ctx PreInstallContext ---- @return table Version information -function PLUGIN:PreInstall(ctx) +---@param self table PLUGIN +---@param ctx PreInstallContext +---@return table +local function pre_install(self, ctx) local version = ctx.version if version == "latest" then @@ -149,3 +148,14 @@ function PLUGIN:PreInstall(ctx) end error("Node.js package " .. filename .. " is not listed in " .. baseUrl .. "SHASUMS256.txt") end + +--- Returns some pre-installed information, such as version number, download address, local files, etc. +--- If checksum is provided, vfox will automatically check it for you. +--- @param ctx PreInstallContext +--- @return table Version information +function PLUGIN:PreInstall(ctx) + local result = pre_install(self, ctx) + -- The install goes ahead, which is when a new LTS line is about to matter. + util.refresh_lts_cache(self) + return result +end diff --git a/lib/util.lua b/lib/util.lua index 6c3b30b..a2071bc 100644 --- a/lib/util.lua +++ b/lib/util.lua @@ -219,6 +219,313 @@ function UTIL.capture(command) return output, nil end +--- A Unicode code point as UTF-8. +--- @param code number +--- @return string +local function utf8_char(code) + if code < 0x80 then + return string.char(code) + elseif code < 0x800 then + return string.char(0xC0 + math.floor(code / 0x40), 0x80 + code % 0x40) + elseif code < 0x10000 then + return string.char(0xE0 + math.floor(code / 0x1000), 0x80 + math.floor(code / 0x40) % 0x40, 0x80 + code % 0x40) + end + return string.char( + 0xF0 + math.floor(code / 0x40000), + 0x80 + math.floor(code / 0x1000) % 0x40, + 0x80 + math.floor(code / 0x40) % 0x40, + 0x80 + code % 0x40 + ) +end + +--- The text of a version file as UTF-8 without a byte order mark. Windows +--- PowerShell 5.1 writes UTF-16 with a mark for `>`, `>>` and `Out-File`, and +--- UTF-8 with a mark for `-Encoding UTF8`. Read as they are, the mark and the +--- zero bytes of UTF-16 become part of the text, and no release matches it. +--- Text that starts with no mark is returned as it is. +--- @param content string the bytes of the file +--- @return string +function UTIL.decode_text(content) + if content:sub(1, 3) == "\239\187\191" then + return content:sub(4) + end + local first, second = content:byte(1, 2) + local little_endian + if first == 0xFF and second == 0xFE then + little_endian = true + elseif first == 0xFE and second == 0xFF then + little_endian = false + else + return content + end + --- The 16-bit unit at a position, nil when only one byte is left. + local function unit_at(position) + local low, high = content:byte(position, position + 1) + if high == nil then + return nil + end + if little_endian then + return low + high * 256 + end + return low * 256 + high + end + local chars = {} + local position = 3 + while position < #content do + -- Two bytes are there, so the replacement character is never used. + local unit = unit_at(position) or 0xFFFD + position = position + 2 + if unit >= 0xD800 and unit <= 0xDBFF then + -- A high surrogate needs a low one after it, else it is an error. + local low = unit_at(position) + if low ~= nil and low >= 0xDC00 and low <= 0xDFFF then + unit = 0x10000 + (unit - 0xD800) * 0x400 + (low - 0xDC00) + position = position + 2 + else + unit = 0xFFFD + end + elseif unit >= 0xDC00 and unit <= 0xDFFF then + unit = 0xFFFD + end + table.insert(chars, utf8_char(unit)) + end + return table.concat(chars) +end + +--- Make the `vfox env` that is running work out the environment from nothing. +--- +--- vfox keeps the result of `vfox env` for a shell in `env-state.json` in the +--- session directory (`$__VFOX_CURTMPPATH`, which its shell hook exports), and +--- uses it again while no `.vfox.toml` it tracks changes and `PATH` stays as it +--- was. A `.nvmrc` or `.node-version` is not tracked (vfox 1.0.13 and `main` +--- alike), so after a `cd` into a directory that only has one, vfox would go on +--- with the result it made for the directory before, and ignore the file. vfox +--- runs ParseLegacyFile before it loads the state, so removing the file here +--- makes it calculate again. That is a workaround to drop when vfox tracks these +--- files. It cannot do anything for a `cd` out of such a directory, where this +--- hook is not called. See docs/nvmrc.md, "When the file is read". +function UTIL.forget_env_state() + local dir = os.getenv("__VFOX_CURTMPPATH") + if dir == nil or dir == "" or type(os.remove) ~= "function" then + return + end + local windows = RUNTIME ~= nil and RUNTIME.osType == "windows" + local separator = windows and "\\" or "/" + -- Nothing to report when the file is not there or cannot be removed. + pcall(os.remove, (dir:gsub("[/\\]+$", "")) .. separator .. "env-state.json") +end + +--- The LTS lines that the plugin knows without having seen the index. Node.js +--- names each line when it enters long-term support. This is the cold-start +--- fallback of ParseLegacyFile: what the plugin learns from the index (see +--- UTIL.remember_lts) goes before it, so the next line needs no entry here. +UTIL.LtsLines = { + argon = 4, + boron = 6, + carbon = 8, + dubnium = 10, + erbium = 12, + fermium = 14, + gallium = 16, + hydrogen = 18, + iron = 20, + jod = 22, + krypton = 24, + lithium = 26, +} + +--- How long what the plugin learned counts as fresh when an install looks at it. +UTIL.LtsCacheMaxAge = 24 * 60 * 60 + +--- The directory of this plugin. vfox puts the plugin's `hooks/?.lua` and +--- `lib/?.lua` into `package.path`, so it is where `lib/util.lua` is found. +--- @return string|nil dir nil when package.path does not tell +--- @return string|nil separator the one that package.path uses +function UTIL.plugin_dir() + for template in string.gmatch(package.path or "", "[^;]+") do + local candidate = (template:gsub("%?", "util")) + local dir = candidate:match("^(.*)[/\\]lib[/\\]util%.lua$") + if dir ~= nil and UTIL.file_exists(candidate) then + local separator = "/" + if candidate:find("\\", 1, true) ~= nil then + separator = "\\" + end + return dir, separator + end + end + return nil, nil +end + +--- The file in which the plugin keeps what it learned about LTS lines, next to +--- the `.available.cache` that vfox keeps there for the result of Available. +--- @return string|nil +function UTIL.lts_cache_path() + local dir, separator = UTIL.plugin_dir() + if dir == nil then + return nil + end + return dir .. separator .. ".lts-cache" +end + +--- What the version index says about LTS lines: the major of each named line, +--- and the newest major that is LTS at all. A release whose `lts` is `true` +--- (an LTS line without a name) counts for the newest only. +--- @param index any the decoded index.json: a list of releases +--- @return table { lines = { [codename] = major }, newest = major|nil } +function UTIL.lts_summary(index) + local lines = {} + local newest = nil + if type(index) ~= "table" then + return { lines = lines } + end + for _, release in ipairs(index) do + if type(release) == "table" and release.version ~= nil then + local lts = release.lts + local name = nil + if type(lts) == "string" and lts:match("^[%w%-]+$") ~= nil then + name = lts:lower() + end + local major = tonumber(tostring(release.version):match("^v?(%d+)%.")) + if major ~= nil and (name ~= nil or lts == true) then + if newest == nil or major > newest then + newest = major + end + if name ~= nil and (lines[name] == nil or major > lines[name]) then + lines[name] = major + end + end + end + end + return { lines = lines, newest = newest } +end + +--- Write what the plugin learned. The file is plain text, one fact per line: +--- `updated