Skip to content

npm 12 upgrade leaves the Windows executable stub #51553

Closed

Description

@renatomoselli

Description

opencode upgrade can report success with npm 12 while leaving the published text stub in bin/opencode.exe. The next invocation fails on Windows because the file is not a valid executable.

The updater runs npm install --global --force without allowing the target package's lifecycle scripts. npm 12 can block postinstall and still exit 0, even with ignore-scripts=false. Adding --allow-scripts=@opencode/cli lets postinstall replace the stub.

I reproduced that policy behavior with a synthetic package in an isolated Windows install prefix using npm 12.0.2 and Node 24.21.0. The flag also preserved explicit ignore-scripts=true. This report covers the updater command; #48884 covers the broader packaging behavior when scripts are disabled.

OpenCode version

Reported on the upgrade from 2.0.16 to 2.0.18.

Steps to reproduce

  1. Start with a working npm installation of OpenCode 2.0.16 and npm 12.0.2, with ignore-scripts=false and no script approval for @opencode/cli.
  2. Run opencode upgrade 2.0.18 using the npm method.
  3. Run opencode --version.

The upgrade reports success, but Windows rejects bin/opencode.exe. Running the installed package's postinstall.mjs manually repairs it.

Operating System

Windows

Activity

  1. github-actions commented on Sep 26, 2026

    @github-actions
    Contributor

    This issue might be related to existing reports. Please check:

  2. linko22 commented on Oct 1, 2026

    @linko22

    Windows: self-update can brick the install (postinstall blocked + self-locked binary)

    Windows 11 25H2 (26200), npm 12.1.0, Node v24.19.0, [email protected] installed globally. No manual upgrade was run — the running opencode triggered its own npm install -g at launch, and the result was a permanently unlaunchable install.

    What happens, in order:

    13:31:59  opencode.exe PID 16312 starts
    13:32:02  5 MCP child processes
    13:32:09  npm install -g [email protected]   <- triggered by opencode itself
              npm warn cleanup EPERM ... unlink
                '...\.opencode-ai-9qjAHli1\node_modules\opencode-windows-x64\bin\opencode.exe'
              npm warn install-scripts 1 package had install scripts blocked
                because they are not covered by allowScripts:
                [email protected] (postinstall: node ./postinstall.mjs)
    13:32:20  bin/opencode.exe = 479 bytes (text stub)
    

    npm exits 0 and reports info ok. The install is silently broken.

    Afterwards every launch fails:

    opencode.exe : Указанный исполняемый файл не является действительным
                   приложением для этой операционной системы.
    opencode.ps1:14  & "$basedir/node_modules/opencode-ai/bin/opencode.exe" $args
    NativeCommandFailed
    

    Two independent causes, neither sufficient alone:

    1. npm 12 blocks postinstall by default unless the package is in allowScripts. bin/opencode.exe in the published tarball is a 479-byte text stub that postinstall.mjs is supposed to replace. Block the script and the stub ships as-is. npm still exits 0. Already tracked in npm 12 upgrade leaves the Windows executable stub #51553 and Windows: postinstall fails to copy binary from optionalDependency — stub remains 479 bytes #41370; the --allow-scripts workaround works.

    2. autoupdate defaults to true, so opencode runs npm install -g against itself while its own .exe is loaded. That produces the EPERM on unlink. The EPERM itself is harmless (npm only fails to delete the stale .opencode-ai-<hash> directory) — but combined with cause 1, the running process turns a routine self-update into a bricked install.

    The combination is the part I don't see covered. #51553 describes the updater leaving the stub; #50819 (merged 2026-09-23) fixes Windows upgrades fighting the running binary — but it's merged into the v2 branch. npm dist-tag latest is still 1.18.34 on the 1.x line, so the fix isn't reachable for anyone installing from latest, and #50819 doesn't address that autoupdate: true is what initiates the update from inside the locked process in the first place.

    Happy to backport-test if a 1.x fix or a release note is warranted.

    Workaround in use until this is fixed

    • opencode.jsonc: "autoupdate": "notify". The config schema allows true | false | "notify", but "notify" only appears in the schema description, not in the published config docs — I had to read the schema to find it. Since true is the default and true is what triggers the in-process self-update, "notify" looks like a safer default on Windows specifically.
    • npm config set allow-scripts=opencode-ai --location=user
    • A PATH shim that validates bin/opencode.exe (size + MZ signature) and repairs via node postinstall.mjs if it's the stub, then defers the update check to an exit hook where no process holds the .exe.

    Two smaller notes

    • package.json has no repository/bugs field, so tooling can't discover the tracker automatically.
    • npm 12 exiting 0 after install-scripts blocked makes this hard to detect from scripts or CI. A non-zero exit, or a stderr warning that isn't warn-level, would surface it much sooner.
  3. coygeek commented on Oct 5, 2026

    @coygeek

    Also reproduces on macOS arm64 (2.0.22 → 2.0.23)

    This is not Windows-specific. On macOS the stub is a shell script, so instead of an invalid-executable error, every invocation prints the postinstall error:

    Error: @opencode/cli's postinstall script was not run.
    
    This occurs when installation scripts are disabled.
    Run the package postinstall script or reinstall with scripts enabled.
    

    Environment: macOS arm64, Node 24.21.0, @opencode/cli 2.0.22 upgrading to 2.0.23. I ran it in an isolated prefix with a fresh HOME, npm cache and npmrc, and no allow-scripts configured:

    1. npm install --global --allow-scripts=@opencode/cli @opencode/[email protected], then opencode --version printed opencode v2.0.22.
    2. opencode upgrade 2.0.23 --method npm
    3. opencode --version and opencode2 --version
    npm Step 2 Step 3
    12.0.2 (2 runs) Using method: npm, Upgrade complete, exit 0 postinstall error above, exit 1
    12.2.0 (current latest) Upgrade complete, exit 0 postinstall error above, exit 1
    11.19.0 (control) Upgrade complete, exit 0 opencode v2.0.23, exit 0

    The updater's command, npm install --global --force @opencode/[email protected], exits 0 under npm 12.0.2 with only this warning:

    npm warn install-scripts 1 package had install scripts blocked because they are not covered by allowScripts:
    npm warn install-scripts   @opencode/[email protected] (postinstall: node ./postinstall.mjs)
    

    The same failure first appeared on a regular npm install upgraded under npm 12.0.2. From the broken state, either of these restored a working 2.0.23:

    • node ./postinstall.mjs in the installed package directory
    • npm install --global --force --allow-scripts=@opencode/cli @opencode/[email protected]

    On the v2 branch at 88dd4417c7e9, packages/cli/src/services/updater.ts still passes no allow flag to npm, though pnpm gets --allow-build and bun gets --trust. #51554 covers this case on macOS too. Separately, it might be worth having opencode upgrade check that the new binary actually runs before printing Upgrade complete.

  4. baize-1-heic commented on Oct 15, 2026

    @baize-1-heic

    Confirming this on 2.0.30 with npm 12.0.2 (Windows 11, global npm install channel):

    • opencode upgrade reports success (From 2.0.29 → 2.0.30) but bin\opencode.exe is left as the 229-byte stub, so opencode --version fails with "The specified executable is not a valid application for this OS platform".
    • The platform packages are installed correctly at 2.0.30 (@opencode/cli-windows-x64 and @opencode/cli-windows-x64-baseline, both containing a valid 213,522,824-byte bin\opencode.exe), so only the postinstall link step is skipped.
    • npm config get ignore-scripts is false here, and there is no ignore-scripts=true in the user or global npmrc.
    • Workaround without reinstalling: cd "$env:APPDATA\npm\node_modules\@opencode\cli"; node postinstall.mjs — this produces a working 213,522,824-byte launcher.
  5. GuzhouYiyun commented on Oct 16, 2026

    @GuzhouYiyun

    Windows + npm 12.2.0 data point, plus the persistent-config answer.

    Same failure, end to end. Windows 11 (10.0.26300.9711), Node 24.21.0, npm 12.2.0, @opencode/cli 2.0.30, global npm install, channel latest. Update was triggered from the TUI with /update, so the npm install ran from inside the already-running process; it reported success, but the first launch afterwards failed. bin\opencode.exe had been left as the 229-byte stub. npm config get ignore-scripts is false and there is no allowScripts entry anywhere, so nothing is opting out explicitly - this is npm 12's default. That extends the Windows 12.0.2 data above to 12.2.0.

    Confirmed workaround, no uninstall needed. Re-running the same global install with the flag repaired the broken install in place:

    npm i -g @opencode/cli --allow-scripts=@opencode/cli
    

    No uninstall and no clean prefix was required - the postinstall simply replaced the stub. Running node postinstall.mjs in the installed package directory also repairs it without going through npm.

    Persistent config instead of a per-invocation flag. Repeating --allow-scripts=@opencode/cli on every install and update is easy to forget. npm documents that flag as the route for -g / npx (there is no project package.json for npm approve-scripts, which fails with EGLOBAL), and npm config set as the persistent form:

    npm config set allow-scripts=@opencode/cli --location=user
    

    I have not tested that end-to-end, but it follows from the documented npm behaviour, and it answers the question asked on #51554. A line in the docs or troubleshooting page would turn "remember the flag on every update" into one-time setup.

    Docs gap on the install page. https://opencode.ai/v2/docs currently lists bun install -g --trust @opencode/cli, pnpm add -g --allow-build=@opencode/cli, and then a bare npm install -g @opencode/cli. On npm 12 that last command produces an install that cannot run. The npm entry should carry --allow-scripts=@opencode/cli as well, and troubleshooting should mention the no-reinstall repair:

    cd "$(npm root -g)/@opencode/cli" && node postinstall.mjs
    # Windows:
    cd "$env:APPDATA\npm\node_modules\@opencode\cli"; node postinstall.mjs
    

    Root cause worth reviving. The updater fix is #51554. The packaging fix that removes the postinstall dependency altogether is the runtime launcher in #48885, which was auto-closed by the PR cleanup on 2026-10-14 while #48884 is still open. It seems worth judging on its merits rather than letting it age out.

  6. shehryaraijaz commented on Oct 20, 2026

    @shehryaraijaz

    I’m seeing the same issue on a newer OpenCode 2 release.

    Environment:

    • Windows 11 x64 (AMD64)
    • Node.js v24.15.0
    • npm 12.0.2
    • @opencode/cli v2.0.30, updated to v2.1.1

    I started with a working v2.0.30 installation and ran /update from the TUI. The update reported success, but afterward, running opencode failed with:

    Program 'opencode.exe' failed to run:
    The specified executable is not a valid application for this OS platform.
    

    PowerShell showed that opencode.ps1 was trying to run:

    C:\nvm4w\nodejs\node_modules\@opencode\cli\bin\opencode.exe
    

    That file was only 229 bytes and did not have a valid Windows executable header. The matching platform packages, @opencode/[email protected] and @opencode/[email protected], were both present and contained valid Windows executables, each 218,766,376 bytes.

    The CLI package defines a postinstall script that selects and copies the platform executable into bin\opencode.exe. The evidence suggests that this step did not run during /update, leaving the small placeholder file in place.

    I repaired the installation without uninstalling it by running this from the package directory:

    cd C:\nvm4w\nodejs\node_modules\@opencode\cli
    node .\postinstall.mjs
    

    Afterward, opencode --version reported opencode v2.1.1.

    I also configured npm to allow the package’s install script:

    npm config set allow-scripts=@opencode/cli --location=user
    
  7. BingWuJ commented on Oct 24, 2026

    @BingWuJ

    Reproduced on npm 11.6.2 and this is not limited to npm 12.

    Environment

    OS Windows 11 (10.0.26200)
    npm 11.6.2
    Node v24.12.0
    @opencode/cli 2.1.4
    Shell PowerShell 7.6.4

    Symptoms

    opencode is not resolvable at all:

    PS> opencode
    opencode: The term 'opencode' is not recognized as a name of a cmdlet,
    function, script file, or executable program.
    

    No partial install warning, no repair hint.

    Evidence

    1. bin/opencode.exe is the 229-byte text stub

    %APPDATA%\npm\node_modules\@opencode\cli\bin\opencode.exe   229 bytes
    

    Content is the postinstall-failure echo script, not a PE binary.

    2. The real binary was truncated

    %APPDATA%\npm\node_modules\@opencode\cli\node_modules\@opencode\cli-windows-x64\bin\opencode.exe
      actual   149,712,037 bytes
      expected 246,919,797 bytes   (registry dist.unpackedSize)
    

    The header is a valid MZ / PE\0\0 / x64 / PE32+, so it is not a format problem — the file simply stops short. Executing it returns The specified executable is not a valid application for this OS platform.

    3. npm shims never got their final names

    %APPDATA%\npm\.opencode-oJkUTGoE          298 bytes
    %APPDATA%\npm\.opencode.cmd-Fbxo0trL      150 bytes
    %APPDATA%\npm\.opencode.ps1-uF7xkN5x      500 bytes
    

    These are npm's in-flight temp names. The rename to opencode.cmd never happened, which is why PATH has no opencode at all.

    4. ignore-scripts is false

    PS> npm config get ignore-scripts
    false
    

    Why this is worth a look

    I cannot tell from the user side whether npm skipped the postinstall or the extraction was interrupted — but both end in the same state: a text stub, temp shims, a truncated binary, and a command not found error that points at "not installed" instead of "installed corrupt".

    That is the part that cost time: the reported symptom and the actual cause are unrelated.

    It also suggests the #51554 fix may need to cover more than the upgrade path, and npm 11 as well as 12.

    Recovery that worked

    npm uninstall -g @opencode/cli
    Remove-Item "$env:APPDATA\npm\node_modules\@opencode" -Recurse -Force
    Remove-Item "$env:APPDATA\npm\.opencode*" -Force
    npm cache clean --force
    npm install -g @opencode/cli --foreground-scripts

    Completed in 40s, bin/opencode.exe became 246,919,560 bytes, opencode v2.1.4 works.

    Note: cleaning only the package directory is not enough — the stale temp shims must go too, otherwise the reinstall leaves a half-registered command.

    Suggestions

    1. After postinstall, verify bin/opencode.exe is larger than the stub (or matches the platform package's expected size) and fail loudly if not.
    2. On startup, if the stub is detected, print "installation incomplete, reinstall with ..." instead of letting the shell report command not found.
    3. Teach the installer to remove .opencode* temp shims before creating new ones.
  8. opencodejr commented on Oct 28, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report. I could reproduce this, and it isn't specific to Windows: the published bin/opencode.exe is a stub on every platform until postinstall replaces it.

    Steps (on Linux, npm 12.0.2, Node 24.21.0, isolated global prefix, ignore-scripts=false, no allow-scripts config):

    1. Install a working @opencode/[email protected] (scripts allowed once); opencode --version prints v2.0.16.
    2. Run opencode upgrade --method npm 2.0.18. It prints "Upgrade complete" and exits 0.
    3. Run opencode --version. It fails with @opencode/cli's postinstall script was not run. package.json says 2.0.18, but bin/opencode.exe is still the stub.

    Running the same install by hand with --allow-scripts=@opencode/cli gives a working 2.0.18. The npm command in the updater on the latest dev (86ce19d) still has no such flag, while pnpm (--allow-build) and bun (--trust) already get one. Upgrading with the latest release (2.2.0) fails the same way.

    The script below reproduces it. Related: #48884, the broader issue of the package not working when install scripts are disabled.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduces #51553 on Linux: with npm 12, `opencode upgrade` (npm method) exits 0
    # but npm blocks @opencode/cli's postinstall, leaving the bin/opencode.exe stub.
    # Set OC_DEV=/path/to/checkout to run the upgrade command from source instead.
    set -u
    W=/tmp/opencode/r51553; rm -rf "$W"; mkdir -p "$W/npm12" "$W/npmbin" "$W/pfx" "$W/home"
    (cd "$W/npm12" && npm install --silent [email protected] >/dev/null 2>&1)
    ln -sf "$W/npm12/node_modules/.bin/npm" "$W/npmbin/npm"
    export PATH="$W/pfx/bin:$W/npmbin:$PATH" npm_config_prefix="$W/pfx" HOME="$W/home"
    echo "npm $(npm --version); ignore-scripts=$(npm config get ignore-scripts)"
    
    # Working 2.0.16 install (scripts explicitly allowed once).
    npm install --global --allow-scripts=@opencode/cli @opencode/[email protected] >/dev/null 2>&1
    echo "before: $(opencode --version)"
    
    cd "$W"
    if [ -n "${OC_DEV:-}" ]; then
      bun --conditions=browser --preload="$OC_DEV/packages/tui/node_modules/@opentui/solid/scripts/preload.js" \
        "$OC_DEV/packages/cli/src/index.ts" upgrade --method npm 2.0.18 2>&1 | tr '\r' '\n' | grep -E "Using|From|complete|Done|rror" ; echo "upgrade exit=${PIPESTATUS[0]}"
    else
      opencode upgrade --method npm 2.0.18 2>&1 | tr '\r' '\n' | grep -E "Using|From|complete|Done|rror"; echo "upgrade exit=${PIPESTATUS[0]}"
    fi
    
    echo "installed package.json version: $(node -p "require('$W/pfx/lib/node_modules/@opencode/cli/package.json').version")"
    echo "--- opencode --version after upgrade:"
    opencode --version; echo "exit=$?"
    echo "--- bin/opencode.exe head:"; head -c 80 "$W/pfx/lib/node_modules/@opencode/cli/bin/opencode.exe"; echo

    Run it: bash repro.sh

  9. deathman915-sys commented on Nov 2, 2026

    @deathman915-sys

    Still reproducing after this was closed, on the current release.

    Environment

    • Upgrade: opencode upgrade (method npm), 2.2.2 → 2.2.5
    • Date: 2026-11-02
    • OS: Windows 10.0.19045.7807 (win32 x64)
    • npm: 12.2.1, ignore-scripts=false, no ignore-scripts in user or global npmrc
    • Install: global npm at %APPDATA%\npm\node_modules\@opencode\cli
    • Active plugins: one local plugin (mempalace-bootstrap)

    Reproduction

    1. Working global npm install of @opencode/[email protected]; opencode --version prints opencode v2.2.2.
    2. Run opencode upgrade. It prints Using method: npm, From 2.2.2 → 2.2.5, Upgrade complete, and exits 0.
    3. Run opencode --version. It fails immediately:
    Program 'opencode.exe' failed to run: The specified executable is not a valid application for this OS platform.
    

    %APPDATA%\npm\node_modules\@opencode\cli\bin\opencode.exe is left as the 229-byte tarball stub:

    echo "Error: @opencode/cli's postinstall script was not run." >&2
    ...
    exit 1
    

    The optional platform packages installed correctly at 2.2.5 (@opencode/cli-windows-x64\bin\opencode.exe = 259,169,160 bytes), so this is only the missing postinstall hardlink/copy step.

    Evidence the described fix is not present in 2.2.5

    The shipped 2.2.5 binary does not contain the literal --allow-scripts, while other updater strings are present. Calibrated with rg -a -F over bin/opencode.exe (259,169,160 bytes):

    string result
    Upgrade complete found
    Using method found
    postinstall found
    npm_execpath found
    --force found
    ignore-scripts found
    --allow-scripts not found

    So the npm method still does not receive the flag that pnpm (--allow-build) and bun (--trust) already get, and an upgrade performed from 2.2.2 still produces the stub with exit code 0.

    Manual recovery worked here (relevant to #56380, which reports the recommended recovery cannot work on Windows):

    cd "$env:APPDATA\npm\node_modules\@opencode\cli"
    node postinstall.mjs   # exit 0

    Afterwards bin\opencode.exe is 259,169,160 bytes and fsutil hardlink list shows it linked to cli-windows-x64\bin\opencode.exe (so no extra disk cost). opencode --version then prints opencode v2.2.5.

    It succeeded because the platform package was already installed: resolveBinary() + copyBinary() satisfied the loop, and installPackage() — the only path that spawns npm and therefore the only one that can hit ENOENT — was never reached. The recovery appears to fail only when the platform package must actually be fetched.

    Impact

    A user on any pre-fix version who upgrades to the current release loses a runnable CLI, and the upgrade reports success with exit code 0. The only hint is the stub's own text, which contradicts the success banner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions