Skip to content

Commit c60a57c

Browse files
authored
Fix libc detection on various platforms (#6840)
## What's the problem this PR addresses? Two problems: 1. #6170 broke `libc` detection on at least Alpine Linux and Chimera Linux (and probably on all other `musl`-based distributions as well) by looking for the generic string `libc` in `/usr/bin/ldd`. On both Chimera and Alpine, `ldd --version` prints `musl libc` among the output. 2. `getLibc()` was doing unnecessary work on OSes other than Windows, Mac, and Linux (I originally thought it would have been crashing, but that's incorrect). ~~It strongly appears to me that `getLibc()` would throw an error on any OS other than Windows, Mac, and Linux, as the other OSes supported by node have their own libc implementations that are neither `glibc` nor `musl`.~~ ... ## How did you fix it? 1. The strings `GNU libc` and `GNU C Library` are searched for instead of just `libc`. * By default, `ldd` contains the text `GNU libc` in its `--version` output, but that can be easily customized at build time using a build configuration option (see [here](https://github.com/bminor/glibc/blob/632d895f3e5d98162f77b9c3c1da4ec19968b671/elf/ldd.bash.in#L37) and [here](https://github.com/bminor/glibc/blob/632d895f3e5d98162f77b9c3c1da4ec19968b671/INSTALL#L273-L278)). As a fallback, the text `GNU C Library` is found in the header of the `ldd` script, and it will probably remain there as long as that file remains a script (as opposed to a compiled binary). 2. I made `getLibc()` immediately return `null` on all non-Linux platforms (not just Windows and macOS). ... ## Checklist <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent dec0f9c commit c60a57c

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

.yarn/versions/1f924e2c.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
releases:
2+
"@yarnpkg/core": patch
3+
"@yarnpkg/cli": patch
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-essentials"
10+
- "@yarnpkg/plugin-exec"
11+
- "@yarnpkg/plugin-file"
12+
- "@yarnpkg/plugin-git"
13+
- "@yarnpkg/plugin-github"
14+
- "@yarnpkg/plugin-http"
15+
- "@yarnpkg/plugin-init"
16+
- "@yarnpkg/plugin-interactive-tools"
17+
- "@yarnpkg/plugin-jsr"
18+
- "@yarnpkg/plugin-link"
19+
- "@yarnpkg/plugin-nm"
20+
- "@yarnpkg/plugin-npm"
21+
- "@yarnpkg/plugin-npm-cli"
22+
- "@yarnpkg/plugin-pack"
23+
- "@yarnpkg/plugin-patch"
24+
- "@yarnpkg/plugin-pnp"
25+
- "@yarnpkg/plugin-pnpm"
26+
- "@yarnpkg/plugin-stage"
27+
- "@yarnpkg/plugin-typescript"
28+
- "@yarnpkg/plugin-version"
29+
- "@yarnpkg/plugin-workspace-tools"
30+
- "@yarnpkg/builder"
31+
- "@yarnpkg/doctor"
32+
- "@yarnpkg/extensions"
33+
- "@yarnpkg/nm"
34+
- "@yarnpkg/pnpify"
35+
- "@yarnpkg/sdks"

packages/yarnpkg-core/sources/nodeUtils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ export const openUrl = typeof openUrlBinary !== `undefined`
2626
const LDD_PATH = `/usr/bin/ldd` as PortablePath;
2727

2828
function getLibc() {
29-
// Darwin and Windows have their own standard libraries, and the getReport() call is costly.
30-
// It also seems that Node randomly crashes with no output under some circumstances when running a getReport() on Windows.
31-
if (process.platform === `darwin` || process.platform === `win32`)
29+
// As of 2025, linux is the only possible process.platform value that does not imply the libc for Node's purposes.
30+
// Technically mingw32 (a way to build and run software using glibc on Windows) exists and even has a node.js port, but no one in
31+
// the broader node.js ecosystem seems to care about it. There have been issues in the past running getReport() on Windows.
32+
if (process.platform !== `linux`)
3233
return null;
3334

3435
let header: Buffer | undefined;
@@ -39,7 +40,7 @@ function getLibc() {
3940
// Since the getReport can be prohibitely expensive (it also queries DNS which, if misconfigured, can take a long time to timeout),
4041
// we first check if the ldd binary is glibc or musl, and only then run the getReport() if we can't determine the libc variant.
4142
if (typeof header !== `undefined`) {
42-
if (header && (header.includes(`GLIBC`) || header.includes(`libc`)))
43+
if (header && (header.includes(`GLIBC`) || header.includes(`GNU libc`) || header.includes(`GNU C Library`)))
4344
return `glibc`;
4445
if (header && header.includes(`musl`)) {
4546
return `musl`;

0 commit comments

Comments
 (0)