From c9feeae58c6ce5980b19a6f00d1d22105d6a2785 Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Mon, 30 Nov 2026 22:52:04 +0800 Subject: [PATCH 1/3] fix: Bedrock credential chain in the compiled binary resolves the AWS SDK's node entries The build's "browser" condition pulled `@smithy/core/config`'s browser stubs (`parseKnownFiles` is a Symbol). A build plugin resolves @aws-sdk, @smithy and @aws-crypto as Node does; the build refuses a binary with a stub. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQsV67ZmxvZP7buH4b5 --- packages/opencode/script/aws-node-resolve.ts | 35 +++++++++ packages/opencode/script/build.ts | 20 ++++- .../fixture/aws-credential-chain-build.ts | 21 ++++++ .../test/fixture/aws-credential-chain.ts | 8 ++ .../test/provider/aws-node-resolve.test.ts | 74 +++++++++++++++++++ 5 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 packages/opencode/script/aws-node-resolve.ts create mode 100644 packages/opencode/test/fixture/aws-credential-chain-build.ts create mode 100644 packages/opencode/test/fixture/aws-credential-chain.ts create mode 100644 packages/opencode/test/provider/aws-node-resolve.test.ts diff --git a/packages/opencode/script/aws-node-resolve.ts b/packages/opencode/script/aws-node-resolve.ts new file mode 100644 index 0000000000..0505349652 --- /dev/null +++ b/packages/opencode/script/aws-node-resolve.ts @@ -0,0 +1,35 @@ +// altimate_change - new file +// +// The release build bundles with the "browser" export condition, which the Solid TUI needs. Under it, the AWS SDK's +// and Smithy's packages resolve to their browser entries, where every node-only export is a stub: +// `@smithy/core/config` exports `parseKnownFiles = Symbol.for("node-only")`. `fromNodeProviderChain` reads the AWS +// config and credentials files through it, so every Amazon Bedrock request in a compiled binary failed with +// "parseKnownFiles is not a function", while source runs, which resolve the node entries, worked. +// +// `awsNodeResolvePlugin` resolves those packages the way Node does and leaves the browser condition in place for +// everything else. `nodeOnlyStubCount` is the build's check that no such stub reached a binary. +import path from "path" +import type { BunPlugin } from "bun" + +/** Packages that ship browser entries with node-only stubs. */ +export const NODE_RESOLVED_PACKAGES = /^@(aws-sdk|smithy|aws-crypto)\// + +export const awsNodeResolvePlugin: BunPlugin = { + name: "aws-node-resolve", + setup(build) { + build.onResolve({ filter: NODE_RESOLVED_PACKAGES }, (args) => ({ + // The build script runs under Bun's own conditions (bun, node, import, require), never "browser". + path: Bun.resolveSync(args.path, path.dirname(args.importer)), + })) + }, +} + +const NODE_ONLY_STUB = Buffer.from('Symbol.for("node-only")') + +/** How many node-only stubs a bundle or compiled binary carries. Any is a node-only path that can't work. */ +export function nodeOnlyStubCount(bytes: Uint8Array): number { + const buf = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength) + let count = 0 + for (let at = buf.indexOf(NODE_ONLY_STUB); at !== -1; at = buf.indexOf(NODE_ONLY_STUB, at + 1)) count++ + return count +} diff --git a/packages/opencode/script/build.ts b/packages/opencode/script/build.ts index e591f77314..614f1fd713 100755 --- a/packages/opencode/script/build.ts +++ b/packages/opencode/script/build.ts @@ -22,6 +22,9 @@ import { Script } from "@opencode-ai/script" import pkg from "../package.json" import { walkInputs } from "./stamp-inputs" import { assertUsableCatalog, catalogDiagnosticOrigin, formatCatalogSummary } from "./models-catalog" +// altimate_change start — resolve the AWS SDK's node entries under the browser condition (aws-node-resolve.ts) +import { awsNodeResolvePlugin, nodeOnlyStubCount } from "./aws-node-resolve" +// altimate_change end import { FreeTierUrl } from "../src/altimate/free/url" // Python engine has been eliminated — all methods run natively in TypeScript. @@ -624,7 +627,9 @@ for (const item of targets) { await Bun.build({ conditions: ["browser"], tsconfig: "./tsconfig.json", - plugins: [solidPlugin, altimateCoreResolverPlugin], + // altimate_change start — awsNodeResolvePlugin: the AWS SDK's node entries, not its browser stubs + plugins: [solidPlugin, altimateCoreResolverPlugin, awsNodeResolvePlugin], + // altimate_change end sourcemap: "external", // IMPORTANT: Without code splitting, Bun inlines dynamic import() targets // into the main chunk. Any external require() in those targets will fail @@ -664,6 +669,19 @@ for (const item of targets) { // Staging dir is no longer needed once Bun has embedded the shim + .node. await $`rm -rf dist/${name}/.altimate-core-staged` + // altimate_change start — refuse a binary that carries a node-only stub (aws-node-resolve.ts) + // A browser entry's stub in place of a node export fails only at run time, on the path that calls it (the + // Bedrock credential chain did). Checked on the compiled file, since that is what ships. + const builtBinary = `dist/${name}/bin/altimate${item.os === "win32" ? ".exe" : ""}` + const stubs = nodeOnlyStubCount(fs.readFileSync(builtBinary)) + if (stubs > 0) { + throw new Error( + `${builtBinary} contains ${stubs} Symbol.for("node-only") stub(s): a package resolved to its browser entry. ` + + "Add it to NODE_RESOLVED_PACKAGES in script/aws-node-resolve.ts.", + ) + } + // altimate_change end + // Create backward-compatible altimate-code alias inside the platform package. // The npm wrapper (`packages/opencode/bin/altimate`) looks for `bin/altimate-code` // (or .exe) when locating the platform binary, so this must exist for the diff --git a/packages/opencode/test/fixture/aws-credential-chain-build.ts b/packages/opencode/test/fixture/aws-credential-chain-build.ts new file mode 100644 index 0000000000..04de68af01 --- /dev/null +++ b/packages/opencode/test/fixture/aws-credential-chain-build.ts @@ -0,0 +1,21 @@ +// altimate_change - new file +// +// Bundles aws-credential-chain.ts with the release build's resolution settings (the "browser" condition, and +// awsNodeResolvePlugin unless the mode is "plain") and prints the bundle's path. Run in its own process by +// test/provider/aws-node-resolve.test.ts: a second Bun.build inside `bun test` fails reading files. +import path from "path" +import { awsNodeResolvePlugin } from "../../script/aws-node-resolve" + +const [outdir, mode] = process.argv.slice(2) +const out = await Bun.build({ + entrypoints: [path.join(import.meta.dir, "aws-credential-chain.ts")], + outdir, + target: "bun", + conditions: ["browser"], + plugins: mode === "plain" ? [] : [awsNodeResolvePlugin], +}) +if (!out.success) { + for (const log of out.logs) console.error(log) + process.exit(1) +} +console.log(out.outputs[0].path) diff --git a/packages/opencode/test/fixture/aws-credential-chain.ts b/packages/opencode/test/fixture/aws-credential-chain.ts new file mode 100644 index 0000000000..a6974c84a4 --- /dev/null +++ b/packages/opencode/test/fixture/aws-credential-chain.ts @@ -0,0 +1,8 @@ +// altimate_change - new file +// +// Bundled by test/provider/aws-node-resolve.test.ts with the release build's resolution settings: resolves the +// AWS credential chain for AWS_PROFILE, as the Amazon Bedrock provider does, and prints the access key id. +import { fromNodeProviderChain } from "@aws-sdk/credential-providers" + +const credentials = await fromNodeProviderChain({ profile: process.env.AWS_PROFILE })() +console.log(JSON.stringify({ accessKeyId: credentials.accessKeyId })) diff --git a/packages/opencode/test/provider/aws-node-resolve.test.ts b/packages/opencode/test/provider/aws-node-resolve.test.ts new file mode 100644 index 0000000000..467e4ddd03 --- /dev/null +++ b/packages/opencode/test/provider/aws-node-resolve.test.ts @@ -0,0 +1,74 @@ +// altimate_change - new file +// +// The release build bundles with the "browser" export condition, where the AWS SDK's node-only exports are stubs +// (`parseKnownFiles` is `Symbol.for("node-only")`), and every Bedrock request in a compiled binary failed with +// "parseKnownFiles is not a function". These tests bundle the credential chain the way the build does and run it +// offline against a fake profile in scratch files: no network, and the real ~/.aws is never read. +import { afterAll, describe, expect, test } from "bun:test" +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" +import os from "os" +import path from "path" +import { nodeOnlyStubCount } from "../../script/aws-node-resolve" + +const dir = mkdtempSync(path.join(os.tmpdir(), "aws-node-resolve-")) +afterAll(() => rmSync(dir, { recursive: true, force: true })) + +const builder = path.join(import.meta.dir, "..", "fixture", "aws-credential-chain-build.ts") + +/** Bundles the fixture in its own process, with or without awsNodeResolvePlugin; returns the bundle's path. */ +function bundle(mode: "plugin" | "plain"): string { + const run = Bun.spawnSync([process.execPath, builder, path.join(dir, mode), mode]) + expect(run.stderr.toString()).toBe("") + expect(run.exitCode).toBe(0) + return run.stdout.toString().trim() +} + +/** Runs a bundled credential chain with a fake profile; only the scratch AWS files are visible to it. */ +function resolveFakeProfile(file: string) { + const credentials = path.join(dir, "credentials") + const config = path.join(dir, "config") + writeFileSync(credentials, "[fake]\naws_access_key_id = fake-access-key-id\naws_secret_access_key = fake-secret\n") + writeFileSync(config, "[profile fake]\nregion = us-east-1\n") + const run = Bun.spawnSync([process.execPath, file], { + env: { + PATH: process.env.PATH ?? "", + HOME: dir, + AWS_PROFILE: "fake", + AWS_CONFIG_FILE: config, + AWS_SHARED_CREDENTIALS_FILE: credentials, + AWS_EC2_METADATA_DISABLED: "true", + }, + }) + return { exitCode: run.exitCode, stdout: run.stdout.toString(), stderr: run.stderr.toString() } +} + +describe("the AWS credential chain under the release build's browser condition", () => { + test("with awsNodeResolvePlugin, the bundle has no node-only stub and reads the profile", () => { + const file = bundle("plugin") + expect(nodeOnlyStubCount(readFileSync(file))).toBe(0) + const run = resolveFakeProfile(file) + expect(run.stderr).not.toContain("parseKnownFiles") + expect(run.exitCode).toBe(0) + expect(JSON.parse(run.stdout.trim())).toEqual({ accessKeyId: "fake-access-key-id" }) + }, 60_000) + + test("without it, the same bundle gets the browser stubs and the chain fails as it did in the binary", () => { + const file = bundle("plain") + expect(nodeOnlyStubCount(readFileSync(file))).toBeGreaterThan(0) + const run = resolveFakeProfile(file) + expect(run.exitCode).not.toBe(0) + expect(run.stderr).toContain("parseKnownFiles is not a function") + }, 60_000) + + test("the release build registers the plugin and refuses a binary with a node-only stub", () => { + const build = readFileSync(path.join(import.meta.dir, "..", "..", "script", "build.ts"), "utf8") + expect(build).toMatch(/plugins: \[[^\]]*\bawsNodeResolvePlugin\b[^\]]*\]/) + expect(build).toContain("nodeOnlyStubCount(fs.readFileSync(builtBinary))") + }) + + test("nodeOnlyStubCount counts every stub", () => { + const stub = 'Symbol.for("node-only")' + expect(nodeOnlyStubCount(Buffer.from(`a ${stub} b ${stub}${stub}`))).toBe(3) + expect(nodeOnlyStubCount(Buffer.from("leader-node-only function"))).toBe(0) + }) +}) From a42594ce6021991fe56f44210bdee169c7b79946 Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 1 Dec 2026 08:34:26 +0800 Subject: [PATCH 2/3] fix: check bundled modules for node-only stubs, not the binary's bytes `build.ts` embeds `CHANGELOG.md`, builtin skills and migrations verbatim, so a text quoting `Symbol.for("node-only")` would have failed the release build. The guard now reads the modules listed in the build's metafile and names the offending ones. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQsV67ZmxvZP7buH4b5 --- packages/opencode/script/aws-node-resolve.ts | 18 ++++++++++-- packages/opencode/script/build.ts | 25 ++++++++-------- .../fixture/aws-credential-chain-build.ts | 11 ++++--- .../test/fixture/aws-credential-chain.ts | 4 +++ .../test/provider/aws-node-resolve.test.ts | 29 +++++++++++-------- 5 files changed, 57 insertions(+), 30 deletions(-) diff --git a/packages/opencode/script/aws-node-resolve.ts b/packages/opencode/script/aws-node-resolve.ts index 0505349652..01ad853047 100644 --- a/packages/opencode/script/aws-node-resolve.ts +++ b/packages/opencode/script/aws-node-resolve.ts @@ -7,7 +7,8 @@ // "parseKnownFiles is not a function", while source runs, which resolve the node entries, worked. // // `awsNodeResolvePlugin` resolves those packages the way Node does and leaves the browser condition in place for -// everything else. `nodeOnlyStubCount` is the build's check that no such stub reached a binary. +// everything else. `nodeOnlyStubInputs` is the build's check that no bundled module carries such a stub. +import fs from "fs" import path from "path" import type { BunPlugin } from "bun" @@ -26,10 +27,23 @@ export const awsNodeResolvePlugin: BunPlugin = { const NODE_ONLY_STUB = Buffer.from('Symbol.for("node-only")') -/** How many node-only stubs a bundle or compiled binary carries. Any is a node-only path that can't work. */ +/** How many node-only stubs a file carries. Any is a node-only path that can't work. */ export function nodeOnlyStubCount(bytes: Uint8Array): number { const buf = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength) let count = 0 for (let at = buf.indexOf(NODE_ONLY_STUB); at !== -1; at = buf.indexOf(NODE_ONLY_STUB, at + 1)) count++ return count } + +/** + * The bundled modules, from a Bun.build metafile, whose source carries a node-only stub. The check reads the + * modules rather than the compiled binary: CHANGELOG.md, builtin skills and migrations are embedded verbatim and + * may quote the marker. + */ +export function nodeOnlyStubInputs(metafile: { inputs: Record } | undefined): string[] { + if (!metafile) throw new Error("Bun.build returned no metafile, so the node-only stub check cannot run") + return Object.keys(metafile.inputs).filter((input) => { + const file = path.resolve(input) + return fs.existsSync(file) && nodeOnlyStubCount(fs.readFileSync(file)) > 0 + }) +} diff --git a/packages/opencode/script/build.ts b/packages/opencode/script/build.ts index 614f1fd713..4f6ff755eb 100755 --- a/packages/opencode/script/build.ts +++ b/packages/opencode/script/build.ts @@ -23,7 +23,7 @@ import pkg from "../package.json" import { walkInputs } from "./stamp-inputs" import { assertUsableCatalog, catalogDiagnosticOrigin, formatCatalogSummary } from "./models-catalog" // altimate_change start — resolve the AWS SDK's node entries under the browser condition (aws-node-resolve.ts) -import { awsNodeResolvePlugin, nodeOnlyStubCount } from "./aws-node-resolve" +import { awsNodeResolvePlugin, nodeOnlyStubInputs } from "./aws-node-resolve" // altimate_change end import { FreeTierUrl } from "../src/altimate/free/url" @@ -624,11 +624,13 @@ for (const item of targets) { }, } - await Bun.build({ + // altimate_change — keep the result: its metafile feeds the node-only stub check below + const bundled = await Bun.build({ conditions: ["browser"], tsconfig: "./tsconfig.json", // altimate_change start — awsNodeResolvePlugin: the AWS SDK's node entries, not its browser stubs plugins: [solidPlugin, altimateCoreResolverPlugin, awsNodeResolvePlugin], + metafile: true, // altimate_change end sourcemap: "external", // IMPORTANT: Without code splitting, Bun inlines dynamic import() targets @@ -666,22 +668,21 @@ for (const item of targets) { }, }) - // Staging dir is no longer needed once Bun has embedded the shim + .node. - await $`rm -rf dist/${name}/.altimate-core-staged` - - // altimate_change start — refuse a binary that carries a node-only stub (aws-node-resolve.ts) + // altimate_change start — refuse a build that bundled a node-only stub (aws-node-resolve.ts) // A browser entry's stub in place of a node export fails only at run time, on the path that calls it (the - // Bedrock credential chain did). Checked on the compiled file, since that is what ships. - const builtBinary = `dist/${name}/bin/altimate${item.os === "win32" ? ".exe" : ""}` - const stubs = nodeOnlyStubCount(fs.readFileSync(builtBinary)) - if (stubs > 0) { + // Bedrock credential chain did). + const stubbed = nodeOnlyStubInputs(bundled.metafile) + if (stubbed.length > 0) { throw new Error( - `${builtBinary} contains ${stubs} Symbol.for("node-only") stub(s): a package resolved to its browser entry. ` + - "Add it to NODE_RESOLVED_PACKAGES in script/aws-node-resolve.ts.", + `${name}: ${stubbed.length} bundled module(s) carry a Symbol.for("node-only") stub, so a package resolved to ` + + `its browser entry. Add it to NODE_RESOLVED_PACKAGES in script/aws-node-resolve.ts:\n${stubbed.join("\n")}`, ) } // altimate_change end + // Staging dir is no longer needed once Bun has embedded the shim + .node. + await $`rm -rf dist/${name}/.altimate-core-staged` + // Create backward-compatible altimate-code alias inside the platform package. // The npm wrapper (`packages/opencode/bin/altimate`) looks for `bin/altimate-code` // (or .exe) when locating the platform binary, so this must exist for the diff --git a/packages/opencode/test/fixture/aws-credential-chain-build.ts b/packages/opencode/test/fixture/aws-credential-chain-build.ts index 04de68af01..0447ec77e3 100644 --- a/packages/opencode/test/fixture/aws-credential-chain-build.ts +++ b/packages/opencode/test/fixture/aws-credential-chain-build.ts @@ -1,10 +1,11 @@ // altimate_change - new file // // Bundles aws-credential-chain.ts with the release build's resolution settings (the "browser" condition, and -// awsNodeResolvePlugin unless the mode is "plain") and prints the bundle's path. Run in its own process by -// test/provider/aws-node-resolve.test.ts: a second Bun.build inside `bun test` fails reading files. +// awsNodeResolvePlugin unless the mode is "plain"), embedding a note that quotes the stub marker the way build.ts +// embeds CHANGELOG.md. Prints the bundle's path and the bundled modules that carry a stub. Run in its own process +// by test/provider/aws-node-resolve.test.ts: a second Bun.build inside `bun test` fails reading files. import path from "path" -import { awsNodeResolvePlugin } from "../../script/aws-node-resolve" +import { awsNodeResolvePlugin, nodeOnlyStubInputs } from "../../script/aws-node-resolve" const [outdir, mode] = process.argv.slice(2) const out = await Bun.build({ @@ -13,9 +14,11 @@ const out = await Bun.build({ target: "bun", conditions: ["browser"], plugins: mode === "plain" ? [] : [awsNodeResolvePlugin], + define: { EMBEDDED_NOTE: JSON.stringify('A changelog line quoting Symbol.for("node-only").') }, + metafile: true, }) if (!out.success) { for (const log of out.logs) console.error(log) process.exit(1) } -console.log(out.outputs[0].path) +console.log(JSON.stringify({ file: out.outputs[0].path, stubInputs: nodeOnlyStubInputs(out.metafile) })) diff --git a/packages/opencode/test/fixture/aws-credential-chain.ts b/packages/opencode/test/fixture/aws-credential-chain.ts index a6974c84a4..c12e7dab39 100644 --- a/packages/opencode/test/fixture/aws-credential-chain.ts +++ b/packages/opencode/test/fixture/aws-credential-chain.ts @@ -4,5 +4,9 @@ // AWS credential chain for AWS_PROFILE, as the Amazon Bedrock provider does, and prints the access key id. import { fromNodeProviderChain } from "@aws-sdk/credential-providers" +// Defined by aws-credential-chain-build.ts; referenced so the bundle embeds it. +declare const EMBEDDED_NOTE: string +if (process.env.PRINT_EMBEDDED_NOTE) console.log(EMBEDDED_NOTE) + const credentials = await fromNodeProviderChain({ profile: process.env.AWS_PROFILE })() console.log(JSON.stringify({ accessKeyId: credentials.accessKeyId })) diff --git a/packages/opencode/test/provider/aws-node-resolve.test.ts b/packages/opencode/test/provider/aws-node-resolve.test.ts index 467e4ddd03..160701d39f 100644 --- a/packages/opencode/test/provider/aws-node-resolve.test.ts +++ b/packages/opencode/test/provider/aws-node-resolve.test.ts @@ -8,19 +8,19 @@ import { afterAll, describe, expect, test } from "bun:test" import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" import os from "os" import path from "path" -import { nodeOnlyStubCount } from "../../script/aws-node-resolve" +import { nodeOnlyStubCount, nodeOnlyStubInputs } from "../../script/aws-node-resolve" const dir = mkdtempSync(path.join(os.tmpdir(), "aws-node-resolve-")) afterAll(() => rmSync(dir, { recursive: true, force: true })) const builder = path.join(import.meta.dir, "..", "fixture", "aws-credential-chain-build.ts") -/** Bundles the fixture in its own process, with or without awsNodeResolvePlugin; returns the bundle's path. */ -function bundle(mode: "plugin" | "plain"): string { +/** Bundles the fixture in its own process, with or without awsNodeResolvePlugin. */ +function bundle(mode: "plugin" | "plain"): { file: string; stubInputs: string[] } { const run = Bun.spawnSync([process.execPath, builder, path.join(dir, mode), mode]) expect(run.stderr.toString()).toBe("") expect(run.exitCode).toBe(0) - return run.stdout.toString().trim() + return JSON.parse(run.stdout.toString().trim()) } /** Runs a bundled credential chain with a fake profile; only the scratch AWS files are visible to it. */ @@ -43,9 +43,11 @@ function resolveFakeProfile(file: string) { } describe("the AWS credential chain under the release build's browser condition", () => { - test("with awsNodeResolvePlugin, the bundle has no node-only stub and reads the profile", () => { - const file = bundle("plugin") - expect(nodeOnlyStubCount(readFileSync(file))).toBe(0) + test("with awsNodeResolvePlugin, no bundled module has a node-only stub and the bundle reads the profile", () => { + const { file, stubInputs } = bundle("plugin") + expect(stubInputs).toEqual([]) + // The one marker left is the embedded note's, which the check does not count as a stub. + expect(nodeOnlyStubCount(readFileSync(file))).toBe(1) const run = resolveFakeProfile(file) expect(run.stderr).not.toContain("parseKnownFiles") expect(run.exitCode).toBe(0) @@ -53,22 +55,25 @@ describe("the AWS credential chain under the release build's browser condition", }, 60_000) test("without it, the same bundle gets the browser stubs and the chain fails as it did in the binary", () => { - const file = bundle("plain") - expect(nodeOnlyStubCount(readFileSync(file))).toBeGreaterThan(0) + const { file, stubInputs } = bundle("plain") + expect(stubInputs.length).toBeGreaterThan(0) + for (const input of stubInputs) expect(input).toMatch(/node_modules\/@(smithy|aws-sdk)\//) const run = resolveFakeProfile(file) expect(run.exitCode).not.toBe(0) expect(run.stderr).toContain("parseKnownFiles is not a function") }, 60_000) - test("the release build registers the plugin and refuses a binary with a node-only stub", () => { + test("the release build registers the plugin and checks its bundled modules for node-only stubs", () => { const build = readFileSync(path.join(import.meta.dir, "..", "..", "script", "build.ts"), "utf8") expect(build).toMatch(/plugins: \[[^\]]*\bawsNodeResolvePlugin\b[^\]]*\]/) - expect(build).toContain("nodeOnlyStubCount(fs.readFileSync(builtBinary))") + expect(build).toContain("metafile: true") + expect(build).toContain("nodeOnlyStubInputs(bundled.metafile)") }) - test("nodeOnlyStubCount counts every stub", () => { + test("nodeOnlyStubCount counts every stub, and the check refuses to run without a metafile", () => { const stub = 'Symbol.for("node-only")' expect(nodeOnlyStubCount(Buffer.from(`a ${stub} b ${stub}${stub}`))).toBe(3) expect(nodeOnlyStubCount(Buffer.from("leader-node-only function"))).toBe(0) + expect(() => nodeOnlyStubInputs(undefined)).toThrow("no metafile") }) }) From 864b0e43cb6e7876f0b327ef5ac78e00b61620fc Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 1 Dec 2026 08:40:05 +0800 Subject: [PATCH 3/3] fix: fail the node-only stub check when it can't read a bundled module A metafile input that doesn't resolve from the build's cwd was skipped, so a path mismatch would have passed the check without reading anything. It now fails and names the modules, and fails on an empty input list. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQsV67ZmxvZP7buH4b5 --- packages/opencode/script/aws-node-resolve.ts | 17 ++++++++++++----- .../test/provider/aws-node-resolve.test.ts | 12 +++++++++--- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/packages/opencode/script/aws-node-resolve.ts b/packages/opencode/script/aws-node-resolve.ts index 01ad853047..64a38815f3 100644 --- a/packages/opencode/script/aws-node-resolve.ts +++ b/packages/opencode/script/aws-node-resolve.ts @@ -38,12 +38,19 @@ export function nodeOnlyStubCount(bytes: Uint8Array): number { /** * The bundled modules, from a Bun.build metafile, whose source carries a node-only stub. The check reads the * modules rather than the compiled binary: CHANGELOG.md, builtin skills and migrations are embedded verbatim and - * may quote the marker. + * may quote the marker. A module it cannot read fails the check, so it never passes without having looked. */ export function nodeOnlyStubInputs(metafile: { inputs: Record } | undefined): string[] { if (!metafile) throw new Error("Bun.build returned no metafile, so the node-only stub check cannot run") - return Object.keys(metafile.inputs).filter((input) => { - const file = path.resolve(input) - return fs.existsSync(file) && nodeOnlyStubCount(fs.readFileSync(file)) > 0 - }) + const inputs = Object.keys(metafile.inputs) + if (inputs.length === 0) throw new Error("Bun.build's metafile lists no inputs, so the node-only stub check cannot run") + // Metafile inputs are relative to the build's working directory. + const unreadable = inputs.filter((input) => !fs.existsSync(path.resolve(input))) + if (unreadable.length > 0) { + throw new Error( + `The node-only stub check cannot read ${unreadable.length} bundled module(s) from ${process.cwd()}:\n` + + unreadable.join("\n"), + ) + } + return inputs.filter((input) => nodeOnlyStubCount(fs.readFileSync(path.resolve(input))) > 0) } diff --git a/packages/opencode/test/provider/aws-node-resolve.test.ts b/packages/opencode/test/provider/aws-node-resolve.test.ts index 160701d39f..31b60ec3ff 100644 --- a/packages/opencode/test/provider/aws-node-resolve.test.ts +++ b/packages/opencode/test/provider/aws-node-resolve.test.ts @@ -46,8 +46,8 @@ describe("the AWS credential chain under the release build's browser condition", test("with awsNodeResolvePlugin, no bundled module has a node-only stub and the bundle reads the profile", () => { const { file, stubInputs } = bundle("plugin") expect(stubInputs).toEqual([]) - // The one marker left is the embedded note's, which the check does not count as a stub. - expect(nodeOnlyStubCount(readFileSync(file))).toBe(1) + // The markers left are the embedded note's, which the check does not count as stubs. + expect(nodeOnlyStubCount(readFileSync(file))).toBeGreaterThan(0) const run = resolveFakeProfile(file) expect(run.stderr).not.toContain("parseKnownFiles") expect(run.exitCode).toBe(0) @@ -70,10 +70,16 @@ describe("the AWS credential chain under the release build's browser condition", expect(build).toContain("nodeOnlyStubInputs(bundled.metafile)") }) - test("nodeOnlyStubCount counts every stub, and the check refuses to run without a metafile", () => { + test("nodeOnlyStubCount counts every stub", () => { const stub = 'Symbol.for("node-only")' expect(nodeOnlyStubCount(Buffer.from(`a ${stub} b ${stub}${stub}`))).toBe(3) expect(nodeOnlyStubCount(Buffer.from("leader-node-only function"))).toBe(0) + }) + + test("the module check fails rather than pass without reading every bundled module", () => { expect(() => nodeOnlyStubInputs(undefined)).toThrow("no metafile") + expect(() => nodeOnlyStubInputs({ inputs: {} })).toThrow("lists no inputs") + const unreadable = path.relative(process.cwd(), path.join(dir, "not-on-disk.js")) + expect(() => nodeOnlyStubInputs({ inputs: { [unreadable]: {} } })).toThrow("cannot read 1 bundled module(s)") }) })