Skip to content

fix(cli): run bundled wrangler binary instead of npx - #265

Merged
stordahl merged 2 commits into
mainfrom
fix/cli-bundled-wrangler
Sep 10, 2026
Merged

stordahl merged 2 commits into
mainfrom
fix/cli-bundled-wrangler

Conversation

@stordahl

@stordahl stordahl commented Sep 6, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #256

Problem

Running npx @counterscale/cli@latest install fails with:

✘ [ERROR] Processing /var/folders/.../counterscale-XXXX/wrangler.json configuration:
    - "assets.bucket" is a required field.

Root cause

The CLI spawned bare npx wrangler for every wrangler invocation (whoami, secret list, secret put, deploy). npx resolves wrangler from the user's environment — the npx cache entry keyed by the bare wrangler spec (which is never auto-upgraded once cached), PATH, or a parent node_modules — not the wrangler bundled with @counterscale/cli.

When that resolution lands on wrangler < 3.78.10, it misinterprets the modern assets: { directory } config in packages/server/wrangler.json as legacy Workers Sites config and throws exactly this error (documented in Cloudflare's troubleshooting docs).

Verified by running wrangler 3.60.0 against a CLI-staged config (reproduces the exact error), while the wrangler bundled with the CLI (4.x) parses the same config fine.

Fix

  • Add getBundledWranglerBinPath() which resolves bin/wrangler.js inside the CLI's own pinned wrangler@^4.23.0 dependency (via createRequire(...).resolve("wrangler/package.json"), which wrangler's exports map allows)
  • All spawn sites now run node <bundled-bin> via wranglerArgv(), falling back to the old npx wrangler behavior only if resolution fails
  • The fallback is no longer silent: a console.warn explains which wrangler is being used if the bundled binary can't be resolved; the resolved path is memoized
  • Note: wrangler 4.x requires Node 18+, so node <bin> implicitly raises the runtime floor — @counterscale/cli already requires Node >= 20 (engines field), so this is a non-issue in practice

Testing

  • Reproduced the reported error with a real old wrangler against a CLI-staged config; confirmed the bundled wrangler parses it successfully
  • 6 new tests in cloudflare.test.ts covering bundled-binary resolution, memoization, the argv used by whoami / secret list, the npx fallback, and the resolution-failure warnings
  • @counterscale/cli: 109/109 tests pass, typecheck clean

Follow-up

`npx wrangler` resolves whatever wrangler is cached in ~/.npm/_npx for
the bare "wrangler" spec (or on PATH), which can be an old version that
misreads the modern `assets.directory` config in wrangler.json and fails
with "assets.bucket is a required field".

Run the wrangler binary bundled with @counterscale/cli's own dependency
tree instead, falling back to `npx wrangler` only if resolution fails.

Fixes #256
@codecov

codecov Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.68%. Comparing base (fe226f6) to head (fb6195f).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #265      +/-   ##
==========================================
- Coverage   83.90%   82.68%   -1.22%     
==========================================
  Files          55       56       +1     
  Lines        1882     2010     +128     
  Branches      518      539      +21     
==========================================
+ Hits         1579     1662      +83     
- Misses        267      310      +43     
- Partials       36       38       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Address PR review: the silent npx fallback made a future resolution
failure (e.g. wrangler changing its exports map) degrade invisibly back
to the bug this PR fixes. Also memoize the resolved binary path.
@stordahl
stordahl merged commit 957ef50 into main Sep 10, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: "assets.buckets is a required field" error when running the install command in the README

1 participant