I had quite a trouble trying to build Anki, and the cause was because Yarn Modern (Berry) was being used instead of Yarn Classic (1.x).
At first, I didn’t realize that wrong Yarn version was the cause. The build fails while building canvas@npm:1.0.0 package, with the message that 'cairo/cairo.h' file not found.
So I thought maybe libcairo was missing, and installed libcairo from homebrew and tried other similar solutions, to no avail.
It turns out that the error happens when using Yarn Modern because it interprets the "resolutions" field in package.json differently, and tries to build an older canvas package. When using Yarn Classic, the error does not occur as the package is not built at all.
Figuring out how to switch to Yarn Classic was a hassle as well. Just switching global yarn installation to Yarn Classic does not work. Anki uses its own copy of yarn that it downloads using corepack, so I had to change corepack’s version of yarn using:
corepack install -g yarn@^1
I’m writing this post so that if others have the same issue as mine, they can come across this post and find out what’s wrong.
And I think it should be added to the documentations that you need to change corepack yarn to Yarn Classic.
From what I can figure out, it depends on the global cache file ~/.cache/node/corepack/lastKnownGood.json. If I delete this file, yarn v1 is correctly used.
lastKnownGood.json is created with {yarn: 3.xx} when you install yarn v3 with corepack. So if you’ve installed yarn v3 with corepack, then Anki build will also use yarn v3.
So Anki build will use yarn v1 only when you’ve never used yarn v2+ with corepack.
Btw I’m using Mac (M1), but I think corepack should act the same on linux and mac.
Sorry, I wasn’t aware of that, and assumed this was yet another case of distro packages making our lives harder. The relevant code is in YarnSetup in node.rs. Are you saying that updating the existing commands to add @^1 will resolve this?
corepack enable don’t support version ranges. The command just creates a shim file so you can use the yarn command as a shortcut for corepack yarn.
I think the intended way of using corepack to pin version is to use the "packageManger" field in package.json.
It can be set up automatically by using the command corepack use yarn@^1. Once set up, calling corepack yarn will use (and installs if needed) the specified yarn version.