From 8dae9656b8ab011fb0fee7649e7c5e8dd701d2a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=80=D0=BE=D0=B4=D0=B8=D0=BD=20=D0=A0=D0=BE?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD?= Date: Wed, 30 Sep 2026 22:08:26 +0300 Subject: [PATCH] fix: use dynamic imports for matrix-bot-sdk to defer loading ES modules hoist all static imports before any code executes, so matrix-bot-sdk was loaded BEFORE ensureNativeBinary() could download the native binary. Using dynamic imports inside runPlugin ensures the binary is downloaded first. --- dist/index.js | 6 ++++-- index.ts | 11 +++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index e625bbe..6af7823 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7,8 +7,6 @@ import os from "os"; import path from "path"; import * as acorn from "acorn"; import * as acornWalk from "acorn-walk"; -import { MatrixBotClient } from "./matrix-client.js"; -import { SessionManager } from "./session-manager.js"; import { loadConfig } from "./config-loader.js"; import { setAppLog, log, logError } from "./logger.js"; // ============================================================================= @@ -64,6 +62,10 @@ function releaseLock() { // Plugin Setup // ============================================================================= async function runPlugin(ctx, options, dataDir) { + // Dynamic imports — loaded AFTER ensureNativeBinary() completes + // (static imports would be hoisted and evaluated before ensureNativeBinary()) + const { MatrixBotClient } = await import("./matrix-client.js"); + const { SessionManager } = await import("./session-manager.js"); ensureDataDir(dataDir); LOCK_FILE = `${dataDir}/lock`; const directory = ctx?.location?.directory || process.cwd(); diff --git a/index.ts b/index.ts index ad9093b..baf51db 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,8 @@ import { ensureNativeBinary } from "./bootstrap-native.js" + +// Type-only import — doesn't execute any code at runtime +import type { MatrixEventContext } from "./matrix-client.js" + // Ensure native binary is available before loading matrix-bot-sdk // (opencode's npm doesn't run postinstall scripts) ensureNativeBinary() @@ -8,8 +12,6 @@ import os from "os" import path from "path" import * as acorn from "acorn" import * as acornWalk from "acorn-walk" -import { MatrixBotClient, type MatrixEventContext } from "./matrix-client.js" -import { SessionManager } from "./session-manager.js" import { loadConfig } from "./config-loader.js" import { setAppLog, log, logError } from "./logger.js" @@ -68,6 +70,11 @@ function releaseLock(): void { // ============================================================================= async function runPlugin(ctx: any, options: any, dataDir: string) { + // Dynamic imports — loaded AFTER ensureNativeBinary() completes + // (static imports would be hoisted and evaluated before ensureNativeBinary()) + const { MatrixBotClient } = await import("./matrix-client.js") + const { SessionManager } = await import("./session-manager.js") + ensureDataDir(dataDir) LOCK_FILE = `${dataDir}/lock`