Skip to content

Fix native sharp embedding so macOS (and other targets) can build#1

Merged
xjdr-noumena merged 1 commit into
Noumena-Network:mainfrom
masonjames:fix/macos-cross-platform-sharp-build
Jun 18, 2026
Merged

Fix native sharp embedding so macOS (and other targets) can build#1
xjdr-noumena merged 1 commit into
Noumena-Network:mainfrom
masonjames:fix/macos-cross-platform-sharp-build

Conversation

@masonjames

Copy link
Copy Markdown
Contributor

Problem

bun run build and bun run build:source fail on macOS with:

error: Could not resolve: "../../../node_modules/@img/sharp-linux-x64/lib/sharp-linux-x64.node"
    at src/shims/assets/sharpLinuxX64.ts:1:23
error: Could not resolve: "../../../node_modules/@img/sharp-linuxmusl-x64/lib/sharp-linuxmusl-x64.node"
    ...

The sharp shim (src/shims/sharp/sharpBinding.cjs) statically imports the linux-x64 and linux-x64-musl @img/sharp-* native assets via import … with { type: 'file' }. Bun only installs the @img/sharp-* optional dependencies that match the build host's os/cpu, so on macOS (and Linux arm64, etc.) those Linux files are absent and the bundler aborts.

This breaks the macOS "build from source" path that OSS_BUILD.md lists as supported. The build had effectively only ever worked on linux-x64.

Fix

Make the native sharp embedding target-aware instead of Linux-only:

  • Add darwin asset shims: src/shims/assets/sharpDarwinArm64.ts, sharpDarwinX64.ts.
  • Add per-target bindings: sharpBinding.darwin-arm64.cjs, sharpBinding.darwin-x64.cjs. Each binding only imports its own platform's assets — no single build host has every platform's @img packages installed, so an all-platforms shim can never bundle anywhere.
  • Extract the shared materialize-and-load logic into loadEmbeddedSharp.cjs; the existing linux binding now uses it. Linux behavior is unchanged.
  • In build/build.mjs, select the sharp binding per compile target (linux-x64, linux-x64-musl, darwin-arm64, darwin-x64), defaulting to the build host for source/standalone builds.
  • Ignore Bun's *.bun-build compile intermediates.

Verification

On macOS arm64:

  • bun run build now completes → .tmp/packages/ncode-0.1.0-darwin-arm64/ncode.
  • The binary runs: ncode --version0.1.0 (NCode), ncode --help works.
  • A standalone compiled probe confirms the embedded darwin sharp addon materializes and dlopens libvips through its @loader_path rpath at runtime (the darwin .node resolves @rpath/libvips-cpp.8.17.3.dylib via @loader_path/../../sharp-libvips-darwin-arm64/lib, which matches the materialized layout).
  • bun test src/shims/nativeAssetRuntime.test.ts passes.

The linux-x64 / linux-x64-musl code path is untouched apart from the shared-loader refactor.

Notes

  • The unrelated rustc 1.87 … napi-build requires 1.88 warnings during the build are non-fatal (the build continues with JS/SSE fallbacks) and are out of scope here.

🤖 Generated with Claude Code

The sharp shim statically imported the linux-x64 and linux-x64-musl
`@img/sharp-*` native assets via `import ... with { type: 'file' }`.
Bun only installs the `@img/sharp-*` optional dependencies matching the
build host's os/cpu, so on macOS those Linux files are absent and the
bundler fails to resolve them, aborting both `bun run build` and
`bun run build:source`. This broke the macOS "build from source" path
that OSS_BUILD.md documents as supported.

Make the embedding target-aware instead of Linux-only:

- Add darwin asset shims (sharpDarwinArm64.ts, sharpDarwinX64.ts).
- Add per-target bindings (sharpBinding.darwin-arm64.cjs,
  sharpBinding.darwin-x64.cjs); each only imports its own platform's
  assets, since no single host has every platform's packages installed.
- Extract the shared materialize-and-load logic into
  loadEmbeddedSharp.cjs; the linux binding now uses it (behavior
  unchanged).
- Select the binding per compile target in build.mjs, defaulting to the
  build host for source/standalone builds.
- Ignore Bun's *.bun-build compile intermediates.

Verified: `bun run build` produces a working ncode-darwin-arm64 binary,
and a standalone probe confirms the embedded darwin sharp addon loads
libvips through its @loader_path rpath at runtime.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@xjdr-noumena xjdr-noumena self-assigned this Jun 17, 2026
@xjdr-noumena

xjdr-noumena commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

This looks fine, i will most likely merge once i can test. i am setting up MacOS and Windows build and test clusters this week. stay tuned

@xjdr-noumena xjdr-noumena merged commit 2512d1a into Noumena-Network:main Jun 18, 2026
@xjdr-noumena

Copy link
Copy Markdown
Contributor

merged, thank you again for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants