Skip to content

Commit e70166a

Browse files
authored
Fix build_codegen! not finding @react-native/codegen in pnpm setups (#41456)
`build_codegen!` currently assumes that `@react-native/codegen` gets installed next to `react-native`. In a pnpm setup, it's found under `/~/react-native/node_modules/@react-native/codegen` instead. However, as @dmytrorykun pointed out, we don't actually need to build it outside of this repository.
1 parent df2bbba commit e70166a

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,32 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenFileAlreadyExists_doNothing(
6868
assert_equal(Pod::Executable.executed_commands.length, 0)
6969
end
7070

71-
def testCheckAndGenerateEmptyThirdPartyProvider_whenHeaderMissingAndCodegenMissing_raiseError()
71+
def testCheckAndGenerateEmptyThirdPartyProvider_whenHeaderMissingAndCodegenMissing_dontBuildCodegen()
7272

7373
# Arrange
7474
FileMock.mocked_existing_files([
7575
@base_path + "/build/" + @third_party_provider_implementation,
7676
])
7777

7878
# Act
79-
assert_raise {
79+
assert_nothing_raised {
8080
checkAndGenerateEmptyThirdPartyProvider!(@prefix, false, dir_manager: DirMock, file_manager: FileMock)
8181
}
8282

8383
# Assert
8484
assert_equal(Pathname.pwd_invocation_count, 1)
8585
assert_equal(Pod::Config.instance.installation_root.relative_path_from_invocation_count, 1)
8686
assert_equal(FileMock.exist_invocation_params, [
87-
@prefix + "/React/Fabric/" + @third_party_provider_header
87+
@prefix + "/React/Fabric/" + @third_party_provider_header,
88+
@prefix + "/React/Fabric/tmpSchemaList.txt",
8889
])
8990
assert_equal(DirMock.exist_invocation_params, [
9091
@base_path + "/"+ @prefix + "/../react-native-codegen",
91-
@base_path + "/"+ @prefix + "/../@react-native/codegen",
9292
])
93-
assert_equal(Pod::UI.collected_messages, [])
93+
assert_equal(Pod::UI.collected_messages, ["[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider"])
9494
assert_equal($collected_commands, [])
95-
assert_equal(FileMock.open_files.length, 0)
96-
assert_equal(Pod::Executable.executed_commands.length, 0)
95+
assert_equal(FileMock.open_files.length, 1)
96+
assert_equal(Pod::Executable.executed_commands.length, 1)
9797
end
9898

9999
def testCheckAndGenerateEmptyThirdPartyProvider_whenImplementationMissingAndCodegenrepoExists_dontBuildCodegen()
@@ -145,7 +145,7 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenImplementationMissingAndCode
145145

146146
def testCheckAndGenerateEmptyThirdPartyProvider_whenBothMissing_buildCodegen()
147147
# Arrange
148-
codegen_cli_path = @base_path + "/" + @prefix + "/../@react-native/codegen"
148+
codegen_cli_path = @base_path + "/" + @prefix + "/../react-native-codegen"
149149
DirMock.mocked_existing_dirs([
150150
codegen_cli_path,
151151
])
@@ -160,15 +160,14 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenBothMissing_buildCodegen()
160160
@prefix + "/React/Fabric/" + @tmp_schema_list_file
161161
])
162162
assert_equal(DirMock.exist_invocation_params, [
163-
@base_path + "/" + @prefix + "/../react-native-codegen",
164163
codegen_cli_path,
165164
codegen_cli_path + "/lib",
166165
])
167166
assert_equal(Pod::UI.collected_messages, [
168-
"[Codegen] building #{codegen_cli_path}.",
167+
"[Codegen] building #{codegen_cli_path}",
169168
"[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider"
170169
])
171-
assert_equal($collected_commands, ["~/app/ios/../../../@react-native/codegen/scripts/oss/build.sh"])
170+
assert_equal($collected_commands, ["~/app/ios/../../../react-native-codegen/scripts/oss/build.sh"])
172171
assert_equal(FileMock.open_files[0].collected_write, ["[]"])
173172
assert_equal(FileMock.open_files[0].fsync_invocation_count, 1)
174173
assert_equal(Pod::Executable.executed_commands[0], {

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,12 @@
1111
# - dir_manager: a class that implements the `Dir` interface. Defaults to `Dir`, the Dependency can be injected for testing purposes.
1212
# @throws an error if it could not find the codegen folder.
1313
def build_codegen!(react_native_path, relative_installation_root, dir_manager: Dir)
14-
codegen_repo_path = "#{relative_installation_root}/#{react_native_path}/../react-native-codegen";
15-
codegen_npm_path = "#{relative_installation_root}/#{react_native_path}/../@react-native/codegen";
16-
codegen_cli_path = ""
14+
codegen_repo_path = "#{relative_installation_root}/#{react_native_path}/../react-native-codegen";
15+
return unless dir_manager.exist?(codegen_repo_path) && !dir_manager.exist?("#{codegen_repo_path}/lib")
1716

18-
if dir_manager.exist?(codegen_repo_path)
19-
codegen_cli_path = codegen_repo_path
20-
elsif dir_manager.exist?(codegen_npm_path)
21-
codegen_cli_path = codegen_npm_path
22-
else
23-
raise "[codegen] Could not find react-native-codegen."
24-
end
25-
26-
if !dir_manager.exist?("#{codegen_cli_path}/lib")
27-
Pod::UI.puts "[Codegen] building #{codegen_cli_path}."
28-
system("#{codegen_cli_path}/scripts/oss/build.sh")
29-
end
30-
end
17+
Pod::UI.puts "[Codegen] building #{codegen_repo_path}"
18+
system("#{codegen_repo_path}/scripts/oss/build.sh")
19+
end
3120

3221
# It generates an empty `ThirdPartyProvider`, required by Fabric to load the components
3322
#

0 commit comments

Comments
 (0)