Skip to content

fix(cli): fix TypeError: fetch failed with proxy env vars on newer Node (undici major mismatch) - #17634

Open
sleitor wants to merge 3 commits into
vercel:mainfrom
sleitor:fix-17629
Open

sleitor wants to merge 3 commits into
vercel:mainfrom
sleitor:fix-17629

Conversation

@sleitor

@sleitor sleitor commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

What & why

Fixes #17629 — on Node.js 26 (and any Node release whose bundled undici major differs enough from the CLI's bundled [email protected]), every CLI command that goes through the network fails with:

TypeError: fetch failed
  cause: UND_ERR_INVALID_ARG: invalid onError method

as soon as any proxy env var (HTTP_PROXY/HTTPS_PROXY/ALL_PROXY, lower or upper case) is set.

Root cause

hasProxyConfig() in packages/cli/src/index.ts installs a custom EnvProxyDispatcher (built on the CLI's bundled [email protected] Agent/ProxyAgent) as the dispatcher for Node's native, built-in fetch. Node's native fetch is backed by whichever undici major version ships internally with that particular Node release (undici 7 on Node 24, undici 8 on Node 26, etc.), and the internal handler/dispatcher shape it expects from a dispatcher option is not a stable, cross-version public API. undici@8 removed the legacy handler wrappers that undici@5's Agent.dispatch() relies on being called with, so handing a v5-built dispatcher to a v8-backed native fetch throws.

Fix

  • packages/cli/src/util/fetch.ts: when a custom dispatcher is configured (i.e. HTTP_PROXY/HTTPS_PROXY/etc. is set), route the request through the CLI's bundled undici package's own fetch() instead of globalThis.fetch. This guarantees the dispatcher and the fetch implementation always come from the same undici major version and therefore always agree on the handler shape — regardless of what undici version the host Node release happens to embed internally. This fixes the crash on Node 26 and is forward-compatible with future Node/undici bumps, without needing to special-case Node versions.
  • packages/error-utils/src/index.ts: errorToString() now appends the message of Error#cause when present, instead of silently discarding it (suggestion Pass flags to subcommands #3 in the issue). Previously --debug output only showed the generic TypeError: fetch failed with no indication of the real underlying cause, which made this look like a network/sandbox problem rather than a CLI bug.

Alternatives considered

The issue also suggests skipping the custom dispatcher entirely on Node versions that support NODE_USE_ENV_PROXY (v22.21+/v24.5+) and letting Node's native fetch proxy natively. I tested this and it doesn't work as a simple version check: NODE_USE_ENV_PROXY is only honored if it's set in the process's environment before Node starts — setting process.env.NODE_USE_ENV_PROXY from CLI code at runtime has no effect (verified locally), since Node reads it during bootstrap. Making it work would require re-exec'ing the whole CLI process with a modified env once the proxy config is detected, which is invasive for an interactive CLI (TTY passthrough, signal handling, partial side effects already run earlier in main()) and didn't seem worth the risk compared to the version-agnostic fetch/dispatcher-pairing fix above, which fixes the crash outright without depending on the runtime Node version at all.

Testing

  • Added a regression test in packages/cli/test/unit/util/fetch.test.ts asserting that fetch() never calls globalThis.fetch when a dispatcher is configured (routes through bundled undici's fetch instead), plus verifying the response still round-trips correctly through a MockAgent.
  • Added tests in packages/error-utils/test/index.test.ts for errorToString() surfacing Error/string/non-error cause values.
  • pnpm run lint / pnpm run format:check pass.
  • Existing proxy-selection tests (EnvProxyDispatcher HTTP/HTTPS/NO_PROXY handling) are untouched and still pass.

Closes #17629

… Node

When HTTP_PROXY/HTTPS_PROXY/ALL_PROXY is set, the CLI installs a custom
EnvProxyDispatcher (built on the bundled [email protected] Agent/ProxyAgent)
as the dispatcher for Node's *native* fetch. Native fetch is backed by
whichever undici major version ships internally with that Node release,
and the dispatcher/handler shape it expects is not a stable, cross-version
public API. On Node 26 (bundled undici 8, which removed legacy handler
wrappers) this throws:

  TypeError: fetch failed
    cause: UND_ERR_INVALID_ARG: invalid onError method

Fix: when a custom dispatcher is configured, route the request through the
CLI's *bundled* undici package's own fetch() instead of globalThis.fetch,
so the dispatcher and the fetch implementation always agree on the handler
shape regardless of the host Node version.

Also surface Error#cause in CLI error output (errorToString), which was
previously discarded and made proxy/network failures look like an opaque
'fetch failed' with no indication of the real cause.

Fixes vercel#17629
@sleitor
sleitor requested review from a team as code owners September 20, 2026 10:38
@changeset-bot

changeset-bot Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: af96698

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
vercel Patch
@vercel/client Patch
@vercel/error-utils Patch
@vercel-internals/builder-orchestration Patch
@vercel-internals/cli-builder-integration Patch
@vercel/frameworks Patch
@vercel/fs-detectors Patch
@vercel/node Patch
@vercel/remix-builder Patch
@vercel-internals/runtimes Patch
@vercel-internals/service-topology Patch
@vercel/elysia Patch
@vercel/express Patch
@vercel/fastify Patch
@vercel/h3 Patch
@vercel/hono Patch
@vercel/koa Patch
@vercel/nestjs Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

@sleitor is attempting to deploy a commit to the mobile-ent-vtest314 Team on Vercel.

A member of the Team first needs to authorize it.

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

@vercel/client's fetchApi hands a bundled-undici@5 dispatcher to the runtime's native globalThis.fetch, so vercel deploy/vercel redeploy still throw TypeError: fetch failed on Node 26 when a proxy is configured.

Fix on Vercel

…is set

@vercel/client's fetchApi() was still handing the CLI's proxy-aware
EnvProxyDispatcher (built on the bundled [email protected]) straight to
globalThis.fetch, the same TypeError: fetch failed crash the sibling
CLI fix in packages/cli/src/util/fetch.ts addresses did not cover this
path. vercel deploy/redeploy and other client upload/continue/status
calls go through fetchApi, so they would still throw on Node 26 (or
any Node whose bundled undici major differs from 5) when a proxy env
var is set.

Route fetchApi through undici's own fetch() when opts.dispatcher is
present, matching the CLI wrapper's approach, so the dispatcher and
fetch implementation always agree on the handler shape regardless of
the host Node version.

Addresses VADE bot review feedback on vercel#17634.
@sleitor

sleitor commented Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Good catch on the "Additional Suggestion" — you were right that @vercel/client's fetchApi() was a separate hot path that still handed the same undici@5-built EnvProxyDispatcher straight to globalThis.fetch, unfixed by the CLI-only wrapper change. That path backs vercel deploy/vercel redeploy (process-deployment.ts → deploy.ts/upload.ts/continue.ts/check-deployment-status.ts), so it would still throw TypeError: fetch failed on Node 26 with a proxy set.

Fixed in a0baeab: fetchApi() now routes through undici's own fetch() when opts.dispatcher is set, mirroring the CLI wrapper. Added undici as a proper dependency of @vercel/client (already pinned at 5.29.0 in the CLI) and two regression tests (dispatcher-present routes through bundled undici and never touches native fetch; no-dispatcher case still uses native fetch). Extended the changeset to cover @vercel/client. Lint/format/type-check/tests all pass.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

1 participant