Skip to content

Commit 85dd351

Browse files
mdesmetclaude
andcommitted
fix: resolve altimate-core platform packages via createRequire
M3 from PR #820 review. The old locatePlatformPackageDir walked the filesystem hand-checking three different prefix shapes for bun's flat `.bun/<scope>+<name>@<version>/node_modules/<scope>/<name>` layout and interpolated `altimateCoreVersion` (read from packages/opencode/package.json dependencies) into the path literally. Both fragile: - pkg.dependencies values can carry semver prefixes (`^0.3.1`, `~0.3.1`, `>=0.3.1`) — the literal interpolation would miss the on-disk dir, which is keyed by the exact resolved version. - bun could change its flat-layout dir name format at any time; we'd silently start failing to find the prebuild. Replace with `createRequire(path.join(loaderDir, "index.js"))` then `req.resolve(`${pkgName}/package.json`)`. Node's resolver walks parent node_modules from the require base, which (in this project's bun-hoisted layout) finds the prebuild via the loader's `.bun/@AltimateAI+altimate-core@.../node_modules/@altimateai/altimate-core-<platform>` sibling entries without us having to know what the surrounding store looks like. Verified all 5 platform packages resolve to the same `.bun` paths the old function returned: darwin-arm64 -> .../node_modules/.bun/@AltimateAI[email protected]/node_modules/@altimateai/altimate-core-darwin-arm64 darwin-x64 -> .../node_modules/.bun/@AltimateAI[email protected]/node_modules/@altimateai/altimate-core-darwin-x64 linux-arm64 -> .../node_modules/.bun/@AltimateAI[email protected]/node_modules/@altimateai/altimate-core-linux-arm64-gnu linux-x64 -> .../node_modules/.bun/@AltimateAI[email protected]/node_modules/@altimateai/altimate-core-linux-x64-gnu win32-x64 -> .../node_modules/.bun/@AltimateAI[email protected]/node_modules/@altimateai/altimate-core-win32-x64-msvc Build still succeeds, binary still starts hermetically: $ rm -rf packages/opencode/dist && \ bun run packages/opencode/script/build.ts --single --skip-install building @altimateai/altimate-code-darwin-arm64 $ cd /tmp && env -u NODE_PATH .../bin/altimate --version 0.0.0-fix/curl-install-bundle-altimate-core-202605181341 $ bun test test/install/ test/branding/ 401 pass / 0 fail altimateCoreVersion is no longer used and is dropped. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 2564abf commit 85dd351

1 file changed

Lines changed: 14 additions & 26 deletions

File tree

‎packages/opencode/script/build.ts‎

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { $ } from "bun"
44
import fs from "fs"
55
import path from "path"
66
import { fileURLToPath } from "url"
7+
import { createRequire } from "node:module"
78
import solidPlugin from "@opentui/solid/bun-plugin"
89

910
const __filename = fileURLToPath(import.meta.url)
@@ -246,7 +247,13 @@ function altimateCorePlatformFor(item: { os: string; arch: "arm64" | "x64"; abi?
246247
// node_modules/.bun) — we copy from this for each per-target staging dir.
247248
const altimateCoreLoaderPkgJson = fileURLToPath(import.meta.resolve("@altimateai/altimate-core/package.json"))
248249
const altimateCoreLoaderDir = fs.realpathSync(path.dirname(altimateCoreLoaderPkgJson))
249-
const altimateCoreVersion = pkg.dependencies["@altimateai/altimate-core"]
250+
251+
// A `require` rooted at the loader's index.js so we can resolve sibling
252+
// `@altimateai/altimate-core-<platform>` packages without hand-walking bun's
253+
// `.bun/` flat layout. Node's resolution walks parent node_modules from the
254+
// require base, which (in bun's hoisted layout used by this project) reaches
255+
// the top-level `node_modules/@altimateai/altimate-core-<platform>` symlinks.
256+
const altimateCoreLoaderRequire = createRequire(path.join(altimateCoreLoaderDir, "index.js"))
250257

251258
// Extract the `_requiredExports` literal from the upstream NAPI-RS loader so
252259
// the generated single-platform shim can keep the same correctness check
@@ -265,32 +272,13 @@ if (!requiredExportsMatch) {
265272
const altimateCoreRequiredExportsLiteral = requiredExportsMatch[1]
266273

267274
// Locate the on-disk dir for an @altimateai/altimate-core-<platform> NAPI
268-
// prebuild. bun installs each optional platform package in its own `.bun/`
269-
// store entry, not next to the loader, so import.meta.resolve doesn't find it
270-
// from the workspace. Walk up from the loader dir looking for the surrounding
271-
// `.bun` store, then look up the platform package by its conventional name.
275+
// prebuild. Use createRequire rooted at the loader's index.js — Node's
276+
// require.resolve walks parent node_modules from the require base, which
277+
// reaches both bun's hoisted top-level @altimateai/altimate-core-<platform>
278+
// symlinks and any nested layout.
272279
function locatePlatformPackageDir(pkgName: string): string {
273-
// bun's flat layout: `@scope+name@version` — only the `/` becomes `+`, the
274-
// leading `@` of the scope stays.
275-
const flatName = pkgName.replace("/", "+")
276-
let current = altimateCoreLoaderDir
277-
for (;;) {
278-
// Match every layout we walk through:
279-
// • current === `.bun` → check current
280-
// • current === node_modules → check current/.bun
281-
// • current === workspace root above node_modules → check current/node_modules/.bun
282-
for (const prefix of [current, path.join(current, ".bun"), path.join(current, "node_modules", ".bun")]) {
283-
const candidate = path.join(prefix, `${flatName}@${altimateCoreVersion}`, "node_modules", pkgName)
284-
if (fs.existsSync(candidate)) return fs.realpathSync(candidate)
285-
}
286-
const parent = path.dirname(current)
287-
if (parent === current) break
288-
current = parent
289-
}
290-
throw new Error(
291-
`Could not locate ${pkgName}@${altimateCoreVersion} on disk under any .bun store; ` +
292-
`ensure 'bun install --os=* --cpu=*' has fetched it.`,
293-
)
280+
const pkgJsonPath = altimateCoreLoaderRequire.resolve(`${pkgName}/package.json`)
281+
return fs.realpathSync(path.dirname(pkgJsonPath))
294282
}
295283
for (const item of targets) {
296284
const name = [

0 commit comments

Comments
 (0)