yarnpkg / berry

πŸ“¦πŸˆ Active development trunk for Yarn βš’
https://yarnpkg.com
BSD 2-Clause "Simplified" License
7.35k stars 1.1k forks source link

🐞 `yarn set version` on Node.js <18 #5829

Open arcanis opened 11 months ago

arcanis commented 11 months ago

I open this thread to cover an issue we noticed regarding Yarn 3.x when used with versions of Node.js lower than 18. It's a little complex, so it's worth opening a thread outlining the problem and the progress on solving it.

What happens?

Running yarn set version on Yarn 1.x project will cause it to crash if running with versions of Node.js lower than 18. In particular this happens even when running yarn set version 3.6.4, which one would expect to work (since 3.x supports both Node.js 14 and 16).

Why does that happen?

As you may know, Yarn 1.x has a different codebase than modern releases. To make yarn set version work there, Yarn 1.x actually pulls the latest version of Yarn and forwards the yarn set version call to it. Unfortunately, it always pulls the latest build - so when we published Yarn 4.0 as stable (which drops support for both Node.js 14 and 16), things broke.

Why did we miss it?

It's not a common problem. The installation guide for 3.x releases recommended checking-in the Yarn binary, so running yarn set version on CI isn't the most common experience - typical projects already have 3.x installed in their repo, so they don't need to pull the binary at image time.

How can you workaround it?

The easiest solution is to not use yarn set version with Node.js 12 / 14 / 16. Assuming you cannot upgrade to Node.js 18.x, here are some things you can try:

Or, for Node.js 16.x users:

Or, for Node.js 12.x / 14.x users:

What are we doing to fix that?

Ideally we'd like to "unbreak" it without you having to do anything. We already shipped a mitigation which should help people running Yarn without Corepack, but if you use Corepack things things get quite difficult, and I'm not sure it'll be possible. The best we can do will probably be to release a fix in Yarn 1.x, but older Node.js 12 / 14 / 16 Docker images probably have outdated global Yarn binaries in the first place, so releasing an hotfix won't help.

We're sorry about this problem, and we have plans to avoid them in the future. For now, I suggest to try the listed workarounds and let us know if none of them work for you. I'll update this post as needed.

sivnath-pt commented 10 months ago

Any workaround so far a part from the version upgrade? We are currently using node.js 14 and 16

brunozoric commented 10 months ago

What I did was:

vipul-nema commented 10 months ago

@arcanis - Issue seems fixed today automatically , Did your team publish any fix. ?

we were using - RUN yarn set version 3.5.0 in docker file. and it was giving same error.

We would enable corepack too now.

litinskii commented 10 months ago

FROM node:16.16.0

RUN yarn set version 3.6.4

RUN touch yarn.lock RUN yarn install

works for me

arcanis commented 10 months ago

@arcanis - Issue seems fixed today automatically , Did your team publish any fix. ?

Yes, the mitigation I mentioned in the post:

Or, for Node.js 12.x / 14.x users:

  • Make sure you use Yarn without Corepack. We released a mitigation which should restore the original behaviour, as long as you don't use Corepack.

In your case you're not using Corepack on those old Node.js images, so our mitigation was sufficient. It's probably the same for most people, but to be careful I preferred to list alternative strategies.

remus-selea commented 10 months ago

This issue occurs with node 20.x as well. yarn set version 3.6.4 sets 4.0.1 instead of 3.6.4

image

merceyz commented 10 months ago

That's a bug in Yarn v1 and not related to the Node.js version you're using, for example it happens on Node.js v18 as well:

$ docker run --rm -it node:18.18.2 bash -c "corepack enable && corepack yarn@1.22.19 set version 3.6.4 && yarn --version"
4.0.1

@arcanis Seems we need to update https://github.com/yarnpkg/yarn/blob/98036fccb545e4604e9e1ea710dff4e18d7dcc28/src/cli/commands/policies.js#L170 to use the version range provided by the user.

anilgsharma900 commented 1 month ago

This issue occurs with node 20.x as well. yarn set version 3.6.4 sets 4.0.1 instead of 3.6.4

image

I am also getting same error, I am using node 20 and yarn classic version 1.22.22 . I resovled it after mentioned "packageManager": "yarn@1.22.22"