Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for --ignore-cpu in yarn install #8381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
@@ -1 +1 @@
Please see https://yarnpkg.com/org/contributing/
Please see https://classic.yarnpkg.com/org/contributing/
46 changes: 34 additions & 12 deletions __tests__/package-compatibility.js
Expand Up @@ -20,13 +20,28 @@ test('ignore semver prerelease semantics for yarn', () => {
expect(testEngine('yarn', '^1.3.0', {yarn: '1.4.1-20180208.2355'}, true)).toEqual(true);
});

test('shouldCheck returns false if the manifest does not specify any requirements', () => {
expect(shouldCheck({}, {ignorePlatform: false, ignoreEngines: false, ignoreCpu: false})).toBe(false);

expect(
shouldCheck(
{
os: [],
cpu: [],
engines: {},
},
{ignorePlatform: false, ignoreEngines: false, ignoreCpu: false},
),
).toBe(false);
});

test('shouldCheck returns true if ignorePlatform is false and the manifest specifies an os or cpu requirement', () => {
expect(
shouldCheck(
{
os: ['darwin'],
},
{ignorePlatform: false, ignoreEngines: false},
{ignorePlatform: false, ignoreEngines: false, ignoreCpu: false},
),
).toBe(true);

Expand All @@ -35,29 +50,38 @@ test('shouldCheck returns true if ignorePlatform is false and the manifest speci
{
cpu: ['i32'],
},
{ignorePlatform: false, ignoreEngines: false},
{ignorePlatform: false, ignoreEngines: false, ignoreCpu: false},
),
).toBe(true);
});

expect(shouldCheck({}, {ignorePlatform: false, ignoreEngines: false})).toBe(false);
test('shouldCheck returns false if the manifest specifies an os or cpu requirement but ignorePlatform is true', () => {
expect(
shouldCheck(
{
os: ['darwin'],
},
{ignorePlatform: true, ignoreEngines: false, ignoreCpu: false},
),
).toBe(false);

expect(
shouldCheck(
{
os: [],
cpu: [],
cpu: ['i32'],
},
{ignorePlatform: false, ignoreEngines: false},
{ignorePlatform: true, ignoreEngines: false, ignoreCpu: false},
),
).toBe(false);
});

test('shouldCheck returns false if the manifest specifies a cpu requirement but ignoreCpu is true', () => {
expect(
shouldCheck(
{
cpu: ['i32'],
os: ['darwin'],
},
{ignorePlatform: true, ignoreEngines: false},
{ignorePlatform: false, ignoreEngines: false, ignoreCpu: true},
),
).toBe(false);
});
Expand All @@ -68,18 +92,16 @@ test('shouldCheck returns true if ignoreEngines is false and the manifest specif
{
engines: {node: '>= 10'},
},
{ignorePlatform: false, ignoreEngines: false},
{ignorePlatform: false, ignoreEngines: false, ignoreCpu: false},
),
).toBe(true);

expect(shouldCheck({}, {ignorePlatform: false, ignoreEngines: false})).toBe(false);

expect(
shouldCheck(
{
engines: {node: '>= 10'},
},
{ignorePlatform: false, ignoreEngines: true},
{ignorePlatform: false, ignoreEngines: true, ignoreCpu: false},
),
).toBe(false);
});
26 changes: 25 additions & 1 deletion packages/pkg-tests/pkg-tests-specs/sources/basic.js
Expand Up @@ -405,6 +405,30 @@ module.exports = (makeTemporaryEnv: PackageDriver) => {
),
);

test(
`it should not fail if the environment does not satisfy the cpu architecture but ignore cpu is true`,
makeTemporaryEnv(
{
cpu: ['unicorn'],
},
async ({path, run, source}) => {
await run(`install`, '--ignore-cpu');
},
),
);

test(
`it should not fail if the environment does not satisfy the cpu architecture but ignore platform is true`,
makeTemporaryEnv(
{
cpu: ['unicorn'],
},
async ({path, run, source}) => {
await run(`install`, '--ignore-platform');
},
),
);

test(
`it should fail if the environment does not satisfy the engine requirements`,
makeTemporaryEnv(
Expand All @@ -420,7 +444,7 @@ module.exports = (makeTemporaryEnv: PackageDriver) => {
);

test(
`it should not fail if the environment does not satisfy the os and cpu architecture but ignore platform is true`,
`it should not fail if the environment does not satisfy the os platform but ignore platform is true`,
makeTemporaryEnv(
{
os: ['unicorn'],
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/config.js
Expand Up @@ -20,6 +20,7 @@ const CONFIG_KEYS = [
'linkFolder',
'offline',
'binLinks',
'ignoreCpu',
'ignorePlatform',
'ignoreScripts',
'disablePrepublish',
Expand Down
6 changes: 6 additions & 0 deletions src/cli/commands/install.js
Expand Up @@ -55,6 +55,7 @@ export type InstallCwdRequest = {
type Flags = {
// install
har: boolean,
ignoreCpu: boolean,
ignorePlatform: boolean,
ignoreEngines: boolean,
ignoreOptional: boolean,
Expand Down Expand Up @@ -136,6 +137,7 @@ function normalizeFlags(config: Config, rawFlags: Object): Flags {
const flags = {
// install
har: !!rawFlags.har,
ignoreCpu: !!rawFlags.ignoreCpu,
ignorePlatform: !!rawFlags.ignorePlatform,
ignoreEngines: !!rawFlags.ignoreEngines,
ignoreScripts: !!rawFlags.ignoreScripts,
Expand Down Expand Up @@ -170,6 +172,10 @@ function normalizeFlags(config: Config, rawFlags: Object): Flags {
flags.ignoreScripts = true;
}

if (config.getOption('ignore-cpu')) {
flags.ignoreCpu = true;
}

if (config.getOption('ignore-platform')) {
flags.ignorePlatform = true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/cli/index.js
Expand Up @@ -86,7 +86,8 @@ export async function main({
commander.option('--json', 'format Yarn log messages as lines of JSON (see jsonlines.org)');
commander.option('--ignore-scripts', "don't run lifecycle scripts");
commander.option('--har', 'save HAR output of network traffic');
commander.option('--ignore-platform', 'ignore platform checks');
commander.option('--ignore-cpu', 'ignore CPU check');
commander.option('--ignore-platform', 'ignore platform ("os" and "cpu") checks');
commander.option('--ignore-engines', 'ignore engines check');
commander.option('--ignore-optional', 'ignore optional dependencies');
commander.option('--force', 'install and build packages even if they were built before, overwrite lockfile');
Expand Down Expand Up @@ -539,6 +540,7 @@ export async function main({
binLinks: commander.binLinks,
preferOffline: commander.preferOffline,
captureHar: commander.har,
ignoreCpu: commander.ignoreCpu,
ignorePlatform: commander.ignorePlatform,
ignoreEngines: commander.ignoreEngines,
ignoreScripts: commander.ignoreScripts,
Expand Down
3 changes: 3 additions & 0 deletions src/config.js
Expand Up @@ -38,6 +38,7 @@ export type ConfigOptions = {
enableMetaFolder?: boolean,
linkFileDependencies?: boolean,
captureHar?: boolean,
ignoreCpu?: boolean,
ignoreScripts?: boolean,
ignorePlatform?: boolean,
ignoreEngines?: boolean,
Expand Down Expand Up @@ -117,6 +118,7 @@ export default class Config {
enableMetaFolder: boolean;
enableLockfileVersions: boolean;
linkFileDependencies: boolean;
ignoreCpu: boolean;
ignorePlatform: boolean;
binLinks: boolean;
updateChecksums: boolean;
Expand Down Expand Up @@ -474,6 +476,7 @@ export default class Config {
this.plugnplayUnplugged = [];
this.plugnplayPurgeUnpluggedPackages = false;

this.ignoreCpu = !!opts.ignoreCpu;
this.ignorePlatform = !!opts.ignorePlatform;
this.ignoreScripts = !!opts.ignoreScripts;

Expand Down
16 changes: 10 additions & 6 deletions src/package-compatibility.js
Expand Up @@ -133,7 +133,7 @@ export function checkOne(info: Manifest, config: Config, ignoreEngines: boolean)
pushError(reporter.lang('incompatibleOS', process.platform));
}

if (shouldCheckCpu(cpu, config.ignorePlatform) && !isValidArch(cpu)) {
if (shouldCheckCpu(cpu, config.ignorePlatform, config.ignoreCpu) && !isValidArch(cpu)) {
pushError(reporter.lang('incompatibleCPU', process.arch));
}

Expand Down Expand Up @@ -167,24 +167,28 @@ export function check(infos: Array<Manifest>, config: Config, ignoreEngines: boo
}
}

function shouldCheckCpu(cpu: $PropertyType<Manifest, 'cpu'>, ignorePlatform: boolean): boolean %checks {
return !ignorePlatform && Array.isArray(cpu) && cpu.length > 0;
function shouldCheckCpu(
cpu: $PropertyType<Manifest, 'cpu'>,
ignorePlatform: boolean,
ignoreCpu: boolean,
): boolean %checks {
return !(ignorePlatform || ignoreCpu) && Array.isArray(cpu) && cpu.length > 0;
}

function shouldCheckPlatform(os: $PropertyType<Manifest, 'os'>, ignorePlatform: boolean): boolean %checks {
return !ignorePlatform && Array.isArray(os) && os.length > 0;
}

function shouldCheckEngines(engines: $PropertyType<Manifest, 'engines'>, ignoreEngines: boolean): boolean %checks {
return !ignoreEngines && typeof engines === 'object';
return !ignoreEngines && typeof engines === 'object' && Object.keys(engines).length > 0;
}

export function shouldCheck(
manifest: PartialManifest,
options: {ignoreEngines: boolean, ignorePlatform: boolean},
options: {ignoreEngines: boolean, ignorePlatform: boolean, ignoreCpu: boolean},
): boolean {
return (
shouldCheckCpu(manifest.cpu, options.ignorePlatform) ||
shouldCheckCpu(manifest.cpu, options.ignorePlatform, options.ignoreCpu) ||
shouldCheckPlatform(manifest.os, options.ignorePlatform) ||
shouldCheckEngines(manifest.engines, options.ignoreEngines)
);
Expand Down