()
| 35 | } |
| 36 | |
| 37 | export function initializeWarningHandler(): void { |
| 38 | // Only set up handler once - check if our handler is already installed |
| 39 | const currentListeners = process.listeners('warning') |
| 40 | if (warningHandler && currentListeners.includes(warningHandler)) { |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | // For external users, remove default Node.js handler to suppress stderr output |
| 45 | // For internal users, only keep default warnings for development builds |
| 46 | // Check development/source mode directly to avoid async call in init. |
| 47 | // This preserves the same logic as getCurrentInstallationType(). |
| 48 | const isDevelopment = isDevelopmentLikeBuild() |
| 49 | if (!isDevelopment) { |
| 50 | process.removeAllListeners('warning') |
| 51 | } |
| 52 | |
| 53 | // Create and store our warning handler |
| 54 | warningHandler = (warning: Error) => { |
| 55 | try { |
| 56 | const warningKey = `${warning.name}: ${warning.message.slice(0, 50)}` |
| 57 | const count = warningCounts.get(warningKey) || 0 |
| 58 | |
| 59 | // Bound the map to prevent unbounded memory growth from unique warning keys. |
| 60 | // Once the cap is reached, new unique keys are not tracked — their |
| 61 | // occurrence_count will always be reported as 1 in analytics. |
| 62 | if ( |
| 63 | warningCounts.has(warningKey) || |
| 64 | warningCounts.size < MAX_WARNING_KEYS |
| 65 | ) { |
| 66 | warningCounts.set(warningKey, count + 1) |
| 67 | } |
| 68 | |
| 69 | const isInternal = isInternalWarning(warning) |
| 70 | |
| 71 | // Always log to Statsig for monitoring |
| 72 | // Include full details for ant users only, since they may contain code or filepaths |
| 73 | logEvent('ncode_node_warning', { |
| 74 | is_internal: isInternal ? 1 : 0, |
| 75 | occurrence_count: count + 1, |
| 76 | classname: |
| 77 | warning.name as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 78 | ...(isInternalBuild() && { |
| 79 | message: |
| 80 | warning.message as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 81 | }), |
| 82 | }) |
| 83 | |
| 84 | // In debug mode, show all warnings with context |
| 85 | if (isEnvTruthy(process.env.CLAUDE_DEBUG)) { |
| 86 | const prefix = isInternal ? '[Internal Warning]' : '[Warning]' |
| 87 | logForDebugging(`${prefix} ${warning.toString()}`, { level: 'warn' }) |
| 88 | } |
| 89 | // Hide all warnings from users - they are only logged to Statsig for monitoring |
| 90 | } catch { |
| 91 | // Fail silently - we don't want the warning handler to cause issues |
| 92 | } |
| 93 | } |
| 94 |
no test coverage detected