Skip to content

Commit

Permalink
fix: node<16 download fail on arm chips on macos (#5239)
Browse files Browse the repository at this point in the history
* fix(node.fetcher): node < 16 download fail on apple silicon

Add a check for macos apple silicon and required node version < 16.
In such case, download x64 binary.
Because arm build does not exist.
Previous behaviour:
Try to download non existing arm build and fail with 404.

NO breaking change
fixes #4489

* refactor(node.fetcher): move code to normalizeArch
  • Loading branch information
ambar-arkin committed Aug 20, 2022
1 parent cccaa02 commit 1c7b439
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-ties-add.md
@@ -0,0 +1,5 @@
---
"@pnpm/node.fetcher": patch
---

For node version < 16, install x64 build on darwin arm as arm build is not available.
2 changes: 1 addition & 1 deletion packages/node.fetcher/src/getNodeTarball.ts
Expand Up @@ -7,7 +7,7 @@ export function getNodeTarball (
processArch: string
) {
const platform = processPlatform === 'win32' ? 'win' : processPlatform
const arch = normalizeArch(processPlatform, processArch)
const arch = normalizeArch(processPlatform, processArch, nodeVersion)
const extension = platform === 'win' ? 'zip' : 'tar.gz'
const pkgName = `node-v${nodeVersion}-${platform}-${arch}`
return {
Expand Down
8 changes: 7 additions & 1 deletion packages/node.fetcher/src/normalizeArch.ts
@@ -1,4 +1,10 @@
export default function getNormalizedArch (platform: string, arch: string) {
export default function getNormalizedArch (platform: string, arch: string, nodeVersion?: string) {
if (nodeVersion) {
const nodeMajorVersion = +nodeVersion.split('.')[0]
if ((platform === 'darwin' && arch === 'arm64' && (nodeMajorVersion < 16))) {
return 'x64'
}
}
if (platform === 'win32' && arch === 'ia32') {
return 'x86'
}
Expand Down
20 changes: 20 additions & 0 deletions packages/node.fetcher/test/getNodeTarball.test.ts
Expand Up @@ -31,6 +31,26 @@ test.each([
tarball: 'https://nodejs.org/download/release/v16.0.0/node-v16.0.0-linux-x64.tar.gz',
},
],
[
'15.14.0',
'https://nodejs.org/download/release/',
'darwin',
'arm64',
{
pkgName: 'node-v15.14.0-darwin-x64',
tarball: 'https://nodejs.org/download/release/v15.14.0/node-v15.14.0-darwin-x64.tar.gz',
},
],
[
'16.0.0',
'https://nodejs.org/download/release/',
'darwin',
'arm64',
{
pkgName: 'node-v16.0.0-darwin-arm64',
tarball: 'https://nodejs.org/download/release/v16.0.0/node-v16.0.0-darwin-arm64.tar.gz',
},
],
])('getNodeTarball', (version, nodeMirrorBaseUrl, platform, arch, tarball) => {
expect(getNodeTarball(version, nodeMirrorBaseUrl, platform, arch)).toStrictEqual(tarball)
})
8 changes: 8 additions & 0 deletions packages/node.fetcher/test/normalizeArch.test.ts
Expand Up @@ -7,3 +7,11 @@ test.each([
])('normalizedArch(%s, %s)', (platform, arch, normalizedArch) => {
expect(normalizeArch(platform, arch)).toBe(normalizedArch)
})

// macos apple silicon
test.each([
['darwin', 'arm64', '14.20.0', 'x64'],
['darwin', 'arm64', '16.17.0', 'arm64'],
])('normalizedArch(%s, %s)', (platform, arch, nodeVersion, normalizedArch) => {
expect(normalizeArch(platform, arch, nodeVersion)).toBe(normalizedArch)
})

0 comments on commit 1c7b439

Please sign in to comment.