From 26fb1cd47ec5472d36abb37492af53b295258da7 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 15:15:49 -0600 Subject: [PATCH] fix: handle symlinked entry points (#4325) --- bin/codedb-bridge.mjs | 13 +++- bin/codedb-bridge.test.mjs | 47 +++++++++++++- .../cloudflare-quota-export.mjs | 13 +++- .../cloudflare-quota-export.test.mjs | 61 ++++++++++++++++++- pm-stack/opencode-claimant/install.mjs | 13 +++- .../supabase-export/supabase-quota-export.mjs | 13 +++- 6 files changed, 147 insertions(+), 13 deletions(-) diff --git a/bin/codedb-bridge.mjs b/bin/codedb-bridge.mjs index 6efcc01ac..1336dcc6a 100644 --- a/bin/codedb-bridge.mjs +++ b/bin/codedb-bridge.mjs @@ -313,7 +313,16 @@ const main = async () => { }); }; -import { pathToFileURL } from 'node:url'; -if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { +import fs from 'node:fs'; +import { fileURLToPath } from 'node:url'; +const isMain = (() => { + try { + if (!process.argv[1]) return false; + return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url)); + } catch { + return false; + } +})(); +if (isMain) { await main(); } diff --git a/bin/codedb-bridge.test.mjs b/bin/codedb-bridge.test.mjs index cbe381c5b..93d8d5254 100644 --- a/bin/codedb-bridge.test.mjs +++ b/bin/codedb-bridge.test.mjs @@ -1,11 +1,13 @@ // Tests for the codedb MCP bridge (#4120). // Run: node --test bin/codedb-bridge.test.mjs import assert from 'node:assert/strict'; -import { spawn } from 'node:child_process'; +import { spawn, spawnSync } from 'node:child_process'; +import fs from 'node:fs'; import http from 'node:http'; +import os from 'node:os'; import path from 'node:path'; import test from 'node:test'; -import { fileURLToPath } from 'node:url'; +import { fileURLToPath, pathToFileURL } from 'node:url'; import { CODEDB_TOOLS, createHandler, parseDaemonUrl, routeForTool } from './codedb-bridge.mjs'; const binDir = path.dirname(fileURLToPath(import.meta.url)); @@ -173,6 +175,47 @@ test('closing stdin mid-request still delivers the complete final response (PR # } }); +test('CLI: runs main via file and directory symlinks (#4325)', () => { + const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'codedb-bridge-entrypoint-')); + const bridge = path.join(binDir, 'codedb-bridge.mjs'); + const fileLink = path.join(temp, 'codedb-bridge-link.mjs'); + const dirLink = path.join(temp, 'bin-link'); + fs.symlinkSync(bridge, fileLink); + fs.symlinkSync(binDir, dirLink, 'dir'); + + try { + const input = `${JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'ping' })}\n`; + for (const [label, entry] of [ + ['direct', bridge], + ['file symlink', fileLink], + ['directory symlink', path.join(dirLink, 'codedb-bridge.mjs')], + ]) { + const result = spawnSync(process.execPath, [entry], { + input, + encoding: 'utf8', + timeout: 5000, + }); + assert.equal(result.status, 0, `${label}: ${result.stderr}`); + assert.notEqual(result.stdout, '', `${label}: main did not write a response`); + assert.deepEqual(JSON.parse(result.stdout), { + jsonrpc: '2.0', id: 1, result: {}, + }, `${label} must start the stdio bridge`); + } + } finally { + fs.rmSync(temp, { recursive: true, force: true }); + } +}); + +test('importing codedb-bridge.mjs as a library does not start main (#4325)', () => { + const bridge = path.join(binDir, 'codedb-bridge.mjs'); + const result = spawnSync(process.execPath, [ + '--input-type=module', '-e', `await import(${JSON.stringify(pathToFileURL(bridge).href)});`, + ], { encoding: 'utf8', timeout: 5000 }); + assert.equal(result.status, 0, result.stderr); + assert.equal(result.stdout, ''); + assert.equal(result.stderr, ''); +}); + test('daemon errors and an unreachable daemon surface as MCP errors naming the cause', async () => { const handle404 = createHandler({ fetchFn: stubFetch({}), daemonUrl: 'http://127.0.0.1:7719' }); const missing = await call(handle404, 'codedb_outline', { path: 'nope.rs' }); diff --git a/pm-stack/cloudflare-export/cloudflare-quota-export.mjs b/pm-stack/cloudflare-export/cloudflare-quota-export.mjs index 1b6cc3732..94b249e5b 100755 --- a/pm-stack/cloudflare-export/cloudflare-quota-export.mjs +++ b/pm-stack/cloudflare-export/cloudflare-quota-export.mjs @@ -42,10 +42,11 @@ * unparseable an endpoint answered an unrecognised shape. Exit 6. * fetch-failed network/other error. Exit 7. */ +import fs from "node:fs"; import { appendFile, mkdir, rename, writeFile } from "node:fs/promises"; import { homedir } from "node:os"; import { join } from "node:path"; -import { pathToFileURL } from "node:url"; +import { fileURLToPath } from "node:url"; export const SOURCE = "file-export/cloudflare"; /** @@ -586,6 +587,14 @@ async function main() { } } -if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { +const isMain = (() => { + try { + if (!process.argv[1]) return false; + return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url)); + } catch { + return false; + } +})(); +if (isMain) { await main(); } diff --git a/pm-stack/cloudflare-export/cloudflare-quota-export.test.mjs b/pm-stack/cloudflare-export/cloudflare-quota-export.test.mjs index 5c55b3f08..1455f8798 100755 --- a/pm-stack/cloudflare-export/cloudflare-quota-export.test.mjs +++ b/pm-stack/cloudflare-export/cloudflare-quota-export.test.mjs @@ -1,7 +1,12 @@ -// Tests for the pure parsing core of cloudflare-quota-export.mjs. -// Run: node --test pm-stack/cloudflare-export/ +// Tests for cloudflare-quota-export parsing and CLI entry-point behavior. +// Run: node --test pm-stack/cloudflare-export/cloudflare-quota-export.test.mjs import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; import { test } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; import { aiNeuronsDayAlerts, aiNeuronsDayAlertThreshold, @@ -50,6 +55,7 @@ const ACCOUNT = { }; const NOW = new Date("2026-10-29T10:00:00Z"); const START = cycleStartUtc(NOW); +const EXPORTER_PATH = fileURLToPath(new URL("./cloudflare-quota-export.mjs", import.meta.url)); test("API_REQUEST_TIMEOUT_MS: bounded below the 120s service deadline", () => { // cloudflare-quota-export.service has TimeoutStartSec=120; a stalled @@ -59,6 +65,57 @@ test("API_REQUEST_TIMEOUT_MS: bounded below the 120s service deadline", () => { assert.ok(API_REQUEST_TIMEOUT_MS < 120_000); }); +test("CLI: runs main via file and directory symlinks (#4325)", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "cloudflare-export-entrypoint-")); + const links = path.join(home, "links"); + fs.mkdirSync(links); + const fileLink = path.join(links, "export.mjs"); + const dirLink = path.join(links, "export-dir"); + fs.symlinkSync(EXPORTER_PATH, fileLink); + fs.symlinkSync(path.dirname(EXPORTER_PATH), dirLink, "dir"); + const env = { ...process.env, HOME: home, CLOUDFLARE_API_TOKEN: "" }; + + try { + for (const [name, entry] of [ + ["direct", EXPORTER_PATH], + ["file-symlink", fileLink], + ["directory-symlink", path.join(dirLink, path.basename(EXPORTER_PATH))], + ]) { + const outDir = path.join(home, name); + const result = spawnSync(process.execPath, [entry, "--out-dir", outDir], { + env, + encoding: "utf8", + timeout: 10000, + }); + assert.equal(result.status, 2, `${name}: ${result.stderr}`); + const doc = JSON.parse(fs.readFileSync(path.join(outDir, "latest.json"), "utf8")); + assert.equal(doc.service, "cloudflare"); + assert.equal(doc.status, "no-token"); + } + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("importing cloudflare-quota-export.mjs as a library does not run main (#4325)", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "cloudflare-export-import-")); + try { + const result = spawnSync(process.execPath, [ + "--input-type=module", "-e", `await import(${JSON.stringify(pathToFileURL(EXPORTER_PATH).href)});`, + ], { + env: { ...process.env, HOME: home, CLOUDFLARE_API_TOKEN: "" }, + encoding: "utf8", + timeout: 5000, + }); + assert.equal(result.status, 0, result.stderr); + assert.equal(result.stdout, ""); + assert.equal(result.stderr, ""); + assert.equal(fs.existsSync(path.join(home, "atlas", "cloudflare-export")), false); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + test("cycleStartUtc: first of the month, UTC", () => { assert.equal(START.toISOString(), "2026-10-01T00:00:00.000Z"); }); diff --git a/pm-stack/opencode-claimant/install.mjs b/pm-stack/opencode-claimant/install.mjs index 491fdc0c5..86cc1626a 100644 --- a/pm-stack/opencode-claimant/install.mjs +++ b/pm-stack/opencode-claimant/install.mjs @@ -12,10 +12,11 @@ // Usage: // bun install.mjs [--store ] [--dry-run] [--enable] // node install.mjs ... (same; no dependencies) +import fs from "node:fs"; import { readFile, writeFile, rename, copyFile } from "node:fs/promises"; import { homedir } from "node:os"; import { dirname, join } from "node:path"; -import { fileURLToPath, pathToFileURL } from "node:url"; +import { fileURLToPath } from "node:url"; export const TASK_ID = "f1b4a33b-14d5-4a69-9ad6-d221e42e3ac9"; export const TASK_NAME = "Opencode lane claimant/executor - #3026"; @@ -128,8 +129,14 @@ async function main(argv) { return 0; } -const isMain = process.argv[1] - && import.meta.url === pathToFileURL(process.argv[1]).href; +const isMain = (() => { + try { + if (!process.argv[1]) return false; + return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url)); + } catch { + return false; + } +})(); if (isMain) { main(process.argv.slice(2)).then( (code) => process.exit(code), diff --git a/pm-stack/supabase-export/supabase-quota-export.mjs b/pm-stack/supabase-export/supabase-quota-export.mjs index 499554506..cd6394f59 100755 --- a/pm-stack/supabase-export/supabase-quota-export.mjs +++ b/pm-stack/supabase-export/supabase-quota-export.mjs @@ -47,10 +47,11 @@ * egres bytes no endpoint exists (request counts only) — documented gap */ import { execFile } from "node:child_process"; +import fs from "node:fs"; import { appendFile, mkdir, rename, writeFile } from "node:fs/promises"; import { homedir } from "node:os"; import { join } from "node:path"; -import { pathToFileURL } from "node:url"; +import { fileURLToPath } from "node:url"; export const SOURCE = "file-export/supabase"; export const DEFAULT_PROJECT_REF = "nltgxorqwxhgfzseiirt"; // muse-dev (#1050) @@ -396,6 +397,14 @@ async function main() { } } -if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { +const isMain = (() => { + try { + if (!process.argv[1]) return false; + return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url)); + } catch { + return false; + } +})(); +if (isMain) { await main(); }