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
let cliPathPromise: Promise<string | null> | null = null
let cleanupIntervalStarted = false
function cleanupOldPendingCalls(): void {
const now = Date.now()
@@ -31,11 +32,14 @@ function cleanupOldPendingCalls(): void {
}
}
setInterval(cleanupOldPendingCalls, 10_000)
export function createCommentCheckerHooks(config?: CommentCheckerConfig) {
debugLog("createCommentCheckerHooks called", { config })
if (!cleanupIntervalStarted) {
cleanupIntervalStarted = true
setInterval(cleanupOldPendingCalls, 10_000)
}
// Start background CLI initialization (may trigger lazy download)
startBackgroundInit()
cliPathPromise = getCommentCheckerPath()

View File

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