Closed jessecollier closed 1 month ago
On my end, this problem appeared while we were using Node 18.x, and upgrade to Node 20.x fixed the problem E.g. by adding this into the GHA Workflow:
- name: Set Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: 20.x
This is further apparent from how this PR was tested, because the build pipeline uses the same: https://github.com/actions/toolkit/actions/runs/10707564967/workflow#L27-L30
@jessecollier which version of Node are you using for that action?
Both Node 16 actions and Node 20 should work with this function.
https://nodejs.org/docs/latest-v16.x/api/crypto.html#cryptorandomuuidoptions
We are seeing the same problem. Using node v18.20.4, and image version 20240922.1.0.
Here's the failing job: https://github.com/github/codeql-action/actions/runs/11152388670/job/31043230806?pr=2518
Interestingly, when I run this locally using the same version of node, the tests pass (and crypto
is indeed available in globalThis
). Is it possible that the default node version in the action image is using a version of node that was not compiled with the crypto class?
According to the node docs:
It is possible for Node.js to be built without including support for the node:crypto module. In such cases, attempting to import from crypto or calling require('node:crypto') will result in an error being thrown.
I will look into reverting the addition of crypto
, need to investigate a bit more.
In the meantime, I would recommend pinning to an earlier version of @actions/core
.
Is it possible that the default node version in the action image is using a version of node that was not compiled with the crypto class?
Prior to Node 19, crypto
needs an explicit require
(or import
given it's TypeScript), which seems to be what's missing here.
$ /opt/homebrew/opt/node@20/bin/node - <<<"console.log(crypto.randomUUID())"
df14bf83-0951-4296-a0a9-44f57230e56d
$ /opt/homebrew/opt/node@18/bin/node - <<<"console.log(crypto.randomUUID())"
[stdin]:1
console.log(crypto.randomUUID())
^
ReferenceError: crypto is not defined
$ /opt/homebrew/opt/node@18/bin/node - <<<"const crypto = require('crypto'); console.log(crypto.randomUUID())"
477f9e75-369a-4a88-b7ee-6f1ab9bcbeba
$ /opt/homebrew/opt/node@20/bin/node - <<<"const crypto = require('crypto'); console.log(crypto.randomUUID())"
7e78649d-c0bb-4d28-b474-3700c6ce04a1
(not using -e
here as that does some auto-require magic)
Yea we're using node 18. Pinning to the previous version of the package resolved the issue for us.
Thanks for the info @Bo98!
Confirmed that crypto
works as expected when explicitly required/imported in Node 16, 18, 20, and the default Node version on ubuntu-latest
(currently Node 18).
https://nodejs.org/en/blog/announcements/v19-release-announce#stable-webcrypto
This should be fixed in 1.11.1
Marking as resolved! Thanks for reporting this and thanks again to @Bo98 for helping find the solution.
Feel free to reopen or file a new issue if you're still seeing this problem.
Describe the bug We have a github action that runs in the following environment:
To Reproduce Steps to reproduce the behavior:
const core = require('@actions/core');
Expected behavior Should not throw an error
Additional context Add any other context about the problem here.