Skip to content

Commit 7163033

Browse files
kittenmeta-codesync[bot]
authored andcommitted
Update generate-artifacts-executor script to account for optional output path (#54609)
Summary: The argument for the output path to this CLI has always been optional, but the types didn't reflect this, which caused a few regressions in recent changes (e.g. #53503) There's several code paths where the `baseOutputPath` can be altered or derived in different ways, which wasn't respected in logic before the main codegen output block or in `generateReactCodegenPodspec`. When applied, this PR alters the `baseOutputPath` logic to use a default by `computeBaseOutputPath` extracted from `computeOutputPath` and have an added `process.env.RCT_SCRIPT_OUTPUT_DIR` override, which was previously only applied to `readReactNativeConfig` This should restore: - `react-native codegen` being run without an `--outputPath` argument - codegen running with a `package.json:codegenConfig.outputDir` override Resolves #54473 ## Changelog: [GENERAL] [FIXED] - Ensure codegen CLI supports unspecified `--outputPath` argument and respects `codegenConfig.outputDir` configurations Pull Request resolved: #54609 Test Plan: - Manually run `react-native codegen` in `rn-tester` Reviewed By: cipolleschi Differential Revision: D87456501 Pulled By: cortinico fbshipit-source-id: 997e99af1f99227eb70955478643b28b1a90df61
1 parent 7c95d68 commit 7163033

File tree

3 files changed

+70
-35
lines changed

3 files changed

+70
-35
lines changed

packages/react-native/scripts/codegen/generate-artifacts-executor/generateReactCodegenPodspec.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,18 @@ function getInputFiles(appPath /*: string */, appPkgJson /*: $FlowFixMe */) {
7676
return `[${list}]`;
7777
}
7878

79-
function codegenScripts(appPath /*: string */, outputPath /*: string */) {
80-
const relativeAppPath = path.relative(outputPath, appPath);
79+
function codegenScripts(appPath /*: string */, baseOutputPath /*: string */) {
80+
const relativeAppPath = path.relative(baseOutputPath, appPath);
81+
const relativeReactNativeRootFolder = path.relative(
82+
baseOutputPath,
83+
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
84+
);
8185
return `<<-SCRIPT
8286
pushd "$PODS_ROOT/../" > /dev/null
8387
RCT_SCRIPT_POD_INSTALLATION_ROOT=$(pwd)
8488
popd >/dev/null
8589
86-
export RCT_SCRIPT_RN_DIR="$RCT_SCRIPT_POD_INSTALLATION_ROOT/${path.relative(outputPath, REACT_NATIVE_PACKAGE_ROOT_FOLDER)}"
90+
export RCT_SCRIPT_RN_DIR="$RCT_SCRIPT_POD_INSTALLATION_ROOT/${relativeReactNativeRootFolder}"
8791
export RCT_SCRIPT_APP_PATH="$RCT_SCRIPT_POD_INSTALLATION_ROOT/${relativeAppPath.length === 0 ? '.' : relativeAppPath}"
8892
export RCT_SCRIPT_OUTPUT_DIR="$RCT_SCRIPT_POD_INSTALLATION_ROOT"
8993
export RCT_SCRIPT_TYPE="withCodegenDiscovery"

packages/react-native/scripts/codegen/generate-artifacts-executor/index.js

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const path = require('path');
6363
function execute(
6464
projectRoot /*: string */,
6565
targetPlatform /*: string */,
66-
baseOutputPath /*: string */,
66+
optionalBaseOutputPath /*: ?string */,
6767
source /*: string */,
6868
runReactNativeCodegen /*: boolean */ = true,
6969
) {
@@ -88,32 +88,35 @@ function execute(
8888
buildCodegenIfNeeded();
8989
}
9090

91-
const reactNativeConfig = readReactNativeConfig(
92-
projectRoot,
93-
// NOTE: Used to load `build/generated/autolinking/autolinking.json` generated by `scripts/cocoapods/autolinking.rb`
94-
// If we have RCT_SCRIPT_OUTPUT_DIR (set in `react_native_pods_utils/script_phases.sh`, it takes precedence, otherwise
95-
// we search for the `autolinking.json` output in the `baseOutputPath`
96-
process.env.RCT_SCRIPT_OUTPUT_DIR != null &&
97-
process.env.RCT_SCRIPT_OUTPUT_DIR.length > 0
98-
? process.env.RCT_SCRIPT_OUTPUT_DIR
99-
: baseOutputPath,
100-
);
101-
102-
const codegenEnabledLibraries = findCodegenEnabledLibraries(
103-
pkgJson,
104-
projectRoot,
105-
baseOutputPath,
106-
reactNativeConfig,
107-
);
108-
109-
if (codegenEnabledLibraries.length === 0) {
110-
codegenLog('No codegen-enabled libraries found.', true);
111-
}
112-
113-
let platforms =
91+
const platforms =
11492
targetPlatform === 'all' ? supportedPlatforms : [targetPlatform];
11593

94+
// NOTE: We cache the external libraries search (which may not run) across platforms to not change previous behaviour
95+
const externalLibrariesCache /*: { current?: ?Array<$FlowFixMe> } */ = {};
96+
11697
for (const platform of platforms) {
98+
// NOTE: This needs to be computed per-platform since `platform` can alter the path via a `package.json:codegenConfig.outputDir[platform]` override
99+
const baseOutputPath = computeBaseOutputPath(
100+
projectRoot,
101+
optionalBaseOutputPath,
102+
pkgJson,
103+
platform,
104+
);
105+
const reactNativeConfig = readReactNativeConfig(
106+
projectRoot,
107+
baseOutputPath,
108+
);
109+
const codegenEnabledLibraries = findCodegenEnabledLibraries(
110+
pkgJson,
111+
projectRoot,
112+
baseOutputPath,
113+
reactNativeConfig,
114+
externalLibrariesCache,
115+
);
116+
if (codegenEnabledLibraries.length === 0) {
117+
codegenLog('No codegen-enabled libraries found.', true);
118+
}
119+
117120
const disabledLibraries = findDisabledLibrariesByPlatform(
118121
reactNativeConfig,
119122
platform,
@@ -208,22 +211,38 @@ function readOutputDirFromPkgJson(
208211
return null;
209212
}
210213

211-
function computeOutputPath(
214+
function computeBaseOutputPath(
212215
projectRoot /*: string */,
213-
baseOutputPath /*: string */,
216+
optionalBaseOutputPath /*: ?string */,
214217
pkgJson /*: $FlowFixMe */,
215218
platform /*: string */,
216219
) {
217-
if (baseOutputPath == null) {
220+
if (
221+
process.env.RCT_SCRIPT_OUTPUT_DIR != null &&
222+
process.env.RCT_SCRIPT_OUTPUT_DIR.length > 0
223+
) {
224+
return process.env.RCT_SCRIPT_OUTPUT_DIR;
225+
}
226+
let baseOutputPath /*: string */;
227+
if (optionalBaseOutputPath == null) {
218228
const outputDirFromPkgJson = readOutputDirFromPkgJson(pkgJson, platform);
219229
if (outputDirFromPkgJson != null) {
220-
// $FlowFixMe[reassign-const]
221230
baseOutputPath = path.join(projectRoot, outputDirFromPkgJson);
222231
} else {
223-
// $FlowFixMe[reassign-const]
224232
baseOutputPath = projectRoot;
225233
}
234+
} else {
235+
baseOutputPath = optionalBaseOutputPath;
226236
}
237+
return baseOutputPath;
238+
}
239+
240+
function computeOutputPath(
241+
projectRoot /*: string */,
242+
baseOutputPath /*: string */,
243+
pkgJson /*: $FlowFixMe */,
244+
platform /*: string */,
245+
) /*: string */ {
227246
if (pkgJsonIncludesGeneratedCode(pkgJson)) {
228247
// Don't create nested directories for libraries to make importing generated headers easier.
229248
return baseOutputPath;

packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ function cleanupEmptyFilesAndFolders(filepath /*: string */) {
9898
}
9999

100100
function readGeneratedAutolinkingOutput(
101+
projectRoot /*: string */,
101102
baseOutputPath /*: string */,
102103
) /*: $FlowFixMe */ {
103104
// NOTE: Generated by scripts/cocoapods/autolinking.rb in list_native_modules (called by use_native_modules)
105+
// The `baseOutputPath` is based on a CLI argument and optional
104106
const autolinkingGeneratedPath = path.resolve(
105107
baseOutputPath,
106108
'build/generated/autolinking/autolinking.json',
@@ -120,7 +122,10 @@ function readReactNativeConfig(
120122
projectRoot /*: string */,
121123
baseOutputPath /*: string */,
122124
) /*: $FlowFixMe */ {
123-
const autolinkingOutput = readGeneratedAutolinkingOutput(baseOutputPath);
125+
const autolinkingOutput = readGeneratedAutolinkingOutput(
126+
projectRoot,
127+
baseOutputPath,
128+
);
124129
const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js');
125130
if (autolinkingOutput) {
126131
return autolinkingOutput;
@@ -141,6 +146,7 @@ function findCodegenEnabledLibraries(
141146
projectRoot /*: string */,
142147
baseOutputPath /*: string */,
143148
reactNativeConfig /*: $FlowFixMe */,
149+
externalLibrariesCache /*: { current?: ?Array<$FlowFixMe> } */ = {},
144150
) /*: Array<$FlowFixMe> */ {
145151
const projectLibraries = findProjectRootLibraries(pkgJson, projectRoot);
146152
if (pkgJsonIncludesGeneratedCode(pkgJson)) {
@@ -149,8 +155,14 @@ function findCodegenEnabledLibraries(
149155
const libraries = [...projectLibraries];
150156
// If we ran autolinking, we shouldn't try to run our own "autolinking-like"
151157
// library discovery
152-
if (!readGeneratedAutolinkingOutput(baseOutputPath)) {
153-
libraries.push(...findExternalLibraries(pkgJson, projectRoot));
158+
if (!readGeneratedAutolinkingOutput(projectRoot, baseOutputPath)) {
159+
const externalLibraries =
160+
externalLibrariesCache.current ??
161+
(externalLibrariesCache.current = findExternalLibraries(
162+
pkgJson,
163+
projectRoot,
164+
));
165+
libraries.push(...externalLibraries);
154166
}
155167
libraries.push(
156168
...findLibrariesFromReactNativeConfig(projectRoot, reactNativeConfig),

0 commit comments

Comments
 (0)