Skip to content

Commit

Permalink
Adds support for the libc field in package.json (#3981)
Browse files Browse the repository at this point in the history
* Adds support for the libc field in package.json

* Updates tests

* Fixes tests and adds more patterns

* Adds comment

* Update pnp.test.js

* Update pnp.test.js

* Fixes race condition when spawning server twice

* Avoids calling getReport on Windows

* Missing comment part
  • Loading branch information
arcanis authored Jan 20, 2022
1 parent ecfeca0 commit e64af83
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 23 deletions.
33 changes: 33 additions & 0 deletions .yarn/versions/7f7b169a.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
releases:
"@yarnpkg/cli": minor
"@yarnpkg/core": minor
"@yarnpkg/plugin-pnp": minor

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
11 changes: 5 additions & 6 deletions packages/acceptance-tests/pkg-tests-core/sources/utils/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,14 @@ export const getPackageDirectoryPath = async (
};

const packageServerUrls: {
http: string | null;
https: string | null;
http: Promise<string> | null;
https: Promise<string> | null;
} = {http: null, https: null};

export const startPackageServer = ({type}: { type: keyof typeof packageServerUrls } = {type: `http`}): Promise<string> => {
const serverUrl = packageServerUrls[type];

if (serverUrl !== null)
return Promise.resolve(serverUrl);
return serverUrl;

const applyOtpValidation = (req: IncomingMessage, res: ServerResponse, user: Login) => {
const otp = req.headers[`npm-otp`];
Expand Down Expand Up @@ -536,7 +535,7 @@ export const startPackageServer = ({type}: { type: keyof typeof packageServerUrl
validAuthorizations.set(`Basic ${user.npmAuthIdent.encoded}`, user);
}

return new Promise((resolve, reject) => {
return packageServerUrls[type] = new Promise((resolve, reject) => {
const listener: http.RequestListener = (req, res) =>
void (async () => {
try {
Expand Down Expand Up @@ -595,7 +594,7 @@ export const startPackageServer = ({type}: { type: keyof typeof packageServerUrl
server.unref();
server.listen(() => {
const {port} = server.address() as AddressInfo;
resolve((packageServerUrls[type] = `${type}://localhost:${port}`));
resolve(`${type}://localhost:${port}`);
});
})();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* @flow */

module.exports = require(`./package.json`);

for (const key of [`dependencies`, `devDependencies`, `peerDependencies`]) {
for (const dep of Object.keys(module.exports[key] || {})) {
// $FlowFixMe The whole point of this file is to be dynamic
module.exports[key][dep] = require(dep);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "native-libc-glibc",
"version": "1.0.0",
"libc": ["glibc"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* @flow */

module.exports = require(`./package.json`);

for (const key of [`dependencies`, `devDependencies`, `peerDependencies`]) {
for (const dep of Object.keys(module.exports[key] || {})) {
// $FlowFixMe The whole point of this file is to be dynamic
module.exports[key][dep] = require(dep);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "native-libc-musl",
"version": "1.0.0",
"libc": ["musl"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"optionalDependencies": {
"native-bar-x64": "1.0.0",
"native-foo-x64": "1.0.0",
"native-foo-x86": "1.0.0"
"native-foo-x86": "1.0.0",
"native-libc-glibc": "1.0.0",
"native-libc-musl": "1.0.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,41 @@ __metadata:
languageName: node
linkType: hard
\\"native-libc-glibc@npm:1.0.0\\":
version: 1.0.0
resolution: \\"native-libc-glibc@npm:1.0.0\\"
conditions: libc=glibc
languageName: node
linkType: hard
\\"native-libc-musl@npm:1.0.0\\":
version: 1.0.0
resolution: \\"native-libc-musl@npm:1.0.0\\"
conditions: libc=musl
languageName: node
linkType: hard
\\"optional-native@npm:1.0.0\\":
version: 1.0.0
resolution: \\"optional-native@npm:1.0.0\\"
dependencies:
native-bar-x64: 1.0.0
native-foo-x64: 1.0.0
native-foo-x86: 1.0.0
native-libc-glibc: 1.0.0
native-libc-musl: 1.0.0
dependenciesMeta:
native-bar-x64:
optional: true
native-foo-x64:
optional: true
native-foo-x86:
optional: true
checksum: 0e662ccf2f901c37aa95cc3e8bee87315782ca27582521644c86e1ee44367a31a0201049bad23109ca9783efc9ac70bc7dae8619c31f2b6472830b7cf1670f0d
native-libc-glibc:
optional: true
native-libc-musl:
optional: true
checksum: b62ce5449e2631a9bc4d264ca9cd32cdb96465d6d739cca3378a4e48cf2db199e01c83e97cccee2f657709e5b79403b0cd4dcbc3aa6a96608c11b17b72df3e2c
languageName: node
linkType: hard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe(`Features`, () => {
supportedArchitectures: {
cpu: [`foo`],
os: [`x64`],
libc: [`glibc`],
},
});

Expand All @@ -31,6 +32,7 @@ describe(`Features`, () => {
supportedArchitectures: {
os: [`foo`],
cpu: [`x64`],
libc: [`glibc`],
},
});

Expand All @@ -50,6 +52,10 @@ describe(`Features`, () => {
type: RequestType.PackageTarball,
localName: `native-foo-x64`,
version: `1.0.0`,
}, {
type: RequestType.PackageTarball,
localName: `native-libc-glibc`,
version: `1.0.0`,
}, {
type: RequestType.PackageTarball,
localName: `optional-native`,
Expand All @@ -66,6 +72,7 @@ describe(`Features`, () => {
supportedArchitectures: {
os: [`foo`],
cpu: [`x64`, `x86`],
libc: [`glibc`, `musl`],
},
});

Expand All @@ -89,6 +96,14 @@ describe(`Features`, () => {
type: RequestType.PackageTarball,
localName: `native-foo-x86`,
version: `1.0.0`,
}, {
type: RequestType.PackageTarball,
localName: `native-libc-glibc`,
version: `1.0.0`,
}, {
type: RequestType.PackageTarball,
localName: `native-libc-musl`,
version: `1.0.0`,
}, {
type: RequestType.PackageTarball,
localName: `optional-native`,
Expand All @@ -105,6 +120,7 @@ describe(`Features`, () => {
supportedArchitectures: {
os: [`foo`],
cpu: [`x64`],
libc: [`glibc`],
},
});

Expand All @@ -113,8 +129,9 @@ describe(`Features`, () => {

await xfs.writeJsonPromise(ppath.join(path, Filename.rc), {
supportedArchitectures: {
os: [`foo`],
os: [`bar`],
cpu: [`x86`],
libc: [`musl`],
},
});

Expand All @@ -133,6 +150,7 @@ describe(`Features`, () => {
supportedArchitectures: {
os: [`foo`],
cpu: [`x64`],
libc: [`glibc`],
},
});

Expand All @@ -141,8 +159,9 @@ describe(`Features`, () => {

await xfs.writeJsonPromise(ppath.join(path, Filename.rc), {
supportedArchitectures: {
os: [`foo`],
os: [`bar`],
cpu: [`x86`],
libc: [`musl`],
},
});

Expand All @@ -161,6 +180,7 @@ describe(`Features`, () => {
supportedArchitectures: {
os: [`foo`],
cpu: [`x64`],
libc: [`glibc`],
},
});

Expand All @@ -185,6 +205,7 @@ describe(`Features`, () => {
supportedArchitectures: {
os: [`foo`],
cpu: [`x64`],
libc: [`glibc`],
},
});

Expand Down Expand Up @@ -213,6 +234,7 @@ describe(`Features`, () => {
supportedArchitectures: {
os: [`foo`],
cpu: [`x64`, `x86`],
libc: [`glibc`],
},
});

Expand All @@ -231,6 +253,7 @@ describe(`Features`, () => {
supportedArchitectures: {
os: [`foo`],
cpu: [`x86`],
libc: [`glibc`],
},
});

Expand All @@ -248,6 +271,7 @@ describe(`Features`, () => {
supportedArchitectures: {
os: [`foo`],
cpu: [`x64`, `x86`],
libc: [`glibc`],
},
});

Expand All @@ -257,6 +281,7 @@ describe(`Features`, () => {
supportedArchitectures: {
os: [`foo`],
cpu: [`x64`],
libc: [`glibc`],
},
});

Expand All @@ -280,6 +305,10 @@ describe(`Features`, () => {
type: RequestType.PackageTarball,
localName: `native-foo-x86`,
version: `1.0.0`,
}, {
type: RequestType.PackageTarball,
localName: `native-libc-glibc`,
version: `1.0.0`,
}, {
type: RequestType.PackageTarball,
localName: `optional-native`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,7 @@ describe(`Plug'n'Play`, () => {
const stdout = (await run(`install`)).stdout;

expect(stdout).not.toContain(`Shall not be run`);
expect(stdout).toMatch(new RegExp(`dep@file:./dep.*The ${process.platform}-${process.arch} architecture is incompatible with this module, build skipped.`));
expect(stdout).toMatch(new RegExp(`dep@file:./dep.*The ${process.platform}-${process.arch}(-[a-z]+)? architecture is incompatible with this package, build skipped.`));

await expect(source(`require('dep')`)).resolves.toMatchObject({
name: `dep`,
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby/static/configuration/yarnrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,15 @@
},
"default": [],
"_exampleItems": ["current", "x86", "ia32"]
},
"libc": {
"description": "The list of standard libraries to cover.",
"type": "array",
"items": {
"type": "string"
},
"default": [],
"_exampleItems": ["current", "glibc", "musl"]
}
},
"_exampleKeys": ["os", "cpu"]
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-pnp/sources/jsInstallUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {BuildDirective, BuildType, Configuration, DependencyMeta, FetchResult, LinkType, Manifest, MessageName, Package, Report, structUtils} from '@yarnpkg/core';
import {Filename, ppath} from '@yarnpkg/fslib';
import {BuildDirective, BuildType, Configuration, DependencyMeta, FetchResult, LinkType, Manifest, MessageName, Package, Report, nodeUtils, structUtils} from '@yarnpkg/core';
import {Filename, ppath} from '@yarnpkg/fslib';

export function checkManifestCompatibility(pkg: Package) {
return structUtils.isPackageCompatible(pkg, {os: [process.platform], cpu: [process.arch]});
return structUtils.isPackageCompatible(pkg, nodeUtils.getArchitectureSet());
}

export function checkAndReportManifestCompatibility(pkg: Package, label: string, {configuration, report}: {configuration: Configuration, report?: Report | null}) {
if (!checkManifestCompatibility(pkg)) {
report?.reportWarningOnce(MessageName.INCOMPATIBLE_ARCHITECTURE, `${structUtils.prettyLocator(configuration, pkg)} The ${process.platform}-${process.arch} architecture is incompatible with this module, ${label} skipped.`);
report?.reportWarningOnce(MessageName.INCOMPATIBLE_ARCHITECTURE, `${structUtils.prettyLocator(configuration, pkg)} The ${nodeUtils.getArchitectureName()} architecture is incompatible with this package, ${label} skipped.`);
return false;
}

Expand Down
21 changes: 17 additions & 4 deletions packages/yarnpkg-core/sources/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export enum SettingsType {
export type SupportedArchitectures = {
os: Array<string> | null;
cpu: Array<string> | null;
libc: Array<string> | null;
};

export type FormatType = formatUtils.Type;
Expand Down Expand Up @@ -307,6 +308,13 @@ export const coreDefinitions: {[coreSettingName: string]: SettingsDefinition} =
isNullable: true,
default: [`current`],
},
libc: {
description: `Array of supported libc libraries, or null to target them all`,
type: SettingsType.STRING,
isArray: true,
isNullable: true,
default: [`current`],
},
},
},

Expand Down Expand Up @@ -1469,18 +1477,23 @@ export class Configuration {
return linkers;
}

getSupportedArchitectures() {
getSupportedArchitectures(): nodeUtils.ArchitectureSet {
const architecture = nodeUtils.getArchitecture();
const supportedArchitectures = this.get(`supportedArchitectures`);

let os = supportedArchitectures.get(`os`);
if (os !== null)
os = os.map(value => value === `current` ? process.platform : value);
os = os.map(value => value === `current` ? architecture.os : value);

let cpu = supportedArchitectures.get(`cpu`);
if (cpu !== null)
cpu = cpu.map(value => value === `current` ? process.arch : value);
cpu = cpu.map(value => value === `current` ? architecture.cpu : value);

let libc = supportedArchitectures.get(`libc`);
if (libc !== null)
libc = miscUtils.mapAndFilter(libc, value => value === `current` ? architecture.libc ?? miscUtils.mapAndFilter.skip : value);

return {os, cpu};
return {os, cpu, libc};
}

async refreshPackageExtensions() {
Expand Down
Loading

0 comments on commit e64af83

Please sign in to comment.