Skip to content

Commit 196aaf7

Browse files
committed
Prevent crash from logs that have circular references in json
1 parent 4d9b251 commit 196aaf7

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

cli/src/utils/logger.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ const analyticsDispatcher = createAnalyticsDispatcher({
3838
bufferWhenNoUser: true,
3939
})
4040

41+
/**
42+
* Safely stringify an object, handling circular references.
43+
* Replaces circular references with '[Circular]' placeholder.
44+
*/
45+
function safeStringify(obj: unknown): string {
46+
const seen = new WeakSet()
47+
return JSON.stringify(obj, (_key, value) => {
48+
if (typeof value === 'object' && value !== null) {
49+
if (seen.has(value)) {
50+
return '[Circular]'
51+
}
52+
seen.add(value)
53+
}
54+
return value
55+
})
56+
}
57+
4158
function isEmptyObject(value: any): boolean {
4259
return (
4360
value != null &&
@@ -163,7 +180,7 @@ function sendAnalyticsAndLog(
163180
// In dev mode, use appendFileSync for real-time logging (Bun has issues with pino sync)
164181
// In prod mode, use pino for better performance
165182
if (IS_DEV && logPath) {
166-
const logEntry = JSON.stringify({
183+
const logEntry = safeStringify({
167184
level: level.toUpperCase(),
168185
timestamp: new Date().toISOString(),
169186
...loggerContext,

0 commit comments

Comments
 (0)