Skip to content

Commit 546c71c

Browse files
authored
Reject incompatible Community CLI versions (#59801)
1 parent d512793 commit 546c71c

2 files changed

Lines changed: 115 additions & 3 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @noflow
9+
*/
10+
11+
'use strict';
12+
13+
const {spawnSync} = require('node:child_process');
14+
const fs = require('node:fs');
15+
const os = require('node:os');
16+
const path = require('node:path');
17+
18+
describe('react-native CLI compatibility', () => {
19+
let projectDir;
20+
21+
beforeEach(() => {
22+
projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'rn-cli-test-'));
23+
});
24+
25+
afterEach(() => {
26+
fs.rmSync(projectDir, {recursive: true, force: true});
27+
});
28+
29+
function runConfig(version) {
30+
const cliDir = path.join(
31+
projectDir,
32+
'node_modules',
33+
'@react-native-community',
34+
'cli',
35+
);
36+
fs.mkdirSync(cliDir, {recursive: true});
37+
fs.writeFileSync(
38+
path.join(cliDir, 'package.json'),
39+
JSON.stringify({version}),
40+
);
41+
fs.writeFileSync(
42+
path.join(cliDir, 'index.js'),
43+
'exports.run = () => console.log(JSON.stringify({platforms: {}}));',
44+
);
45+
const reactNativeDir = path.join(
46+
projectDir,
47+
'node_modules',
48+
'react-native',
49+
);
50+
fs.mkdirSync(reactNativeDir, {recursive: true});
51+
for (const file of ['cli.js', 'package.json']) {
52+
fs.copyFileSync(
53+
path.join(__dirname, '..', file),
54+
path.join(reactNativeDir, file),
55+
);
56+
}
57+
const {status, stdout, stderr} = spawnSync(
58+
process.execPath,
59+
[path.join(reactNativeDir, 'cli.js'), 'config'],
60+
{
61+
cwd: projectDir,
62+
encoding: 'utf8',
63+
env: {
64+
...process.env,
65+
NODE_PATH: path.dirname(
66+
path.dirname(require.resolve('semver/package.json')),
67+
),
68+
},
69+
},
70+
);
71+
return {status, stdout, stderr};
72+
}
73+
74+
it('refuses to run an incompatible CLI with an upgrade error', () => {
75+
expect(runConfig('20.2.0')).toMatchInlineSnapshot(`
76+
Object {
77+
"status": 1,
78+
"stderr": "Error: Refusing to use @react-native-community/cli, as the installed version is incompatible with this version of React Native. Please upgrade @react-native-community/cli dependencies to 21.x.x.
79+
",
80+
"stdout": "",
81+
}
82+
`);
83+
});
84+
85+
it.each(['21.0.0-alpha.1', '21.0.0', '22.0.0-alpha.1'])(
86+
'runs compatible CLI %s without an error',
87+
version => {
88+
expect(runConfig(version)).toEqual({
89+
status: 0,
90+
stdout: '{"platforms":{}}\n',
91+
stderr: '',
92+
});
93+
},
94+
);
95+
});

‎packages/react-native/cli.js‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
// $FlowFixMe[untyped-import]
1515
const {name} = require('./package.json');
1616
const {styleText} = require('node:util');
17+
const {major} = require('semver');
18+
19+
const COMMUNITY_CLI_PACKAGE = '@react-native-community/cli';
20+
const MIN_SUPPORTED_CLI_MAJOR = 21;
1721

1822
const deprecated = () => {
1923
throw new Error(
@@ -26,7 +30,18 @@ function findCommunityCli(startDir /*: string */ = process.cwd()) {
2630
// `@react-native-community/cli` starting from the `react-native` directory.
2731
// Instead, we should use the project root, which we assume to be the cwd.
2832
const options /*: {paths?: Array<string>, ...} */ = {paths: [startDir]};
29-
const rncli = require.resolve('@react-native-community/cli', options);
33+
const rncli = require.resolve(COMMUNITY_CLI_PACKAGE, options);
34+
const manifestPath = require.resolve(
35+
`${COMMUNITY_CLI_PACKAGE}/package.json`,
36+
options,
37+
);
38+
// $FlowFixMe[unsupported-syntax]
39+
const {version} = require(manifestPath);
40+
if (major(version) < MIN_SUPPORTED_CLI_MAJOR) {
41+
throw new Error(
42+
`Refusing to use ${COMMUNITY_CLI_PACKAGE}, as the installed version is incompatible with this version of React Native. Please upgrade ${COMMUNITY_CLI_PACKAGE} dependencies to ${MIN_SUPPORTED_CLI_MAJOR}.x.x.`,
43+
);
44+
}
3045
// $FlowFixMe[unsupported-syntax]
3146
return require(rncli);
3247
}
@@ -70,9 +85,11 @@ function main() {
7085
} catch (e) {
7186
if (isMissingCliDependency(e)) {
7287
warnWithExplicitDependency();
73-
process.exit(1);
88+
} else {
89+
console.error(String(e));
7490
}
75-
throw e;
91+
process.exitCode = 1;
92+
return;
7693
}
7794
}
7895

0 commit comments

Comments
 (0)