Skip to content

Conversation

@mcollina
Copy link
Member

@mcollina mcollina commented Feb 6, 2026

Summary

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 was introduced in #4778.

The Problem

The code added in #4778:

module.exports.fetch = function fetch (init, options = undefined) {
  return fetchImpl(init, options).catch(err => {
    appendFetchStackTrace(err, __filename)  // __filename is undefined!
    throw err
  })
}

When bundled in Node.js internals, __filename is not available, causing:

ReferenceError: __filename is not defined

This error is thrown inside the .catch() handler, replacing the original fetch error. As a result, err.cause becomes undefined instead of containing the actual error.

The Fix

  1. Safely capture __filename at module load time, defaulting to undefined if not available
  2. Use the new stack trace augmentation when __filename is available
  3. Fall back to the original Error.captureStackTrace(err) behavior when __filename is not available

Testing

  • Verified undici's stack trace tests still pass: npx borp -p "test/fetch/client-error-stack-trace.js"
  • Verified Node.js tests pass with the fix:
    • test-permission-net-fetch.js
    • test-tls-set-default-ca-certificates-append-fetch.mjs
    • test-tls-set-default-ca-certificates-reset-fetch.mjs

Refs: nodejs/node#61683, nodejs/node#61679

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-commenter
Copy link

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.27%. Comparing base (9cc025b) to head (cbe533b).

Files with missing lines Patch % Lines
index.js 77.77% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina merged commit 92e38fc into main Feb 6, 2026
36 of 37 checks passed
@github-actions github-actions bot mentioned this pull request Feb 6, 2026
@aduh95 aduh95 deleted the fix/filename-undefined-in-bundled-env branch February 8, 2026 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants