opencode icon indicating copy to clipboard operation
opencode copied to clipboard

cli: opencode upgrade via npm uses --force, downloading all platform optionalDependencies (~2GB)

Open xDipzz opened this issue 2 weeks ago • 8 comments

Description

When updating OpenCode via opencode upgrade on an npm-managed installation, the updater spawns npm with --force:

npm install --global --allow-scripts=@opencode/cli --force @opencode/cli@<version>

In packages/cli/src/services/updater.ts:

const commands: Record<Exclude<Method, "bun" | "curl" | "brew" | "winget">, string[]> = {
  npm: [
    npm,
    "install",
    "--global",
    ...npmPrefixArgs,
    `--allow-scripts=${packageName}`,
    ...((OPENCODE_ARTIFACT === "cli" && !installedPackage?.endsWith("/cli-node")) ||
    (installedPackage && packageName !== installedPackage)
      ? ["--force"]
      : []),
    target,
  ],

Because @opencode/cli declares 12 native target binaries in optionalDependencies, passing --force to npm forces it to bypass os and cpu filtering. Consequently, npm downloads all 12 platform packages (~1.5 GB to 2 GB total) simultaneously instead of only the single ~115 MB package for the current host.

From ~/.npm/_logs/*-debug-0.log:

80 silly reify mark retired [
80 silly reify   '.../lib/node_modules/@opencode/cli',
80 silly reify   '.../bin/opencode',
80 silly reify   '.../bin/opencode2'
80 silly reify ]
81 silly reify moves {
81 silly reify   '.../bin/opencode': '.../bin/.opencode-GjisxTmU',
81 silly reify   '.../bin/opencode2': '.../bin/.opencode2-BL1uIZyH'
81 silly reify }
...
93 http fetch GET 200 https://registry.npmjs.org/@opencode/cli-linux-x64-baseline/-/cli-linux-x64-baseline-2.3.0.tgz 246068ms (cache miss)
94 http fetch GET 200 https://registry.npmjs.org/@opencode/cli-linux-x64-baseline-musl/-/cli-linux-x64-baseline-musl-2.3.0.tgz 256299ms (cache miss)

Because npm renames/retires existing global binaries prior to fetching dependencies, if the multi-gigabyte download times out or is interrupted, the installation is left with a truncated binary and missing symlinks, causing command not found: opencode.

Plugins

None

OpenCode version

@opencode/cli 2.2.7 (upgrading to 2.3.0)

Steps to reproduce

  1. Have OpenCode installed globally via npm: npm install -g @opencode/[email protected]
  2. Run opencode upgrade 2.3.0 (or accept update prompt in TUI).
  3. Check ~/.npm/_logs/ or network activity: observe npm executing with --force and downloading all 12 optional dependency platform tarballs (macOS arm64/x64, Windows, Linux arm64/musl/baseline).

Operating System

Ubuntu 24.04.5 LTS (x86_64, Linux 7.0.0-38-generic), Node v24.18.1, npm 12.2.1

Terminal

zsh

xDipzz avatar Nov 13 '26 10:11 xDipzz

Thanks for the detailed report and the npm log. I tried to reproduce this but couldn't.

I used Linux x86_64, Node 24 and npm 12.2.1, with an empty npm cache and global folder. I installed @opencode/[email protected] with npm install -g, then ran opencode upgrade 2.3.0.

  • The updater does pass --force. npm's debug log shows install --global --allow-scripts @opencode/cli --force @opencode/[email protected].
  • npm still downloaded only this machine's binaries. The only tarballs fetched were the four linux-x64 variants (cli-linux-x64, -musl, -baseline, -baseline-musl). For darwin, windows and arm64 it fetched only the small package metadata, no tarballs.
  • A clean install without --force installs the same four packages, about 1 GB in total. So the size comes from those Linux variants, not from --force.
  • The upgrade finished normally, and opencode --version then showed v2.3.0.

The log lines in your report also show only linux-x64-baseline tarballs. Could you help with any of these?

  • A log line showing a darwin-*, windows-* or linux-arm64* tarball (.tgz) being downloaded during opencode upgrade.
  • The output of npm config ls -l | grep -E 'force|os|cpu|libc|include|omit', in case your npm config changes how optional dependencies are filtered.
  • If the binary went missing after an interrupted upgrade, the exact error npm printed, and whether you ran the upgrade from the TUI or from opencode upgrade.

You can reply here with the details, or edit the issue. If you edit it, please also leave a comment saying you've updated it, so we know when you're done.

opencodejr[bot] avatar Nov 13 '26 10:11 opencodejr[bot]

Thanks for testing and looking into this @opencodejr!

Here are the answers and requested details:

1. Tarball downloads & size

You're right: reviewing the log closely, the tarballs actually fetched were the 4 Linux-x64 variants (cli-linux-x64, -baseline, -musl, -baseline-musl), totaling ~1 GB uncompressed (not the darwin/windows tarballs, which were only metadata manifests).

2. Output of npm config ls -l

cpu = null
force = false
include = []
include-attestations = false
include-staged = false
include-workspace-root = false
libc = null
omit = []
omit-lockfile-registry-resolved = false
os = null
replace-registry-host = "npmjs"

3. Upgrade trigger & failure mode

  • Trigger: Run from the TUI via opencode --auto (aliased to oc), which prompted the update dialog.
  • Root Failure: Pulling ~1 GB across the 4 Linux tarballs took over 4 minutes on this connection (246068ms and 256299ms).
  • The Result of Interruption: During npm's reify step, npm retired the existing working binary:
    silly reify moves {
      '/.../bin/opencode': '/.../bin/.opencode-GjisxTmU',
      '/.../bin/opencode2': '/.../bin/.opencode2-BL1uIZyH'
    }
    
    Because the download was interrupted before postinstall/linking completed, the incoming binary in cli-linux-x64/bin/opencode was left truncated at 202MB, and npm never created the new opencode symlinks.
  • Errors seen:
    1. From shell: zsh: command not found: opencode
    2. Running the partially downloaded binary directly:
      error: ".../lib/node_modules/@opencode/cli/node_modules/@opencode/cli-linux-x64/bin/opencode" is truncated. Part of its embedded program is missing.
      note: The download or copy was probably interrupted. Download or install it again.
      

Because the upgrade pulls ~1 GB across 4 variants, any connection hiccup, timeout, or user cancellation during the 4+ minute download leaves the installation in a broken state without a working opencode binary in PATH.

xDipzz avatar Nov 13 '26 11:11 xDipzz

Seen with a simulated model or a mock server, not a real provider; a maintainer should confirm.

thanks for the details!! with them i could reproduce it on 2.2.7 → 2.3.0, and it still happens on latest dev (71d63d4) :(

wat i did:

  • npm 12.2.1, a new global prefix and an empty cache. npm install -g @opencode/[email protected]
  • made the internet slow on purpose: a local proxy limited to about 1 MB/s. the 4 linux-x64 tarballs are about 484 MB, so the install takes more than 5 min. the proxy was the only fake thing; the CLI was the real release
  • opencode upgrade 2.3.0

wat happened:

  • exactly 5 minits after start (11:03:19 → 11:08:22), opencode stopped npm: npm could not install OpenCode / Timed out
  • bin/ had only .opencode-rKrLPFsr n .opencode2-i7dupeuE left, the links npm moved aside. opencode: command not found
  • cli-linux-x64/bin/opencode was 159 MB, n running it gave the same "is truncated. Part of its embedded program is missing." as u saw

so it is not --force. it is the updater's own 5-minute timer killing npm in the middle of the install, n nothing puts the old opencode back after that.

the script below reproduces it (it needs its small throttle proxy). until this is fixed, if u r on a slow connection, running npm install -g @opencode/cli urself avoids the 5-minute limit

Reproduction script (repro.sh)
#!/usr/bin/env bash
# #63743: on a slow connection, `opencode upgrade` (npm install) is killed by the updater's
# 5-minute timeout mid-install, leaving no `opencode` on PATH.
# Needs node + npm 12 on PATH. OPENCODE_CMD can point at a dev checkout run (default: opencode from the prefix).
set -u
HERE=$(cd "$(dirname "$0")" && pwd)
ROOT=${ROOT:-/tmp/opencode/repro63743-slow}
PORT=${PORT:-8899}
rm -rf "$ROOT"; mkdir -p "$ROOT/g" "$ROOT/cache"
export npm_config_prefix="$ROOT/g" npm_config_cache="$ROOT/cache"
export PATH="$ROOT/g/bin:$PATH"
npm -v
# 1. normal-speed install of 2.2.7
npm install -g --allow-scripts=@opencode/cli @opencode/[email protected] >/dev/null 2>&1
opencode --version
# 2. slow connection: ~1 MB/s through a throttling CONNECT proxy (4 tarballs = ~484 MB compressed)
PORT=$PORT RATE_BYTES=${RATE_BYTES:-1000000} node "$HERE/throttle-proxy.mjs" & PROXY=$!
sleep 1
export npm_config_https_proxy="http://127.0.0.1:$PORT" npm_config_proxy="http://127.0.0.1:$PORT"
date -u +"start %T"
${OPENCODE_CMD:-opencode} upgrade 2.3.0 2>&1 | sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' | tr '\r' '\n' | grep -v 'Upgrading' 
echo "exit: ${PIPESTATUS[0]}"
date -u +"end   %T"
kill $PROXY
sleep 5  # let any orphaned npm settle
echo "--- npm processes still running:"; pgrep -af "npm install --global" || echo none
echo "--- $ROOT/g/bin:"; ls -la "$ROOT/g/bin"
hash -r; command -v opencode || echo "opencode: command not found"
echo "--- running the new platform binary directly:"
"$ROOT"/g/lib/node_modules/@opencode/cli/node_modules/@opencode/cli-linux-x64/bin/opencode --version 2>&1 | head -2
echo "--- last npm log lines:"; tail -5 "$(ls -t "$ROOT"/cache/_logs/*-debug-0.log | head -1)"

Run it: bash repro.sh

opencodejr[bot] avatar Nov 13 '26 11:11 opencodejr[bot]

A focused patch removes the updater's five-minute cutoff during installation. The installer-script download keeps its five-minute timeout.

The regression drives the real Updater/AppProcess with a controlled process handle and clock. All eight slow-install cases fail on the base and finish after six simulated minutes with the patch. Stalled script downloads still time out, and cancellation releases the process scope.

At dev a660c18c08, macOS/Bun 1.4.4: ten updater runs (480 passed, 50 platform skips), full CLI (409 passed, 9 skipped), repository lint/all 35 typecheck tasks, and CLI official TypeScript pass. Real throttled npm upgrades and Windows were not tested.

GitHub currently blocks a fourth open PR here; upgrade-install-timeout is available meanwhile. AI-assisted implementation and verification.

r266-tech avatar Nov 13 '26 11:11 r266-tech

Thanks @opencodejr for isolating the 5-minute timeout, and @r266-tech for the quick patch! I'll use npm install -g @opencode/cli directly for now. Appreciate the fast help! 🙌

xDipzz avatar Nov 13 '26 17:11 xDipzz

Adding Windows data, since the patch above was not tried on Windows.

Windows 11, Node 24.21.0, npm 11.19.0, same steps as the bot: a new npm prefix and an empty cache, npm install -g @opencode/[email protected], npm's downloads slowed to 1.2 Mbit/s by a local CONNECT proxy, then opencode upgrade 2.3.0 --method npm.

In this run the 5-minute limit did not stop it: after 20 minutes npm was still downloading (about 180 MB had gone through the proxy) and opencode upgrade had printed nothing, so I killed it. But opencode was gone long before that, because npm moves the old shims aside before it starts the downloads (reify moves in its debug log). What the prefix looked like:

.opencode-ZTG5OddB          298 bytes   (the moved-aside shims; no opencode, opencode.cmd, opencode.ps1)
.opencode.cmd-TmLHSReS      150 bytes
.opencode.ps1-sljhPH5y      500 bytes
node_modules\@opencode\.cli-bJKp0fS4   2.2.7, still complete
node_modules\@opencode\cli             2.3.0, bin\opencode.exe is the 229-byte placeholder
  node_modules\@opencode\cli-windows-x64\bin\opencode.exe   187 MB, truncated

opencode --version in cmd: 'opencode' is not recognized as an internal or external command, operable program or batch file. Running bin\opencode.exe directly fails too, since it is the placeholder.

So on Windows a slow upgrade leaves no working opencode for its whole duration, and any interruption (the timeout, Ctrl+C, closing the terminal) leaves it that way. I hit the timeout version of this in October, 2.0.24 -> 2.0.28 on a ~1-3 Mbit/s link: npm could not install OpenCode ... Timed out, with the same 229-byte stub and no shims.

Workaround: run npm install -g @opencode/cli@<version> yourself.

igorsaevets avatar Nov 16 '26 03:11 igorsaevets

@igorsaevets Thanks. The timeout patch only removes automatic mid-install termination. It does not preserve npm's shims or roll back an interrupted installation. Direct npm avoids OpenCode's timer but still has the missing-command window.

I checked real 2.2.7 → 2.3.0 on macOS, Node 26.13.1/npm 12.2.1, using isolated prefixes/caches and a CONNECT proxy paused after reify moves:

  • Released updater: Timed out after 303.7 seconds; only retired command links remained.
  • Direct npm: still running with missing commands at 330 seconds. Releasing the network then let it finish at 354.0 seconds; both command links returned and opencode --version reported opencode v2.3.0.

In the source, 2.2.7 and dev b75b158396 both pass the five-minute timeout on Windows too. RetainedImage protects a link to the running image; it does not restore the shims. I cannot explain your 20-minute run from this evidence.

These new checks used macOS releases. The patched updater with real npm and Windows timeout/cleanup remain unverified. The patch is unchanged; cancellation recovery remains open. AI-assisted verification.

r266-tech avatar Nov 16 '26 04:11 r266-tech

Correction to my Windows comment above: the 20-minute run was a bug in my test script, not Windows behaviour.

The script read opencode upgrade's output through a pipe, and its reader thread crashed on the first line (Python could not encode ┌ in cp1251). With nobody reading, opencode blocked on a write once about 4 KB of spinner output filled the pipe, so its timer could not fire.

Re-run today on Windows 11, Node 24.21.0, npm 11.19.0, same setup (new prefix, empty cache, 1.2 Mbit/s proxy, upgrade --method npm):

  • 2.2.7 → 2.3.0, output to a file: taskkill /pid <npm's cmd.exe> /T /F at 301.4 s, Timed out at 302.5 s.
  • 2.3.1 → 2.3.2: taskkill at 300.8 s, Timed out at 301.9 s.
  • 2.2.7 → 2.3.0, output pipe left unread: no timeout, 4,101 bytes waiting in the pipe. When I drained it at 600 s, the timeout fired 1.2 s later.

So the released updater times out on Windows as on macOS and Linux. npm runs as cmd.exe /d /s /c npm ... → node.exe, and the timeout kills the whole tree. The end state is the one from my first comment.

Your patch (ccaa8403), same setup, run from source with Bun 1.4.4 (release version and channel set with --define): no timeout at 5 minutes. After I lifted the throttle at 361 s, npm's postinstall ran at 378 s and the upgrade ended with Done (exit 0) at 380 s. Shims back, opencode --version printed opencode v2.3.0, no process left. Not tested: Ctrl+C or closing the terminal mid-install.

igorsaevets avatar Nov 21 '26 23:11 igorsaevets