Wrangler — Agent Guide
Wrangler-specific context only. See root AGENTS.md for monorepo conventions.
Overview
Main CLI for Cloudflare Workers. ~2k-line yargs command tree in src/index.ts. Entry point is src/cli.ts (NOT src/index.ts).
Structure
src/— CLI sourcesrc/__tests__/— Unit tests, helpers insrc/__tests__/helpers/e2e/— E2E tests, requires Cloudflare credentialsbin/wrangler.js— Shim that spawns Node with--experimental-vm-modulestemplates/— Worker templates
Entry Points
src/cli.ts— Build entry AND library API surface (dual-purpose). Callsmain()when run directly; re-exports./apiwhen imported as library.src/index.ts— Yargs CLI tree builder (large file). Exportsmain(). NOT the package entry point despite the name.src/api/index.ts— Public programmatic API barrel.
Conventions (Wrangler-Specific)
- No
console.*— useloggersingleton - No
__dirname/__filename— usegetBasePath() - No global
fetch— use undici's fetch - No direct Cloudflare REST API calls — use the Cloudflare TypeScript SDK
telemetryMessagevalues forUserError-compatible errors must be static, safe labels. Do not usetelemetryMessage: trueunless the user-facing message cannot include user input, file paths, resource names, IDs, secret names, raw API messages, or command input. Even when safe,telemetryMessage: trueis usually less useful because user-facing copy is harder to group and parse than a stable telemetry label.- Format
telemetryMessagevalues as lower-case phrases:<service or area> <command or sub-area> <failure>, for examplekv namespace binding not found in config,r2 object put file not found, orpages deploy project name missing. - Keep the service/area first and the failure last so telemetry groups consistently. Prefer stable categories over user-facing copy; telemetry labels should not change just because CLI wording changes.
Build
- tsup: single entry
src/cli.ts→wrangler-dist/cli.js package.jsonmainpoints atwrangler-dist/cli.js- Custom
embedWorkersPlugin()bundles Worker templates via esbuild for tests
Testing
- Pool:
"forks"(not threads) — heavy global mocking runWrangler(cmd)callsmain()in-process (no subprocess)- MSW for API mocking — pre-built handlers per API domain in
src/__tests__/helpers/msw/ - Shared helpers:
runInTempDir(),mockAccountId(),mockApiToken(),mockConfirm(),mockPrompt(),mockSelect(),captureRequestsFrom() - Unit timeout: 15s, retry: 0 (overrides shared config)
- E2E timeout: 90s, bail: 1, singleThread
globals: truefor vitest globals- Output normalization via
normalizeString()pipeline
Anti-Patterns
- Test files use
.test.ts(not.spec.ts) - Never import
expectfrom vitest — use test context({ expect }) => {}- When adding
expectas a parameter to helper functions, check ALL call sites (e.g., acrossdeployments.test.ts,versions.test.ts)
- When adding