fix: defer module-level side effects to prevent Bun 1.3.5 + macOS 15 segfault (#301)

- Remove eager SG_CLI_PATH constant; use getSgCliPath() lazily in checkEnvironment()
- Move setInterval to inside createCommentCheckerHooks() with guard flag

These changes eliminate module-level side effects that could trigger segfaults
during plugin initialization on Bun 1.3.5 + macOS 15 due to createRequire()
being called during module evaluation.

Fixes #292

Co-authored-by: sisyphus-dev-ai <sisyphus-dev-ai@users.noreply.github.com>
This commit is contained in:
Sisyphus
2025-12-28 15:55:47 +09:00
committed by GitHub
parent 195e8dcb17
commit c11cb2e3f1
2 changed files with 11 additions and 10 deletions

View File

@@ -21,6 +21,7 @@ const pendingCalls = new Map<string, PendingCall>()
const PENDING_CALL_TTL = 60_000 const PENDING_CALL_TTL = 60_000
let cliPathPromise: Promise<string | null> | null = null let cliPathPromise: Promise<string | null> | null = null
let cleanupIntervalStarted = false
function cleanupOldPendingCalls(): void { function cleanupOldPendingCalls(): void {
const now = Date.now() const now = Date.now()
@@ -31,10 +32,13 @@ function cleanupOldPendingCalls(): void {
} }
} }
setInterval(cleanupOldPendingCalls, 10_000)
export function createCommentCheckerHooks(config?: CommentCheckerConfig) { export function createCommentCheckerHooks(config?: CommentCheckerConfig) {
debugLog("createCommentCheckerHooks called", { config }) debugLog("createCommentCheckerHooks called", { config })
if (!cleanupIntervalStarted) {
cleanupIntervalStarted = true
setInterval(cleanupOldPendingCalls, 10_000)
}
// Start background CLI initialization (may trigger lazy download) // Start background CLI initialization (may trigger lazy download)
startBackgroundInit() startBackgroundInit()

View File

@@ -100,8 +100,6 @@ export function setSgCliPath(path: string): void {
resolvedCliPath = path resolvedCliPath = path
} }
export const SG_CLI_PATH = getSgCliPath()
// CLI supported languages (25 total) // CLI supported languages (25 total)
export const CLI_LANGUAGES = [ export const CLI_LANGUAGES = [
"bash", "bash",
@@ -184,21 +182,20 @@ export interface EnvironmentCheckResult {
* Call this at startup to provide early feedback about missing dependencies. * Call this at startup to provide early feedback about missing dependencies.
*/ */
export function checkEnvironment(): EnvironmentCheckResult { export function checkEnvironment(): EnvironmentCheckResult {
const cliPath = getSgCliPath()
const result: EnvironmentCheckResult = { const result: EnvironmentCheckResult = {
cli: { cli: {
available: false, available: false,
path: SG_CLI_PATH, path: cliPath,
}, },
napi: { napi: {
available: false, available: false,
}, },
} }
// Check CLI availability if (existsSync(cliPath)) {
if (existsSync(SG_CLI_PATH)) {
result.cli.available = true result.cli.available = true
} else if (SG_CLI_PATH === "sg") { } else if (cliPath === "sg") {
// Fallback path - try which/where to find in PATH
try { try {
const { spawnSync } = require("child_process") const { spawnSync } = require("child_process")
const whichResult = spawnSync(process.platform === "win32" ? "where" : "which", ["sg"], { const whichResult = spawnSync(process.platform === "win32" ? "where" : "which", ["sg"], {
@@ -213,7 +210,7 @@ export function checkEnvironment(): EnvironmentCheckResult {
result.cli.error = "Failed to check sg availability" result.cli.error = "Failed to check sg availability"
} }
} else { } else {
result.cli.error = `Binary not found: ${SG_CLI_PATH}` result.cli.error = `Binary not found: ${cliPath}`
} }
// Check NAPI availability // Check NAPI availability