Skip to content

Commit

Permalink
🐛 FIX: Use detect-libc to get libc family (#388)
Browse files Browse the repository at this point in the history
Ignore libc check if libc family is null
  • Loading branch information
fengmk2 committed Mar 20, 2022
1 parent fcfe984 commit d58a37b
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 25 deletions.
6 changes: 3 additions & 3 deletions lib/download/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const moment = require('moment');
const os = require('os');
const semver = require('semver');
const utility = require('utility');
const { family: getLibcFamily } = require('detect-libc');
const get = require('../get');
const utils = require('../utils');
const config = require('../cnpm_config');
Expand Down Expand Up @@ -248,9 +249,8 @@ async function download(pkg, options) {
}
// dont download pkg in not matched libc
if (Array.isArray(pkg.libc)) {
const currentLibc = utils.getLibc();
debug('currentLibc', currentLibc, pkg.libc);
if (!utils.matchPlatform(currentLibc, pkg.libc)) {
const currentLibc = await getLibcFamily();
if (currentLibc && !utils.matchPlatform(currentLibc, pkg.libc)) {
const errMsg = `[${pkg.name}@${pkg.version}] skip download for reason ${pkg.libc.join(', ')} dont includes your libc ${currentLibc}`;
const err = new Error(errMsg);
err.name = 'UnSupportedPlatformError';
Expand Down
2 changes: 1 addition & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function install(parentDir, pkg, ancestors, options, context) {
if (pkg.optional) {
if (err.name === 'UnSupportedPlatformError') {
// ignore log error
debug(err);
debug('%s', err.message);
return;
}
options.console.error(chalk.yellow(`[${pkg.name}@${pkg.version}] optional install error: ${err.stack}`));
Expand Down
19 changes: 0 additions & 19 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,25 +463,6 @@ exports.getBugVersions = async (registry, globalOptions) => {
return pkg.config['bug-versions'];
};

// detect libc
// follow yarn impl: https://github.com/yarnpkg/berry/pull/3981/files#diff-f9ba955128e960171ac97ce06b0b0a940bb074af2794429a9f03c75f2a3bd459R10
exports.getLibc = () => {
if (process.platform === 'win32') return null;
const report = process.report && process.report.getReport() || {};
const sharedObjects = report.sharedObjects || [];

// Matches the first group if libc, second group if musl
const libcRE = /\/(?:(ld-linux-|[^/]+-linux-gnu\/)|(libc.musl-|ld-musl-))/i;
for (const sharedObject of sharedObjects) {
const m = sharedObject.match(libcRE);
if (m) {
if (m[1]) return 'libc';
if (m[2]) return 'musl';
}
}
return null;
};

// match platform, arch or libc
// see https://docs.npmjs.com/cli/v7/configuring-npm/package-json#os
exports.matchPlatform = (current, osNames) => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"cmd-shim-hotfix": "^3.0.3",
"debug": "^4.1.1",
"destroy": "^1.0.4",
"detect-libc": "^2.0.1",
"fs-extra": "^7.0.1",
"minimatch": "^3.0.4",
"minimist": "^1.2.0",
Expand Down
4 changes: 2 additions & 2 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ describe('test/utils.test.js', () => {
assert(utils.matchPlatform('glibc', []));
assert(utils.matchPlatform('musl', []));
assert(utils.matchPlatform(null, []));
assert(utils.matchPlatform(null, [ 'musl' ]));
assert(utils.matchPlatform(null, [ 'glibc' ]));
assert(utils.matchPlatform('glibc', [ 'glibc' ]));
assert(utils.matchPlatform('glibc', [ 'glibc', 'musl' ]));
assert(utils.matchPlatform('musl', [ 'glibc', 'musl' ]));
Expand Down Expand Up @@ -69,8 +71,6 @@ describe('test/utils.test.js', () => {
});

it('should not match libc names', () => {
assert(!utils.matchPlatform(null, [ 'musl' ]));
assert(!utils.matchPlatform(null, [ 'glibc' ]));
assert(!utils.matchPlatform('glibc', [ 'musl' ]));
assert(!utils.matchPlatform('musl', [ 'glibc' ]));
assert(!utils.matchPlatform('glibc', [ '!glibc' ]));
Expand Down

0 comments on commit d58a37b

Please sign in to comment.