Skip to content

Conversation

@cursor
Copy link

@cursor cursor bot commented Jan 7, 2026

Replace synchronous createRequire with asynchronous dynamic import() for Sentry loading.

This fixes a TypeError in bundled CommonJS contexts where __filename is undefined, causing createRequire to receive an invalid filename argument. The new approach uses dynamic import() which is more robust in various module environments and gracefully handles Sentry loading.


Open in Cursor Open in Web


Note

Shifts Sentry integration in logger.ts to asynchronous dynamic import with safe, single-attempt loading and caching.

  • Replaces createRequire/sync require('@sentry/node') with await import('@sentry/node'), adding sentryLoadAttempted to prevent repeated loads
  • Updates withSentry to fire-and-forget async loading and safely invoke callbacks without throwing
  • Preserves environment-based disabling and test-environment suppression, with unchanged logging behavior to stderr

Written by Cursor Bugbot for commit a9cad89. This will update automatically on new commits. Configure here.

Co-authored-by: cameron.cooke <cameron.cooke@sentry.io>
@cursor
Copy link
Author

cursor bot commented Jan 7, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 7, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cameroncooke/XcodeBuildMCP/xcodebuildmcp@168

commit: a9cad89

@cameroncooke cameroncooke marked this pull request as ready for review January 7, 2026 21:44

sentryLoadAttempted = true;
try {
cachedSentry = require('@sentry/node') as SentryModule;
Copy link
Author

Choose a reason for hiding this comment

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

Early calls during Sentry loading silently dropped

Medium Severity

The loadSentryAsync function sets sentryLoadAttempted = true before the async import() completes. Any log calls that occur while the first import is still in progress will find sentryLoadAttempted is true but cachedSentry is still null, causing the function to return null immediately. This means error messages logged during the Sentry loading window are silently lost and never sent to Sentry. The loading promise could be stored and returned to subsequent callers to ensure all callbacks execute once loading completes.

Fix in Cursor Fix in Web

Copy link
Owner

Choose a reason for hiding this comment

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

@cursor fix

Copy link
Author

@cursor cursor bot Jan 7, 2026

Choose a reason for hiding this comment

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

❌ Unable to add follow-up after multiple attempts. The agent may be experiencing issues.

Please try again later or Open in Web to view the status.


Open in Cursor Open in Web

Learn more about Cursor Agents

Copy link
Owner

Choose a reason for hiding this comment

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

@cursor fix

Copy link
Author

@cursor cursor bot Jan 8, 2026

Choose a reason for hiding this comment

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

❌ Unable to add follow-up after multiple attempts. The agent may be experiencing issues.

Please try again later or Open in Web to view the status.


Open in Cursor Open in Web

Learn more about Cursor Agents

Comment on lines 63 to 72
if (!SENTRY_ENABLED || isTestEnv()) return null;
if (cachedSentry) return cachedSentry;
if (sentryLoadAttempted) return null;

sentryLoadAttempted = true;
try {
cachedSentry = require('@sentry/node') as SentryModule;
cachedSentry = await import('@sentry/node');
return cachedSentry;
} catch {
// If @sentry/node is not installed in some environments, fail silently.
Copy link

Choose a reason for hiding this comment

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

Bug: The switch to asynchronous Sentry loading in withSentry creates a race condition, causing error logs to be lost if the process exits before the async operation completes.
Severity: CRITICAL | Confidence: High

🔍 Detailed Analysis

The withSentry function was changed to load Sentry asynchronously using loadSentryAsync().then(...). This defers the execution of the callback, which sends the error report, to a future microtask. In scenarios where the application exits shortly after logging an error, such as in fatal error handlers that call process.exit(), the process will terminate before the microtask queue is processed. This will cause the error log to be lost and never sent to Sentry, defeating the purpose of the error reporting mechanism, especially for unhandled exceptions where immediate process termination is common.

💡 Suggested Fix

Revert withSentry to its previous synchronous implementation. Since @sentry/node is already loaded synchronously in other parts of the application, the asynchronous loading is unnecessary. If async loading must be kept, error handlers should be modified to await the logging operation's completion before exiting the process.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/utils/logger.ts#L63-L72

Potential issue: The `withSentry` function was changed to load Sentry asynchronously
using `loadSentryAsync().then(...)`. This defers the execution of the callback, which
sends the error report, to a future microtask. In scenarios where the application exits
shortly after logging an error, such as in fatal error handlers that call
`process.exit()`, the process will terminate before the microtask queue is processed.
This will cause the error log to be lost and never sent to Sentry, defeating the purpose
of the error reporting mechanism, especially for unhandled exceptions where immediate
process termination is common.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 530353

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.

3 participants