Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for bun runtime #3511

Closed
asilvas opened this issue Jan 1, 2023 · 41 comments
Closed

Support for bun runtime #3511

asilvas opened this issue Jan 1, 2023 · 41 comments

Comments

@asilvas
Copy link

asilvas commented Jan 1, 2023

Feature request

What are you trying to achieve?

Bun seems like a good fit for sharp/vips with the focus on performance. The reduction in overhead from bindings could be a win with Bun. Currently no good image processing package for Bun, so this would given even more reason to promote sharp.

When you searched for similar feature requests, what did you find that might be related?

Nothing

What would you expect the API to look like?

n/a

What alternatives have you considered?

Currently use sharp extensively with Node.

Please provide sample image(s) that help explain this feature

n/a

[8.68ms] "node_modules.bun" - 15 modules, 8 packages
29 |     if (loadedModule) {
30 |       const [, loadedPackage] = loadedModule.match(/node_modules[\\/]([^\\/]+)[\\/]/);
31 |       help.push(`- Ensure the version of sharp aligns with the ${loadedPackage} package: "npm ls sharp"`);
32 |     }
33 |   }
34 |   throw new Error(help.join('\n'));
            ^
error:
Something went wrong installing the "sharp" module

Cannot find module "../build/Release/sharp-linux-x64.node" from "./node_modules/sharp/lib/sharp.js"

Possible solutions:
- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
- Install for the current linux-x64 runtime: "npm install --platform=linux --arch=x64 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install
@lovell
Copy link
Owner

lovell commented Jan 1, 2023

This will be dependent on oven-sh/bun#158 and oven-sh/bun#606

@asilvas
Copy link
Author

asilvas commented Jan 2, 2023

This will be dependent on oven-sh/bun#158 and oven-sh/bun#606

Great thanks. I suspect those issues will be unblocked in the next couple months at the rate they're going. I'll try to remember to update this thread once ready.

@birkskyum
Copy link

@lovell , would you be able to narrow down what parts of oven-sh/bun#158 are lacking in order for sharp to function?

@lovell
Copy link
Owner

lovell commented Jul 10, 2023

@birkskyum It looks like almost all of Node-API has been implemented in Bun during the last 6 months - perhaps someone can test it again?

@birkskyum
Copy link

Okay, interesting that it could be resolved already. The other issue mentioned breaks on installation (oven-sh/bun#606), so let's assess the situation when that is fixed.

@infrahead
Copy link

Yes, I was pleasantly surprised to see sharp working on Bun v0.6.14! This was based on some very simple scripts I was using to keep tabs on this progress, not a test of the full API. To @birkskyum point, this is possible as I'm using my own install script (with prebuilt binaries) to avoid all the npm install overhead and lifecycle script permissions.

@birkskyum
Copy link

Landing WebAssembly build #3522 would be a great step to resolve this too, as it would provide a very portable fallback with reduced performance.

@JawadAhbab
Copy link

JawadAhbab commented Aug 20, 2023

Can anyone give me a working example please?

@paperdave
Copy link

Once Bun 0.8.0 releases, Sharp will work (napi features sharp depends on is more stable/fixed a bad crash), though the NPM lifecycle script not running is still an issue, see oven-sh/bun#606

@xhyrom
Copy link

xhyrom commented Aug 22, 2023

For now, you can manually run post install script

@birkskyum
Copy link

@xhyrom , how do i run postinstall with bun?

@xhyrom
Copy link

xhyrom commented Aug 23, 2023

@xhyrom , how do i run postinstall with bun?

cd node_modules/sharp
bun install

then wait until install and try to run your code again, it should work :D

@birkskyum
Copy link

@xhyrom , it works directly on my machine, which is awesome, but when doing this within a Dockerfile I get:

error: script not found "install/libvips"
error: script not found "install/can-compile"

My entire dockerfile is:

FROM oven/bun

WORKDIR /usr/repo

COPY . .

RUN bun install
RUN cd ./node_modules/sharp && bun install

EXPOSE 3001

CMD [ "bun", "--bun", "run", "./src/app.ts" ]

@birkskyum
Copy link

birkskyum commented Aug 23, 2023

Seems like the script can be found by making the paths in sharp's package.json relative, so they are i.e. ./install/libvips.js install of install/libvips.js - unfortunately that still yields:

sharp: Installation error: detectLibc.versionSync is not a function. (In 'detectLibc.versionSync()', 'detectLibc.versionSync' is undefined)
sharp: Please see https://sharp.pixelplumbing.com/install for required dependencies

@xhyrom
Copy link

xhyrom commented Aug 23, 2023

Not sure what to do, with npm you could do npm rebuild --arch=x64 --platform=linux sharp but you want bun

@birkskyum
Copy link

I realized that the bun runtime works with sharp, but that bun install doesn't, so an easy solution was to just install with npm and run with bun after.

@birkskyum
Copy link

birkskyum commented Aug 26, 2023

Normal Sharp install in Bun will likely be resolved by:

Then sharp just has to be added in package.json as trustedDependencies: ["sharp"]

@xhyrom
Copy link

xhyrom commented Aug 26, 2023

Normal Sharp install in Bun will likely be resolved by:

Then sharp just has to be added in package.json as trustedDependencies: ["sharp"]

Can you try to put sharp into trustedDependencies and try bun install again? It should run the install script. Just want to know if it resolve the libc versionSync error

@birkskyum
Copy link

birkskyum commented Aug 26, 2023

With latest canary, and sharp in trustedDependencies, I get this, and i think it's because it can't find the arm64 bundle:

➜ bun run start
$ bun run ./src/app.ts
[0.01ms] ".env"
error: script "start" exited with code 11 (SIGSEGV)

@infrahead
Copy link

Simply using npm install in case of sharp seems to be the answer for now. I'd also suggest that the real solution will be #3750 and not Bun's trustedDependencies convenience feature, as some will never allow install scripts.

@birkskyum
Copy link

I made a separate ticket for supporting bun install. This ticket about bun runtime appear to be resolved.

@Jarred-Sumner
Copy link

With latest canary, and sharp in trustedDependencies, I get this, and i think it's because it can't find the arm64 bundle:

➜ bun run start
$ bun run ./src/app.ts
[0.01ms] ".env"
error: script "start" exited with code 11 (SIGSEGV)

It is unlikely that bun not finding the .node file would cause a segfault. It sounds like we still have a crash to fix here

@birkskyum
Copy link

birkskyum commented Aug 29, 2023

Result of using the debug build:

loader [parsing] /Users/admin/repos/backend/node_modules/stripe/esm/autoPagination.js
Loader [resolve] ./utils.js
loader [parsing] /Users/admin/repos/backend/node_modules/stripe/esm/multipart.js
Loader [resolve] ./utils.js
32 |     if (loadedModule) {
33 |       const [, loadedPackage] = loadedModule.match(/node_modules[\\/]([^\\/]+)[\\/]/);
34 |       help.push(`- Ensure the version of sharp aligns with the ${loadedPackage} package: "npm ls sharp"`);
35 |     }
36 |   }
37 |   throw new Error(help.join('\n'));
            ^
error: 
Something went wrong installing the "sharp" module

Cannot find module "../build/Release/sharp-darwin-arm64v8.node" from "/Users/admin/repos/backend/node_modules/sharp/lib/sharp.js"

Possible solutions:
- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
- Install for the current darwin-arm64v8 runtime: "npm install --platform=darwin --arch=arm64v8 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install
      at /Users/admin/repos/backend/node_modules/sharp/lib/sharp.js:37:8
      at globalThis (/Users/admin/repos/backend/node_modules/sharp/lib/sharp.js:37:33)
      at require (:1:20)
      at /Users/admin/repos/backend/node_modules/sharp/lib/constructor.js:11:0
      at globalThis (/Users/admin/repos/backend/node_modules/sharp/lib/constructor.js:439:17)
      at require (:1:20)
      at /Users/admin/repos/backend/node_modules/sharp/lib/index.js:6:6
      at globalThis (/Users/admin/repos/backend/node_modules/sharp/lib/index.js:16:17)
      at require (:1:20)
      at /Users/admin/repos/backend/node_modules/join-images/lib/main.js:7:24
      at globalThis (/Users/admin/repos/backend/node_modules/join-images/lib/main.js:89:18)
error: script "start" exited with code 1 (SIGHUP)

@qorbani
Copy link

qorbani commented Oct 6, 2023

@hendrikboeck in your message and code snippet you misspelled trustedDependencies 😄

@lovell
Copy link
Owner

lovell commented Oct 9, 2023

Please take a look at #3750 (comment) if you'd like to test an early pre-release of the forthcoming sharp v0.33.0 that should "just work" with Bun without setting trustedDependencies.

@MariuzM
Copy link

MariuzM commented Nov 18, 2023

Sharp sharp@0.33.0-alpha.11 works locally with Bun but when i deploy to railway i get this error

image

Only diff is that i'm using docker on railway

FROM oven/bun:1.0.12-alpine

WORKDIR /usr/src/app
COPY . .
RUN bun install
RUN bun run build

ENV PORT 3001

EXPOSE 3000

CMD bun run start

@lovell
Copy link
Owner

lovell commented Nov 18, 2023

@MariuzM #3848 will fix the wrong error being shown, but there's still an underlying problem. It looks like you're using some kind of bundler, so please ensure sharp is marked as "external" - see https://sharp.pixelplumbing.com/install#bundlers

@MariuzM
Copy link

MariuzM commented Nov 18, 2023

I don't think i use anything else apart what Bun already gives, my build command is
bun build ./src/app.ts --outdir ./build --target bun

i have tried
bun build ./src/app.ts --outdir ./build --target bun --external sharp

but still same issue

@lovell
Copy link
Owner

lovell commented Nov 18, 2023

@MariuzM This looks like something specific to the bun build command itself. Perhaps you could create a minimal, standalone repo that allows someone else to reproduce.

@MariuzM
Copy link

MariuzM commented Nov 18, 2023

@lovell https://github.com/MariuzM/bun-sharp and i have tested on Railway still same issue

@MariuzM
Copy link

MariuzM commented Nov 18, 2023

Ive just tested locally with Docker and issue is there, but when NOT using docker i don't have this issue, so not sure what difference does docker make, my docker config looks simple enough

# https://hub.docker.com/r/oven/bun/tags?page=1&name=alpine
FROM oven/bun:1.0.12-alpine

WORKDIR /usr/src/app
COPY . .
RUN bun install
RUN bun run build

ENV PORT 3001

EXPOSE 3000

CMD bun run start
image

@hendrikboeck
Copy link

@MariuzM I fixed your problem. I think you forgot to add semver to your package.json and forgot to install vips-dev in Alpine, as you declared it as external.

my Dockerfile:

# https://hub.docker.com/r/oven/bun/tags?page=1&name=alpine
FROM oven/bun:1.0.12-alpine

RUN apk add vips-dev

WORKDIR /usr/src/app
COPY . .
RUN bun install
RUN bun run build

EXPOSE 3000
CMD bun run start

my package.json:

{
  "name": "bun-sharp",
  "version": "1.0.0",
  "scripts": {
    "d": "bun run --watch src/app.ts",
    "b": "bun build ./src/app.ts --outdir ./build --target bun --external sharp",
    "dev": "bun run d",
    "build": "bun run b",
    "start": "bun run ./build/app.js"
  },
  "dependencies": {
    "elysia": "^0.7.28",
    "semver": "^7.5.4",
    "sharp": "0.33.0-alpha.11"
  },
  "devDependencies": {
    "@types/node": "^20.9.1",
    "@types/uuid": "^9.0.7",
    "@typescript-eslint/eslint-plugin": "^6.11.0",
    "@typescript-eslint/parser": "^6.11.0",
    "bun-types": "^1.0.12",
    "eslint": "8.54.0",
    "eslint-plugin-import": "^2.29.0",
    "prettier": "^3.1.0"
  },
  "module": "src/app.js"
}

my .dockerignore:

/node_modules
/build

@MariuzM
Copy link

MariuzM commented Nov 18, 2023

@hendrikboeck thank you this worked. Its not that i forgot i just did not know 😁

@lovell
Copy link
Owner

lovell commented Nov 18, 2023

@MariuzM Whilst it might appear to work, there is no need to add the vips-dev package. In addition, the fact that you also had to add a dependency on semver despite your code not depending upon it does rather suggest that bun build is still not working as advertised.

@lovell
Copy link
Owner

lovell commented Nov 19, 2023

@MariuzM Your container uses Alpine Linux but Bun does not officially support musl-based Linux - oven-sh/bun#918

In addition, Bun polyfills the detect-libc dependency and returns a hard-coded glibc on all Linux platforms - see https://github.com/oven-sh/bun/blob/main/src/js/thirdparty/detect-libc.linux.js

@MariuzM
Copy link

MariuzM commented Nov 19, 2023

@lovell thank you for taking time to explain in more detail, dint realized Alpine had missing packages.

@lovell
Copy link
Owner

lovell commented Nov 29, 2023

v0.33.0 is now available and tested working with the Bun JavaScript runtime on both Linux and macOS.

https://github.com/lovell/sharp/actions/runs/7032565186

@lovell lovell closed this as completed Nov 29, 2023
@MariuzM
Copy link

MariuzM commented Nov 29, 2023

THANK YOUUUUU

@7heMech
Copy link

7heMech commented Feb 12, 2024

Nope, it doesn't work.
I'm getting this error in a replit's nix environment, just by trying to import it.

Could not load the "sharp" module using the linux-x64 runtime
undefined: libstdc++.so.6: cannot open shared object file: No such file or directory

Repl link

@lovell
Copy link
Owner

lovell commented Feb 12, 2024

@7heMech It looks like you're using 0.33.0-alpha.6, which is an unsupported pre-release version. Please also ensure you're using the latest Bun as the .replit file suggests an older version.

Repository owner locked and limited conversation to collaborators Feb 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests