Skip to content

Commit a2eb29e

Browse files
kittengabrieldonadel
authored andcommitted
Use autolinking react-native-config output in iOS artifacts generator (#53503)
Summary: Resolves #53501 This is a pretty major oversight of (presumably) the old autolinking refactor. The iOS autolinking's second stage, invoked in `use_react_native!` does not accept the `react-native-config` sub-command's `react-native-config` output. This is only invoked and used in the prior step, `use_native_modules`. The second step instead invokes old code that does something _similar_ to the new autolinking in `scripts/generate-artifacts-executor`, and happens to align in most cases. (But it does "autolinking" from scratch). tl;dr: When the results don't match up, things go wrong. Instead, we now write the autolinking (react native config) results to a file, then read the output back in the second step. This doesn't affect Android/Gradle, which are implemented correctly. [IOS] [FIXED] - Use autolinking-generated react-native-config output in second step of cocoapods linking that generates artifacts and generated source Pull Request resolved: #53503 Test Plan: - See #53501 for failing repro - Clone for working repro: https://github.com/byCedric/react-native-codegen-ios-autolinking/tree/fix-54503 - Note: Contains this PR's changes as a patch - `bun install` - `bun expo run:ios` Reviewed By: cortinico Differential Revision: D81490755 Pulled By: cipolleschi fbshipit-source-id: eefe786a116404f4ed24bd7125dfb108a811f71e
1 parent cc493fc commit a2eb29e

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

packages/react-native/scripts/cocoapods/autolinking.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def list_native_modules!(config_command)
4040
packages = config["dependencies"]
4141
ios_project_root = Pathname.new(config["project"]["ios"]["sourceDir"])
4242
react_native_path = Pathname.new(config["reactNativePath"])
43+
codegen_output_path = ios_project_root.join("build/generated/autolinking/autolinking.json")
44+
45+
# Write autolinking react-native-config output to codegen folder
46+
FileUtils.mkdir_p(File.dirname(codegen_output_path))
47+
File.write(codegen_output_path, json)
48+
4349
found_pods = []
4450

4551
packages.each do |package_name, package|

packages/react-native/scripts/cocoapods/codegen_utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def self.clean_up_build_folder(rn_path, codegen_dir, dir_manager: Dir, file_mana
8787
codegen_path = file_manager.join(ios_folder, codegen_dir)
8888
return if !dir_manager.exist?(codegen_path)
8989

90-
FileUtils.rm_rf(dir_manager.glob("#{codegen_path}/*"))
90+
FileUtils.rm_rf("#{codegen_path}")
9191
base_provider_path = file_manager.join(rn_path, 'React', 'Fabric', 'RCTThirdPartyFabricComponentsProvider')
9292
FileUtils.rm_rf("#{base_provider_path}.h")
9393
FileUtils.rm_rf("#{base_provider_path}.mm")

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ function execute(
8686
buildCodegenIfNeeded();
8787
}
8888

89-
const reactNativeConfig = readReactNativeConfig(projectRoot);
89+
const reactNativeConfig = readReactNativeConfig(
90+
projectRoot,
91+
baseOutputPath,
92+
);
9093
const codegenEnabledLibraries = findCodegenEnabledLibraries(
9194
pkgJson,
9295
projectRoot,
96+
baseOutputPath,
9397
reactNativeConfig,
9498
);
9599

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,40 @@ function cleanupEmptyFilesAndFolders(filepath /*: string */) {
9797
}
9898
}
9999

100-
function readReactNativeConfig(projectRoot /*: string */) /*: $FlowFixMe */ {
101-
const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js');
100+
function readGeneratedAutolinkingOutput(
101+
baseOutputPath /*: string */,
102+
) /*: $FlowFixMe */ {
103+
// NOTE: Generated by scripts/cocoapods/autolinking.rb in list_native_modules (called by use_native_modules)
104+
const autolinkingGeneratedPath = path.resolve(
105+
baseOutputPath,
106+
'build/generated/autolinking/autolinking.json',
107+
);
108+
if (fs.existsSync(autolinkingGeneratedPath)) {
109+
// $FlowFixMe[unsupported-syntax]
110+
return require(autolinkingGeneratedPath);
111+
} else {
112+
codegenLog(
113+
`Could not find generated autolinking output at: ${autolinkingGeneratedPath}`,
114+
);
115+
return null;
116+
}
117+
}
102118

103-
if (!fs.existsSync(rnConfigFilePath)) {
119+
function readReactNativeConfig(
120+
projectRoot /*: string */,
121+
baseOutputPath /*: string */,
122+
) /*: $FlowFixMe */ {
123+
const autolinkingOutput = readGeneratedAutolinkingOutput(baseOutputPath);
124+
const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js');
125+
if (autolinkingOutput) {
126+
return autolinkingOutput;
127+
} else if (fs.existsSync(rnConfigFilePath)) {
128+
// $FlowIgnore[unsupported-syntax]
129+
return require(rnConfigFilePath);
130+
} else {
131+
codegenLog(`Could not find React Native config at: ${rnConfigFilePath}`);
104132
return {};
105133
}
106-
107-
// $FlowIgnore[unsupported-syntax]
108-
return require(rnConfigFilePath);
109134
}
110135

111136
/**
@@ -114,17 +139,23 @@ function readReactNativeConfig(projectRoot /*: string */) /*: $FlowFixMe */ {
114139
function findCodegenEnabledLibraries(
115140
pkgJson /*: $FlowFixMe */,
116141
projectRoot /*: string */,
142+
baseOutputPath /*: string */,
117143
reactNativeConfig /*: $FlowFixMe */,
118144
) /*: Array<$FlowFixMe> */ {
119145
const projectLibraries = findProjectRootLibraries(pkgJson, projectRoot);
120146
if (pkgJsonIncludesGeneratedCode(pkgJson)) {
121147
return projectLibraries;
122148
} else {
123-
return [
124-
...projectLibraries,
125-
...findExternalLibraries(pkgJson, projectRoot),
149+
const libraries = [...projectLibraries];
150+
// If we ran autolinking, we shouldn't try to run our own "autolinking-like"
151+
// library discovery
152+
if (!readGeneratedAutolinkingOutput(baseOutputPath)) {
153+
libraries.push(...findExternalLibraries(pkgJson, projectRoot));
154+
}
155+
libraries.push(
126156
...findLibrariesFromReactNativeConfig(projectRoot, reactNativeConfig),
127-
];
157+
);
158+
return libraries;
128159
}
129160
}
130161

0 commit comments

Comments
 (0)