Skip to content

Commit

Permalink
Fix use_react_native to support absolute paths (#37545)
Browse files Browse the repository at this point in the history
Summary:
While setting up a monorepo that required a custom react-native path location (react-native-macos in my case) I was getting the following error when running `pod install`

![image](https://github.com/facebook/react-native/assets/11707729/29bacfbb-78d9-49db-9c75-3e75674d87e9)

That's because `build_codegen` and `checkAndGenerateEmptyThirdPartyProvider` functions don't check if the given `react_native_path` is absolute or relative.

This PR fixes this problem by checking if `react_native_path` starts with `/`

bypass-github-export-checks

## Changelog:

[IOS] [FIXED] - Fix `use_react_native` to support custom react native absolute paths

Pull Request resolved: #37545

Test Plan:
Modify [rn-tester/Podfile](https://github.com/facebook/react-native/tree/main/packages/rn-tester/Podfile) to use an absolute path when calling `use_react_native`

E.g.

```rb
rn_path = File.dirname(`node --print "require.resolve('react-native/package.json')"`)

use_react_native!(
  path: rn_path,
  ...
)
```

then run `pod install`

Reviewed By: cortinico

Differential Revision: D46279570

Pulled By: cipolleschi

fbshipit-source-id: 0d6da12c5617cfca2c9ef9dea08ecf728a970b6f
  • Loading branch information
gabrieldonadel authored and facebook-github-bot committed Jun 30, 2023
1 parent ed929df commit 835f62c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
41 changes: 41 additions & 0 deletions packages/react-native/scripts/cocoapods/__tests__/codegen-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,47 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenBothMissing_buildCodegen()
})
end

def testCheckAndGenerateEmptyThirdPartyProvider_withAbsoluteReactNativePath_buildCodegen()
# Arrange
rn_path = '/Users/distiller/react-native/packages/react-native'
codegen_cli_path = rn_path + "/../@react-native/codegen"
DirMock.mocked_existing_dirs([
codegen_cli_path,
])

# Act
checkAndGenerateEmptyThirdPartyProvider!(rn_path, false, dir_manager: DirMock, file_manager: FileMock)

# Assert
assert_equal(Pathname.pwd_invocation_count, 1)
assert_equal(Pod::Config.instance.installation_root.relative_path_from_invocation_count, 1)
assert_equal(FileMock.exist_invocation_params, [
rn_path + "/React/Fabric/" + @third_party_provider_header,
rn_path + "/React/Fabric/" + @tmp_schema_list_file
])
assert_equal(DirMock.exist_invocation_params, [
rn_path + "/../react-native-codegen",
codegen_cli_path,
codegen_cli_path + "/lib",
])
assert_equal(Pod::UI.collected_messages, [
"[Codegen] building #{codegen_cli_path}.",
"[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider"
])
assert_equal($collected_commands, [rn_path + "/../@react-native/codegen/scripts/oss/build.sh"])
assert_equal(FileMock.open_files[0].collected_write, ["[]"])
assert_equal(FileMock.open_files[0].fsync_invocation_count, 1)
assert_equal(Pod::Executable.executed_commands[0], {
"command" => "node",
"arguments" => [
rn_path + "/scripts/generate-provider-cli.js",
"--platform", 'ios',
"--schemaListPath", rn_path + "/React/Fabric/" + @tmp_schema_list_file,
"--outputDir", rn_path + "/React/Fabric"
]
})
end

# ================= #
# Test - RunCodegen #
# ================= #
Expand Down
15 changes: 12 additions & 3 deletions packages/react-native/scripts/cocoapods/codegen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# - dir_manager: a class that implements the `Dir` interface. Defaults to `Dir`, the Dependency can be injected for testing purposes.
# @throws an error if it could not find the codegen folder.
def build_codegen!(react_native_path, relative_installation_root, dir_manager: Dir)
codegen_repo_path = "#{relative_installation_root}/#{react_native_path}/../react-native-codegen";
codegen_npm_path = "#{relative_installation_root}/#{react_native_path}/../@react-native/codegen";
codegen_repo_path = "#{basePath(react_native_path, relative_installation_root)}/../react-native-codegen";
codegen_npm_path = "#{basePath(react_native_path, relative_installation_root)}/../@react-native/codegen";
codegen_cli_path = ""

if dir_manager.exist?(codegen_repo_path)
Expand Down Expand Up @@ -61,7 +61,7 @@ def checkAndGenerateEmptyThirdPartyProvider!(react_native_path, new_arch_enabled
Pod::Executable.execute_command(
'node',
[
"#{relative_installation_root}/#{react_native_path}/scripts/generate-provider-cli.js",
"#{basePath(react_native_path, relative_installation_root)}/scripts/generate-provider-cli.js",
"--platform", 'ios',
"--schemaListPath", temp_schema_list_path,
"--outputDir", "#{output_dir}"
Expand Down Expand Up @@ -108,3 +108,12 @@ def run_codegen!(
codegen_utils.generate_react_codegen_podspec!(react_codegen_spec, codegen_output_dir)
end
end

def basePath(react_native_path, relative_installation_root)
expanded_path = File.expand_path(react_native_path)
if expanded_path == react_native_path
react_native_path
else
File.join(relative_installation_root.to_s, react_native_path)
end
end

0 comments on commit 835f62c

Please sign in to comment.