From cbe533bc59d2a32848967db17c960fa653b098b4 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 6 Feb 2026 09:03:13 +0100 Subject: [PATCH] fix: handle undefined __filename in bundled environments When undici is bundled inside Node.js as an internal module, `__filename` is not defined. This causes a ReferenceError when fetch() fails, which swallows the original error and replaces it with the ReferenceError, making `err.cause` undefined. This fix: 1. Safely captures `__filename` at module load time, defaulting to `undefined` if not available 2. Uses the new stack trace augmentation when `__filename` is available 3. Falls back to the original `Error.captureStackTrace(err)` behavior when `__filename` is not available Fixes failures in Node.js tests: - test-permission-net-fetch.js - test-tls-set-default-ca-certificates-append-fetch.mjs - test-tls-set-default-ca-certificates-reset-fetch.mjs --- index-fetch.js | 10 +++++++++- index.js | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/index-fetch.js b/index-fetch.js index 0180b5865b9..3d1b811daa4 100644 --- a/index-fetch.js +++ b/index-fetch.js @@ -4,6 +4,10 @@ const { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global') const EnvHttpProxyAgent = require('./lib/dispatcher/env-http-proxy-agent') const fetchImpl = require('./lib/web/fetch').fetch +// Capture __filename at module load time for stack trace augmentation. +// This may be undefined when bundled in environments like Node.js internals. +const currentFilename = typeof __filename !== 'undefined' ? __filename : undefined + function appendFetchStackTrace (err, filename) { if (!err || typeof err !== 'object') { return @@ -30,7 +34,11 @@ function appendFetchStackTrace (err, filename) { module.exports.fetch = function fetch (init, options = undefined) { return fetchImpl(init, options).catch(err => { - appendFetchStackTrace(err, __filename) + if (currentFilename) { + appendFetchStackTrace(err, currentFilename) + } else if (err && typeof err === 'object') { + Error.captureStackTrace(err, module.exports.fetch) + } throw err }) } diff --git a/index.js b/index.js index 0dc12d62ecd..271c64e8ec3 100644 --- a/index.js +++ b/index.js @@ -121,6 +121,10 @@ module.exports.getGlobalDispatcher = getGlobalDispatcher const fetchImpl = require('./lib/web/fetch').fetch +// Capture __filename at module load time for stack trace augmentation. +// This may be undefined when bundled in environments like Node.js internals. +const currentFilename = typeof __filename !== 'undefined' ? __filename : undefined + function appendFetchStackTrace (err, filename) { if (!err || typeof err !== 'object') { return @@ -147,7 +151,11 @@ function appendFetchStackTrace (err, filename) { module.exports.fetch = function fetch (init, options = undefined) { return fetchImpl(init, options).catch(err => { - appendFetchStackTrace(err, __filename) + if (currentFilename) { + appendFetchStackTrace(err, currentFilename) + } else if (err && typeof err === 'object') { + Error.captureStackTrace(err, module.exports.fetch) + } throw err }) }