Skip to content

Commit

Permalink
fix(env): throw an error on a system that uses MUSL libc (#4958)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Jun 30, 2022
1 parent a0a1b77 commit eccac31
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/strong-ways-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/node.fetcher": patch
"pnpm": patch
---

`pnpm env use` should throw an error on a system that use the MUSL libc.
2 changes: 2 additions & 0 deletions packages/node.fetcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
},
"homepage": "https://github.com/pnpm/pnpm/blob/main/packages/node.fetcher#readme",
"dependencies": {
"@pnpm/error": "workspace:2.1.0",
"@pnpm/fetcher-base": "workspace:11.2.0",
"@pnpm/fetching-types": "workspace:2.2.1",
"@pnpm/package-store": "workspace:12.2.0",
"@pnpm/tarball-fetcher": "workspace:9.3.19",
"adm-zip": "^0.5.5",
"detect-libc": "^2.0.1",
"rename-overwrite": "^4.0.2",
"tempy": "^1.0.1"
},
Expand Down
5 changes: 5 additions & 0 deletions packages/node.fetcher/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import PnpmError from '@pnpm/error'
import {
FetchFromRegistry,
RetryTimeoutOptions,
Expand All @@ -10,6 +11,7 @@ import createFetcher, { waitForFilesIndex } from '@pnpm/tarball-fetcher'
import AdmZip from 'adm-zip'
import renameOverwrite from 'rename-overwrite'
import tempy from 'tempy'
import { isNonGlibcLinux } from 'detect-libc'
import { getNodeTarball } from './getNodeTarball'

export interface FetchNodeOptions {
Expand All @@ -20,6 +22,9 @@ export interface FetchNodeOptions {
}

export async function fetchNode (fetch: FetchFromRegistry, version: string, targetDir: string, opts: FetchNodeOptions) {
if (await isNonGlibcLinux()) {
throw new PnpmError('MUSL', 'The current system uses the "MUSL" C standard library. Node.js currently has prebuilt artifacts only for the "glibc" libc, so we can install Node.js only for glibc')
}
const nodeMirrorBaseUrl = opts.nodeMirrorBaseUrl ?? 'https://nodejs.org/download/release/'
const { tarball, pkgName } = getNodeTarball(version, nodeMirrorBaseUrl, process.platform, process.arch)
if (tarball.endsWith('.zip')) {
Expand Down
19 changes: 19 additions & 0 deletions packages/node.fetcher/test/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import path from 'path'
import { Readable } from 'stream'
import { fetchNode, FetchNodeOptions } from '@pnpm/node.fetcher'
import { tempDir } from '@pnpm/prepare'
import { isNonGlibcLinux } from 'detect-libc'

jest.mock('detect-libc', () => ({
isNonGlibcLinux: jest.fn(),
}))

const fetchMock = jest.fn(async (url: string) => {
if (url.endsWith('.zip')) {
Expand All @@ -20,6 +25,7 @@ const fetchMock = jest.fn(async (url: string) => {
})

beforeEach(() => {
isNonGlibcLinux['mockReturnValue'](Promise.resolve(false))
fetchMock.mockClear()
})

Expand Down Expand Up @@ -52,3 +58,16 @@ test('install Node using the default node mirror', async () => {
expect(call[0]).toMatch('https://nodejs.org/download/release/')
}
})

test('install Node using a custom node mirror', async () => {
isNonGlibcLinux['mockReturnValue'](Promise.resolve(true))
tempDir()

const opts: FetchNodeOptions = {
cafsDir: path.resolve('files'),
}

await expect(
fetchNode(fetchMock, '16.4.0', path.resolve('node'), opts)
).rejects.toThrow('The current system uses the "MUSL" C standard library. Node.js currently has prebuilt artifacts only for the "glibc" libc, so we can install Node.js only for glibc')
})
3 changes: 3 additions & 0 deletions packages/node.fetcher/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
{
"path": "../../privatePackages/prepare"
},
{
"path": "../error"
},
{
"path": "../fetcher-base"
},
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eccac31

Please sign in to comment.