fix(agent): installs without Node ask for it the way that works (#774)

* fix(agent): installs without Node ask for it the way that works

The commands that install Node first, for a machine without it (#727),
failed in ways that say nothing about the real problem.

On a Mac or Linux, `curl -o- … | bash` keeps going when the download
fails: a pipe's status is the last command's, so blocked or 404'd
(raw.githubusercontent.com is commonly unreachable), bash reads nothing,
still exits 0, and the user is told `nvm.sh: No such file or directory`
— the network's fault told as a missing file. The vendor commands beside
it all curl with -f (install.go's own scripts do); this one now does too,
so a failed download stops the line with curl's own words.

On Windows, winget asks to accept the msstore source's agreements the
first time it runs, which waits for input a copy-pasted command can't
give: Node isn't installed, and npm.cmd isn't found. The command now
carries --accept-source-agreements and --accept-package-agreements, the
flags Microsoft documents for unattended installs; they are idempotent
where the source is already accepted.

TestInstallCommandsWithoutNode expects the two commands as they now
stand, and fails on the old ones ("curl -o- …" and the winget line
without the flags, on darwin, linux and windows alike);
TestInstallWithoutNodeParses still reads each line with `bash -n`.
Validation on Windows: go test ./internal/agent under a temp HOME leaves
the three Unix-only failures the base commit leaves (TestCodexStaleOnly
ForMagpiesChange, TestSuffixModesReachAgentFiles, TestWSLProbeScript
SaysWhere); go vet ./internal/... and the darwin, linux and windows
builds pass.

* fix(agent): a failed Node download stops the line where it failed

yetone's review of the first cut: `curl -fsSL … | bash` doesn't stop at a
failed download either, and my claim that -f alone fixes it was wrong — a
pipe's status is the shell's, bash reading nothing exits 0, and the &&
chain goes on to source nvm.sh, which reports the missing file exactly as
before. Reproduced against a 404 server on this machine: `curl: (22) …
404` then `bash: …/.nvm/nvm.sh: No such file or directory`.

The command downloads to a file now and runs that: a failed download ends
the chain at curl, with curl's own words about why. pipefail isn't an
option — the line is pasted into the user's own shell, and not every one
of them sets it.

TestInstallWithoutNodeStopsWhenTheDownloadFails runs the real line,
against a local 404 server with a temporary HOME, on each POSIX shell
here: the old line failed with the download's error *and* nvm.sh missing,
the new one stops at curl's own error and never mentions nvm.sh. It is
skipped where there is no curl. TestInstallCommandsWithoutNode expects
the line as it now stands.

Validation on Windows: go test ./internal/agent under a temp HOME leaves
the three Unix-only failures the base commit leaves (TestCodexStaleOnly
ForMagpiesChange, TestSuffixModesReachAgentFiles, TestWSLProbeScript
SaysWhere); go vet ./internal/..., the darwin, GOOS=linux and GOOS=windows
builds pass.
This commit is contained in:
TryWorld
2026-10-04 19:49:29 +08:00
committed by GitHub
parent 25f67437c2
commit f24ee8b06d
2 changed files with 43 additions and 6 deletions
+3 -3
View File
@@ -119,7 +119,7 @@ func npmInstall(pkg, goos string, node bool) InstallCmd {
case node:
return InstallCmd{Via: "npm", Command: plain}
case goos == "windows":
return InstallCmd{Via: "npm", Node: "winget", Command: "winget install -e --id OpenJS.NodeJS.LTS; " +
return InstallCmd{Via: "npm", Node: "winget", Command: "winget install -e --id OpenJS.NodeJS.LTS --accept-source-agreements --accept-package-agreements; " +
"$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [Environment]::GetEnvironmentVariable('Path','User'); " +
"npm.cmd install -g " + pkg}
}
@@ -128,8 +128,8 @@ func npmInstall(pkg, goos string, node bool) InstallCmd {
how = "nvm-mac"
}
return InstallCmd{Via: "npm", Node: how, Command: `export NVM_DIR="$HOME/.nvm" && mkdir -p "$NVM_DIR" && ` +
"curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/" + nvmVersion + "/install.sh | bash && " +
`. "$NVM_DIR/nvm.sh" && nvm install --lts && ` + plain}
"t=$(mktemp) && curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/" + nvmVersion + `/install.sh -o "$t" && ` +
`bash "$t" && . "$NVM_DIR/nvm.sh" && nvm install --lts && ` + plain}
}
// nodeHere says whether this machine has Node.js's npm: on PATH, in one
+40 -3
View File
@@ -1,10 +1,13 @@
package agent
import (
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
)
@@ -60,9 +63,10 @@ func TestInstallsOnlyMissing(t *testing.T) {
// line (#727, Sun1090): nvm and its LTS Node on a Mac or Linux, winget's
// Node.js LTS on Windows; the vendors' own installers are as they were.
func TestInstallCommandsWithoutNode(t *testing.T) {
nvm := `export NVM_DIR="$HOME/.nvm" && mkdir -p "$NVM_DIR" && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/` + nvmVersion +
`/install.sh | bash && . "$NVM_DIR/nvm.sh" && nvm install --lts && npm install -g `
winget := "winget install -e --id OpenJS.NodeJS.LTS; $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [Environment]::GetEnvironmentVariable('Path','User'); npm.cmd install -g "
nvm := `export NVM_DIR="$HOME/.nvm" && mkdir -p "$NVM_DIR" && ` +
`t=$(mktemp) && curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/` + nvmVersion +
`/install.sh -o "$t" && bash "$t" && . "$NVM_DIR/nvm.sh" && nvm install --lts && npm install -g `
winget := "winget install -e --id OpenJS.NodeJS.LTS --accept-source-agreements --accept-package-agreements; $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [Environment]::GetEnvironmentVariable('Path','User'); npm.cmd install -g "
for _, c := range []struct {
id, goos string
want []InstallCmd
@@ -137,3 +141,36 @@ func TestInstallWithoutNodeParses(t *testing.T) {
}
}
}
// A download that fails stops the line where it failed: piped into a
// shell, the line's status was the shell's, so a 404 or a blocked host
// went on to source nvm.sh and told the user that file was missing. The
// command now downloads to a file, and its own failure ends the chain.
func TestInstallWithoutNodeStopsWhenTheDownloadFails(t *testing.T) {
if _, err := exec.LookPath("curl"); err != nil {
t.Skip("no curl to test the download with")
}
bad := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer bad.Close()
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("NVM_DIR", filepath.Join(home, ".nvm"))
c := strings.Replace(npmInstall("@earendil-works/pi-coding-agent", "linux", false).Command,
"https://raw.githubusercontent.com/nvm-sh/nvm/"+nvmVersion+"/install.sh", bad.URL+"/install.sh", 1)
for _, sh := range []string{"bash", "zsh", "sh"} {
p, err := exec.LookPath(sh)
if err != nil {
continue
}
out, err := exec.Command(p, "-c", c).CombinedOutput()
if err == nil {
t.Errorf("%s: the line went on past the failed download: %s", sh, out)
continue
}
if strings.Contains(string(out), "nvm.sh") {
t.Errorf("%s: the failed download is still told as a missing nvm.sh: %s", sh, out)
}
}
}