setup-node: Node setup fails non-deterministically with Yarn starting on November 17th

Description:

When using the “setup-node” action, CI non-deterministically fails, meaning that sometimes, I get no errors, and other times, I get errors.

Here is an example CI run where you can see the failures: https://github.com/IsaacScript/isaacscript/actions/runs/6907352290/job/18794331354

Specifically, the output is as follows:

Prepare all required actions
Getting action download info
Download action repository 'actions/setup-node@v4' (SHA:8f15[2](https://github.com/IsaacScript/isaacscript/actions/runs/6907352290/job/18794331354#step:3:2)de45cc[3](https://github.com/IsaacScript/isaacscript/actions/runs/6907352290/job/18794331354#step:3:3)93bb[4](https://github.com/IsaacScript/isaacscript/actions/runs/6907352290/job/18794331354#step:3:4)8ce[5](https://github.com/IsaacScript/isaacscript/actions/runs/6907352290/job/18794331354#step:3:5)d89d3[6](https://github.com/IsaacScript/isaacscript/actions/runs/6907352290/job/18794331354#step:3:7)b731f54556e65)
Download action repository 'actions/cache@v3' (SHA:704facf57e6[13](https://github.com/IsaacScript/isaacscript/actions/runs/6907352290/job/18794331354#step:3:15)6b1bc63b828d79edcd491f0ee84)
Run ./.github/workflows/setup
Run actions/setup-node@v4
Found in cache @ /opt/hostedtoolcache/node/20.9.0/x64
Environment details
/usr/local/bin/yarn --version
1.22.21
/usr/local/bin/yarn cache dir
error This project's package.json defines "packageManager": "yarn@4.0.2". However the current global version of Yarn is 1.22.21.

Presence of the "packageManager" field indicates that the project is meant to be used with Corepack, a tool included by default with all official Node.js distributions starting from 16.9 and [14](https://github.com/IsaacScript/isaacscript/actions/runs/6907352290/job/18794331354#step:3:16).[19](https://github.com/IsaacScript/isaacscript/actions/runs/6907352290/job/18794331354#step:3:22).
Corepack must currently be enabled by running corepack enable in your terminal. For more information, check out https://yarnpkg.com/corepack.
Error: error This project's package.json defines "packageManager": "yarn@4.0.2". However the current global version of Yarn is 1.22.[21](https://github.com/IsaacScript/isaacscript/actions/runs/6907352290/job/18794331354#step:3:24).

Presence of the "packageManager" field indicates that the project is meant to be used with Corepack, a tool included by default with all official Node.js distributions starting from 16.9 and 14.19.
Corepack must currently be enabled by running corepack enable in your terminal. For more information, check out https://yarnpkg.com/corepack.

Thus, we can see that this GitHub Action is failing to install Yarn properly, since Yarn appears to be version 1 instead of version 4.

For more context, we can see that this repository has a GitHub Actions matrix set up where it spawns N jobs and all of the jobs use “setup-node”. Thus, since there are some green checkmarks and some red checkmarks, we can see that it fails randomly, and the specific checkmarks that appear are randomized with every commit to the repository.

Action version: 4

Platform:

  • Ubuntu
  • macOS
  • Windows

Runner type:

  • Hosted
  • Self-hosted

Tools version: yarn v4.0.2

More info: I started seeing this error today, with no other related CI code changes, so the problem is likely to come from this action (or the GitHub Action VM) and not my actual code-base.

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Reactions: 34
  • Comments: 21 (1 by maintainers)

Commits related to this issue

Most upvoted comments

As @Mause mentioned, this can be temporarily solved by enabling Corepack before running setup-node.

  jobs:
    test:
      runs-on: ubuntu-latest
      steps:
        - uses: actions/checkout@v4
+       - name: Enable Corepack before setting up Node
+         run: corepack enable
        - uses: actions/setup-node@v4
          with:
            node-version: 20
        - run: npm ci
        - run: npm test

As @Mause mentioned, this can be temporarily solved by enabling Corepack before running setup-node.

  jobs:
    test:
      runs-on: ubuntu-latest
      steps:
        - uses: actions/checkout@v4
+       - name: Enable Corepack before setting up Node
+         run: corepack enable
        - uses: actions/setup-node@v4
          with:
            node-version: 20
        - run: npm ci
        - run: npm test

This is not ideal, because actions/checkout it designed for carrying out git commands, so you can’t be sure that node is present, or that it has the correct version you are specifying in the next step with Node 16 at least required for corepack. Instead I have managed to workaround this issue by running two steps:

      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: corepack enable
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: yarn
      - run: yarn install --immutable

This will setup Node to version 20, run corepack enable, then setup the cache

I was stuck with the same problem. However, putting the Yarn binary in Git management with the following command helped me:

yarn set version latest --yarn-path

@felladrin Yea, that’s better and actually caches my Yarn deps. Setting SKIP_YARN_COREPACK_CHECK=1 and running corepack enable after setup-node was not caching anything.

How do you enable corepack before installing node, since corepack is part of node?

I believe the action runner image contains a copy of node, as most actions are themselves written in JavaScript (including this one I think?)

Committing the Yarn binary to the repo is a good workaround for now, thanks. However, note that doing this is discouraged in Yarn version 4, as the ecosystem should use corepack going forward. So I would love a solution to this setup-node issue sooner rather than later.

If you don’t want to commit the Yarn version you can set SKIP_YARN_COREPACK_CHECK=1 to skip this check. Then later in steps run corepack enable. don’t do this, setup-node wont cache your deps.

Hello @Zamiell , According to my analysis, it seems that the discrepancy between Yarn versions (1.22.19 vs 1.22.21) in different jobs is causing the issue you’re encountering. As per the release notes for Yarn version 1.22.20, a notice will be displayed when Yarn 1.22 identifies a package.json file in the project that references a non-1.x Yarn release using the packageManager field. In this situation, the Yarn version in runner images was updated from v1.22.19 to v1.22.21 on November 16, which potentially led to the display of this error message afterwards. 
At the present moment, the repository doesn’t provide functionality to compare Yarn versions between the global scope and the one specified in the package.json. This feature could be incorporated for future enhancements to the repository. 
 I am going to proceed with closing it. I appreciate your understanding and patience. Many thanks!

We have investigated this issue and found that the action does not upgrade npm,yarn,pnpm.

If you have investigated the issue, can you explain why the error only happens sometimes, and not other times? Naively, I would expect that to indicate some kind of bug in either this GitHub action or the GitHub runner virtual machine.

Furthermore, Yarn 4 was released on October 22nd, 2023, and this GitHub Actions issue started occurring on November 17th, 2023. Which means that Yarn 4 worked just fine with this action for 26 days. Thus, I find your classification of the race condition described in the OP as a “feature request” rather than an “bug” completely baffling. Can you explain further why it is a “feature request” to request that something that already worked continues to work in the same way?

Hey @Zamiell , as per my previous comment, we will consider this issue as an enhancement in future. Feel free to share your inputs !!!

Hello @Zamiell , just a gentle ping!

Can confirm @kurone-kito solution works as a workaround for this 🎉