Repository navigation
fix(cli): fix TypeError: fetch failed with proxy env vars on newer Node (undici major mismatch) - #17634
fix(cli): fix TypeError: fetch failed with proxy env vars on newer Node (undici major mismatch)#17634sleitor wants to merge 3 commits into
Conversation
… 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
🦋 Changeset detectedLatest commit: af96698 The changes in this PR will be included in the next version bump. This PR includes changesets to release 18 packages
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 |
|
@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. |
…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.
|
Good catch on the "Additional Suggestion" — you were right that Fixed in a0baeab: |
What & why
Fixes #17629 — on Node.js 26 (and any Node release whose bundled
undicimajor differs enough from the CLI's bundled[email protected]), every CLI command that goes through the network fails with:as soon as any proxy env var (
HTTP_PROXY/HTTPS_PROXY/ALL_PROXY, lower or upper case) is set.Root cause
hasProxyConfig()inpackages/cli/src/index.tsinstalls a customEnvProxyDispatcher(built on the CLI's bundled[email protected]Agent/ProxyAgent) as thedispatcherfor Node's native, built-infetch. Node's native fetch is backed by whicheverundicimajor 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 adispatcheroption is not a stable, cross-version public API.undici@8removed the legacy handler wrappers thatundici@5'sAgent.dispatch()relies on being called with, so handing a v5-built dispatcher to a v8-backed nativefetchthrows.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 bundledundicipackage's ownfetch()instead ofglobalThis.fetch. This guarantees the dispatcher and the fetch implementation always come from the sameundicimajor version and therefore always agree on the handler shape — regardless of whatundiciversion 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 ofError#causewhen present, instead of silently discarding it (suggestion Pass flags to subcommands #3 in the issue). Previously--debugoutput only showed the genericTypeError: fetch failedwith 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_PROXYis only honored if it's set in the process's environment before Node starts — settingprocess.env.NODE_USE_ENV_PROXYfrom 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 inmain()) 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
packages/cli/test/unit/util/fetch.test.tsasserting thatfetch()never callsglobalThis.fetchwhen a dispatcher is configured (routes through bundled undici's fetch instead), plus verifying the response still round-trips correctly through aMockAgent.packages/error-utils/test/index.test.tsforerrorToString()surfacingError/string/non-errorcausevalues.pnpm run lint/pnpm run format:checkpass.EnvProxyDispatcherHTTP/HTTPS/NO_PROXY handling) are untouched and still pass.Closes #17629