Closed simplenotezy closed 2 years ago
Not sure if it is related to worker threads (https://sharp.pixelplumbing.com/install#worker-threads). The error does occur when running my tests, which I believe could be multi-threaded (Using Ava 4)
Did you try ava --no-worker-threads
?
@lovell Thanks for prompt reply. Just realised it might be due to that, so I am about to issue a deploy without worker threads. If this is the case, my next question would be: how can I have ava with multiple worker threads to work with Sharp?
Yes, it actually fixed it.
https://sharp.pixelplumbing.com/install#worker-threads
The main thread must call
require('sharp')
before worker threads are created to ensure shared libraries remain loaded in memory until after all threads are complete.
You'll need to work out how to inject your own logic into ava's main thread.
Alternatively, use a musl-based Linux such as Alpine, which sensibly ignores Node.js' attempt to call dlclose
- see https://wiki.musl-libc.org/functional-differences-from-glibc.html#Unloading-libraries
I've slightly expanded the existing docs about this via commit https://github.com/lovell/sharp/commit/d0c8e9564163cb8d1e1096c503f0c03d906f2666
Awesome, thanks!
You can also do this as a better workaround (save this as ava.config.js
and remove ava config from your package.json
):
const isCI = require('is-ci');
module.exports = {
workerThreads: !isCI
};
Thanks @titanism that's a neat solution.
Given this is primarily due to a feature of glibc-based Linux, a possible alternative might be something like:
const { familySync, GLIBC } = require('detect-libc');
module.exports = {
workerThreads: familySync() !== GLIBC
};
In my case I needed worker threads to use AVA's shared workers.
Just requiring the module in the config file for AVA:
// ava.config.js
require("sharp");
// ...
worked for me.
The forthcoming libvips v8.13.0, which will ship with the forthcoming sharp v0.31.0, should remove the need for all these workarounds - see https://github.com/libvips/libvips/pull/2934
Hi there @lovell per https://github.com/forwardemail/forwardemail.net/commit/5460f9ab585c31c3c8729f81f8bb5f82df514116 we updated to latest version v0.32.1 and the error still persists with worker threads. This issue is still not resolved. Please re-open or update the worker threads docs at https://sharp.pixelplumbing.com/install#worker-threads to suggest the following workaround:
npm install detect-libc
// ava.config.js
const { familySync, GLIBC } = require('detect-libc');
module.exports = {
// <https://github.com/lovell/sharp/issues/3164>
workerThreads: familySync() !== GLIBC
};
@lovell we updated the docs for you, please see PR at https://github.com/lovell/sharp/pull/3692
@titanism As of sharp v0.31.0 this should no longer be a problem. Are you able to verify if the removal of this line still allows your tests to pass? https://github.com/forwardemail/forwardemail.net/blob/748b21304ec5224d14ab5b98e7dc0e51ad96b72f/ava.config.js#L10
Hello,
I got a similar problem migrating from a single config of vitest
to a workspace setup.
I'm not sure what exactly happened. Before migration I used vmThreads
pooling method, afterwards I had multiple projects, each using the exact same method.
Unfortunately I'm not that into what's happening in vitest
to tell exactly what's changed between non-workspace and workspace environments.
Adding import("sharp")
into general vitest.config.ts
(which is in an outer nodejs context, I presume) resolved the problem.
@lovell ~I've verified and confirmed this fixes the issue in~ https://github.com/forwardemail/forwardemail.net/commit/69b84019e165986f5fbce95e6024e557eed2ecb6. Thank you for your follow up 🙏 We love using sharp
in https://forwardemail.net.
Actually @lovell I stand corrected, this is a bug and is still not working.
Same error:
2024-07-19T17:27:17.7673481Z Uncaught exception in test/caldav/index.js
2024-07-19T17:27:17.7674145Z
2024-07-19T17:27:17.7674430Z test/caldav/index.js:20
2024-07-19T17:27:17.7674874Z
2024-07-19T17:27:17.7680687Z 19:
2024-07-19T17:27:17.7682003Z 20: const utils = require('../utils');
2024-07-19T17:27:17.7683032Z 21: const CalDAV = require('../../caldav-server');
2024-07-19T17:27:17.7683608Z
2024-07-19T17:27:17.7684830Z Error:
2024-07-19T17:27:17.7685536Z Something went wrong installing the "sharp" module
2024-07-19T17:27:17.7686116Z
2024-07-19T17:27:17.7687860Z Module did not self-register: '/home/runner/work/forwardemail.net/forwardemail.net/node_modules/.pnpm/sharp@0.32.0/node_modules/sharp/build/Release/sharp-linux-x64.node'.
2024-07-19T17:27:17.7689444Z
2024-07-19T17:27:17.7689765Z Possible solutions:
2024-07-19T17:27:17.7690928Z - Using worker threads? See https://sharp.pixelplumbing.com/install#worker-threads
2024-07-19T17:27:17.7692922Z - Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
2024-07-19T17:27:17.7695422Z - Install for the current linux-x64 runtime: "npm install --platform=linux --arch=x64 sharp"
2024-07-19T17:27:17.7697253Z - Consult the installation documentation: https://sharp.pixelplumbing.com/install
2024-07-19T17:27:17.7698100Z
@lovell not working in sharp@0.32.0
No big deal though btw, we have no problem using workerThreads: false
in CI. It doesn't really affect us 🙏
If possible please can you try v0.33.0+ as the final piece of the puzzle was added via commit https://github.com/lovell/sharp/commit/4a37a27cca34782a213e4ac7773299476a6c294b
Done per https://github.com/forwardemail/forwardemail.net/commit/22d5c0429e75e0f5c0b5f8be75257601020157e7, waiting on build to finish.
Ah, it requires a Node engine change:
Error: Could not load the "sharp" module using the darwin-arm64 runtime
Possible solutions:
- Please upgrade Node.js:
Found 18.16.0
Requires ^18.17.0 || ^20.3.0 || >=21.0.0
Going to try to get this sorted out and report back.
It works @lovell !!!! ✅
https://github.com/forwardemail/forwardemail.net/actions/runs/10013293960/job/27680579340 (passing)
Possible bug
I've been noticing this is quite a normal error message to receive, but most of the suggestions recommend deleting node_modules, or similar - which is not really relevant in my case, since the error only happens during a Github Actions deployment.
My github actions step looks something like this:
npm install --platform=linux --ignore-scripts=false --arch=x64 sharp
afternpm ci
but it will give me the same error.package-lock.json
to see if that would make a difference during deployment, but to no avail.npm install --platform=linux --arch=x64 --verbose sharp
in the github actions runner: https://gist.github.com/simplenotezy/bb1dc5fa506c8629c03d80703247969eI am not really sure how to proceed here.
Not sure if it is related to worker threads (https://sharp.pixelplumbing.com/install#worker-threads). The error does occur when running my tests, which I believe could be multi-threaded (Using Ava 4)
Is this a possible bug in a feature of sharp, unrelated to installation?
npm install sharp
completes without error.node -e "require('sharp')"
completes without error.Are you using the latest version of sharp?
sharp
as reported bynpm view sharp dist-tags.latest
.What is the output of running
npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp
?What is the expected behaviour?
Should install without errors
Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem
This happens during my Github Actions deployment, of a Nest.js framework that uses Sharp.
Please provide sample image(s) that help explain this problem