-
-
Notifications
You must be signed in to change notification settings - Fork 162
Undefined filename in require #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: cameron.cooke <cameron.cooke@sentry.io>
|
Cursor Agent can help with this pull request. Just |
commit: |
|
|
||
| sentryLoadAttempted = true; | ||
| try { | ||
| cachedSentry = require('@sentry/node') as SentryModule; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cursor fix
There was a problem hiding this comment.
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.
Learn more about Cursor Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cursor fix
There was a problem hiding this comment.
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.
Learn more about Cursor Agents
| 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. |
There was a problem hiding this comment.
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
Replace synchronous
createRequirewith asynchronous dynamicimport()for Sentry loading.This fixes a
TypeErrorin bundled CommonJS contexts where__filenameis undefined, causingcreateRequireto receive an invalid filename argument. The new approach uses dynamicimport()which is more robust in various module environments and gracefully handles Sentry loading.Note
Shifts Sentry integration in
logger.tsto asynchronous dynamic import with safe, single-attempt loading and caching.createRequire/syncrequire('@sentry/node')withawait import('@sentry/node'), addingsentryLoadAttemptedto prevent repeated loadswithSentryto fire-and-forget async loading and safely invoke callbacks without throwingWritten by Cursor Bugbot for commit a9cad89. This will update automatically on new commits. Configure here.