fix(scripts): make the alias check fail closed, and correct three stale docs (#208)

The alias check was a negative match on an allowlist of English error strings, which returned True for empty output, for $null, and for "exit status 1: Access is denied." — so an alias switch producing nothing, or failing on the symlink permission error this file's own header warns about, was reported as success while the old version kept running. That is the bug #198 was filed about, narrowed rather than removed, and it also broke whenever nvm reworded an error. It is now a positive match on "Now using node v<what is actually running>".

The floor check moves ahead of the switch and reads the constant rather than the result. Where it sat, $major was always whatever NODE_VERSION says, so it validated the switch it had just made instead of the pin it exists to guard, and could never fire.

Use-NodeLatest is now Use-PinnedNode. In a change whose whole subject is that "latest" means something people do not expect, the name was an avoidable trap.

The restore default moves beside NODE_VERSION. It deliberately is not a param default: a param block runs before the dot-source, so $script:DEFAULT_NODE_VERSION is still $null there and the restore would have quietly restored nothing — leaving the machine on the pinned version, which is the exact failure the restore exists to prevent. It is resolved after the dot-source instead, and an explicit -DefaultNodeVersion still wins.

Three documents described behaviour the code no longer has: README's "both scripts run nvm use latest", run-tests.ps1's .DESCRIPTION, and project-context.md's instruction to agents. All corrected, and project-context.md now also says not to run these scripts from an agent shell, which is how this machine once ended up with no Node at all.

Part 4 of the issue is partly stale: backend/package.json already declares engines >=20.9.0. frontend now matches it. The larger question — whether local should be pinned to the Node 20 that CI and the production image actually run — is a decision rather than an oversight and is left open on the issue.

Verified by parsing all three scripts with the PowerShell AST parser, which does not execute them. They are deliberately never run from an agent shell.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-02 08:48:54 -05:00
co-authored by Claude Opus 5
parent be6a2fe5bd
commit cf2f5dd4a3
6 changed files with 77 additions and 22 deletions
+39 -11
View File
@@ -69,8 +69,17 @@ function Use-Node {
# nvm wanted is not installed" into a claim that the newest install
# was too old, which sent the reader to `nvm install` holding a list
# that already had newer versions on it.
# A POSITIVE match on what success looks like, not a negative
# one on an allowlist of English error strings. The negative form
# returned True for empty output, for $null, and for
# 'exit status 1: Access is denied.' — so an alias switch that
# produced nothing, or failed on the symlink permission error this
# file's own header warns about, was reported as success while the
# old version kept running. That is the bug #198 was filed about,
# narrowed rather than removed. It also broke whenever nvm reworded
# an error. See #208.
$switched = if ($Version -in @('latest', 'lts', 'newest')) {
$output -notmatch 'activation error|not installed'
$output -match ('Now using node v' + [regex]::Escape($raw.TrimStart('v')))
}
else {
$raw.TrimStart('v') -eq $Version.TrimStart('v')
@@ -100,9 +109,10 @@ nvm said:
$output
nvm-windows rewrites a symlink at $env:NVM_SYMLINK, and can report success
without having rewritten it. Check the version is installed, and that this shell
can write that link:
without having rewritten it. If that version is not installed, install it; if it
is, check this shell can write that link:
nvm install $Version
nvm list
"@
}
@@ -114,6 +124,20 @@ can write that link:
# the specification.
$script:NODE_VERSION = '26.7.0'
# What the machine is put back to afterwards. Beside NODE_VERSION rather than
# duplicated as a parameter default in each script, because two copies of a
# version drift and the half that drifts is the half nobody runs. Both scripts
# still take -DefaultNodeVersion to override it.
$script:DEFAULT_NODE_VERSION = '18.16.1'
# Local runs are three major lines ahead of CI (Node 20 in every workflow) and of
# the production image (node:20-bookworm-slim). That divergence is deliberate but
# not free: post-20 syntax and node: APIs pass here and fail in the pipeline, and
# Node 26 ships an npm that can touch the lockfile in ways CI's npm reads
# differently. The `engines` field in both package.json files records the floor
# machine-readably; nothing yet catches "too new for where this ships". See #208
# part 4, which is an open decision rather than an oversight.
<#
Switches to the pinned version and insists it clears the floor.
@@ -130,20 +154,24 @@ $script:NODE_VERSION = '26.7.0'
A version that is not installed is Use-Node's error to report, and it now
reports nvm's own reason, so there is nothing to say about it here.
#>
function Use-NodeLatest {
function Use-PinnedNode {
param([scriptblock]$Step, [scriptblock]$Note)
Use-Node -Version $script:NODE_VERSION -Why 'this project needs Node 20 or newer' -Step $Step -Note $Note | Out-Null
$major = Get-NodeMajor
if ($major -lt 20) {
# Checked BEFORE the switch, against the constant rather than against what
# ends up running. After the switch $major is whatever NODE_VERSION says,
# so the old placement could never fire — it validated the switch it had
# just made instead of the pin it exists to guard. See #208.
$pinnedMajor = [int](($script:NODE_VERSION -replace '^v', '') -split '\.')[0]
if ($pinnedMajor -lt 20) {
throw @"
Node is v$major after switching to $($script:NODE_VERSION), which is below the 20 this project needs.
NODE_VERSION in scripts/NodeVersion.ps1 is $($script:NODE_VERSION), which is below the 20 this project needs.
NODE_VERSION in scripts/NodeVersion.ps1 is pinned to a version that is too old.
node-pg-migrate, ts-jest and Playwright all need 20 or newer.
node-pg-migrate, ts-jest and Playwright all need 20 or newer. Nothing was
switched.
"@
}
Use-Node -Version $script:NODE_VERSION -Why 'this project needs Node 20 or newer' -Step $Step -Note $Note | Out-Null
}
<#