-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: cliPath
should handle absolute paths
#32983
Conversation
Base commit: f89a0b7 |
Base commit: f89a0b7 |
@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
react.gradle
Outdated
@@ -26,7 +26,7 @@ def detectEntryFile(config) { | |||
*/ | |||
def detectCliPath(config) { | |||
if (config.cliPath) { | |||
return "${projectDir}/${config.cliPath}" | |||
return (config.cliPath.startsWith("/")) ? config.cliPath : "${projectDir}/${config.cliPath}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this. However I would like to keep react.gradle
aligned with our Gradle Plugin code:
Lines 88 to 92 in b3c69e8
// 3. cli.js in the root folder | |
val rootCliJs = File(reactRoot, "node_modules/react-native/cli.js") | |
if (rootCliJs.exists()) { | |
return rootCliJs.absolutePath | |
} |
So let's maybe check if the file exists instead of checking if .startsWith("/")
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback, tried to do it as minimal as possible.
What do you think about doing a slightly bigger change in that spirit?
The following would use .exists()
check, and also show an explicit error when an erroneous cliPath
is provided, as opposed to having to dig through logs and code for the info.
def detectCliPath(config) {
if (config.cliPath) {
if (new File(config.cliPath).exists()) {
return config.cliPath
}
if (new File("${projectDir}/${config.cliPath}").exists()) {
return "${projectDir}/${config.cliPath}"
}
} else if (new File("${projectDir}/../../node_modules/react-native/cli.js").exists()) {
return "${projectDir}/../../node_modules/react-native/cli.js"
}
throw new Exception("Couldn't determine CLI location. " +
"Please set `project.ext.react.cliPath` to the path of the react-native cli.js");
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about doing a slightly bigger change in that spirit?
That's a change in the right step. However I'd try to keep the two detectCliPath
aligned if possible. How do you feel about implementing the same login from PathUtils.kt
inside react.gradle
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I'll make changes to PathUtils
as well, to include absolute pathing, and .exists
checks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's also great. Please udpate PathUtilsTest.kt as well 👍
@cortinico please let me know if this is more like what would be best to merge, or advise on potential oversight I could not run the tests locally due to some technical difficulties, will try and verify next week if CI doesn't. |
Thanks for the follow-up.
Yup the CI is red with:
|
@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@Krisztiaan the CI is still red. Let me know if you need support to move this forward 👍 |
I'll try running the test on my intel today or tomorrow, and dust off some Gradle knowledge while at that. As far as I can tell, this is a variable not initialised issue, but the code itself is now at the same level / same feature set on both sides. |
since the according to this SO answer, the e.g. --- react.gradle.orig 2022-02-14 16:33:50.000000000 +0800
+++ react.gradle 2022-02-14 16:35:31.000000000 +0800
@@ -24,15 +24,38 @@
/**
* Detects CLI location in a similar fashion to the React Native CLI
*/
-def detectCliPath(config) {
+def detectCliPath(config, reactRoot) {
+ // 1. preconfigured path
if (config.cliPath) {
- return "${projectDir}/${config.cliPath}"
+ def cliJsAbsolute = new File(config.cliPath)
+ if (cliJsAbsolute.exists()) {
+ return cliJsAbsolute.getAbsolutePath()
+ }
+ def cliJsRelativeToRoot = new File("${rootDir}/${config.cliPath}")
+ if (cliJsRelativeToRoot.exists()) {
+ return cliJsRelativeToRoot.getAbsolutePath()
+ }
+ def cliJsRelativeToProject = new File("${projectDir}/${config.cliPath}")
+ if (cliJsRelativeToProject.exists()) {
+ return cliJsRelativeToProject.getAbsolutePath()
+ }
}
- if (new File("${projectDir}/../../node_modules/react-native/cli.js").exists()) {
- return "${projectDir}/../../node_modules/react-native/cli.js"
+
+ // 2. node module path
+ def cliJsFromNode = new File(["node", "--print", "require.resolve('react-native/cli').bin"].execute(null, rootDir).text.trim())
+ if (cliJsFromNode.exists()) {
+ return cliJsFromNode.getAbsolutePath()
}
+
+ // 3. cli.js in the root folder
+ def rootCliJs = new File(reactRoot, "node_modules/react-native/cli.js")
+ if (rootCliJs.exists()) {
+ return rootCliJs.getAbsolutePath()
+ }
+
throw new Exception("Couldn't determine CLI location. " +
- "Please set `project.ext.react.cliPath` to the path of the react-native cli.js");
+ "Please set `project.ext.react.cliPath` to the path of the react-native cli.js file. " +
+ "This file typically resides in `node_modules/react-native/cli.js`");
}
def composeSourceMapsPath = config.composeSourceMapsPath ?: "node_modules/react-native/scripts/compose-source-maps.js"
@@ -133,7 +156,7 @@
// Additional node and packager commandline arguments
def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
- def cliPath = detectCliPath(config)
+ def cliPath = detectCliPath(config, reactRoot)
def execCommand = []
|
@cortinico could you help to take a look this pr when you get a chance? it looks like the ci failure for |
@cortinico it seems like the work is done here, and the only failing test is non relevant. Can someone take a look, and provide feedback, review, or progress on this one? |
@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My apologies if this took a bit longer than expected on our end. It was on my table and got (accidentally) lost in the several of PRs I have to review 🙏 my bad. I've scheduled for landing now.
We'll also make sure to cherry-pick this into RN 0.68.0-rc3 (cc @ShikaSD)
This pull request was successfully merged by @Krisztiaan in 5d560ca. When will my fix make it into a release? | Upcoming Releases |
thanks @cortinico for pushing this to land and @Krisztiaan for the great contribution! |
Summary: Avoid breaking tools relying on absolute path for `cliPath` ## Changelog [Android] [Fixed] - Enable cliPath to have an absolute path value Pull Request resolved: #32983 Test Plan: declare `cliPath` from `expo`: ```groovy cliPath: new File(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()).getParentFile().getAbsolutePath() + "/cli.js", ``` and run an android build Reviewed By: ShikaSD Differential Revision: D33843275 Pulled By: cortinico fbshipit-source-id: 65f55a5e07a4ec0a6205d5f06f150377708c30cc
This seems to be still an issue with react native 0.68.1 After applying the mentioned patch above the gradle sync worked for me. |
This change is scheduled to land on RN 0.69. If you need a fix on a RN 0.68 setup, you can switch to use the React Native Gradle Plugin (which is fixed for this on 0.68.1). |
Summary: Avoid breaking tools relying on absolute path for `cliPath` ## Changelog [Android] [Fixed] - Enable cliPath to have an absolute path value Pull Request resolved: facebook#32983 Test Plan: declare `cliPath` from `expo`: ```groovy cliPath: new File(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()).getParentFile().getAbsolutePath() + "/cli.js", ``` and run an android build Reviewed By: ShikaSD Differential Revision: D33843275 Pulled By: cortinico fbshipit-source-id: 65f55a5e07a4ec0a6205d5f06f150377708c30cc
Summary
Avoid breaking tools relying on absolute path for
cliPath
Changelog
[Android] [Fixed] - Enable cliPath to have an absolute path value
Test Plan
declare
cliPath
fromexpo
:and run an android build