fix: handle undefined __filename in bundled environments#4812
Merged
Conversation
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
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4812 +/- ##
==========================================
+ Coverage 93.26% 93.27% +0.01%
==========================================
Files 109 109
Lines 34098 34106 +8
==========================================
+ Hits 31801 31812 +11
+ Misses 2297 2294 -3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
ronag
approved these changes
Feb 6, 2026
metcoder95
approved these changes
Feb 6, 2026
Merged
slagiewka
pushed a commit
to slagiewka/undici
that referenced
this pull request
Feb 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When undici is bundled inside Node.js as an internal module,
__filenameis not defined. This causes aReferenceErrorwhenfetch()fails, which swallows the original error and replaces it with the ReferenceError, makingerr.causeundefined.This was introduced in #4778.
The Problem
The code added in #4778:
When bundled in Node.js internals,
__filenameis not available, causing:This error is thrown inside the
.catch()handler, replacing the original fetch error. As a result,err.causebecomesundefinedinstead of containing the actual error.The Fix
__filenameat module load time, defaulting toundefinedif not available__filenameis availableError.captureStackTrace(err)behavior when__filenameis not availableTesting
npx borp -p "test/fetch/client-error-stack-trace.js"test-permission-net-fetch.jstest-tls-set-default-ca-certificates-append-fetch.mjstest-tls-set-default-ca-certificates-reset-fetch.mjsRefs: nodejs/node#61683, nodejs/node#61679