From 05d6574a18f0247db352179747ad40bb31d20add Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Tue, 6 Dec 2022 02:16:42 -0800 Subject: [PATCH] RNGP - `findPackageJsonFile` should return `null` if `package.json` does not exist (#35566) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: `findPackageJsonFile` always returns a path even though `package.json` does not exist. This causes issues in libraries whose repo setups look like: ``` react-native-webview ├── android │ └── build.gradle ├── example <-- Note the lack of `package.json` here │ └── App.tsx ├── ios │ └── RNCWebView.xcodeproj ├── macos │ └── RNCWebView.xcodeproj ├── package.json └── src ``` When `newArchEnabled=true`, running `yarn android` will fail with the following: ``` FAILURE: Build failed with an exception. * What went wrong: Could not determine the dependencies of task ':app:generateCodegenArtifactsFromSchema'. > Could not create task ':app:generateCodegenSchemaFromJavaScript'. > /~/react-native-webview/example/package.json (No such file or directory) ``` ## Changelog [Android] [Fixed] - `findPackageJsonFile` should return `null` if `package.json` does not exist Pull Request resolved: https://github.com/facebook/react-native/pull/35566 Test Plan: ``` git clone https://github.com/react-native-webview/react-native-webview.git cd react-native-webview git checkout new-arch-ios yarn cd example/android ./gradlew clean assembleDebug ``` Reviewed By: NickGerleman Differential Revision: D41739176 Pulled By: cortinico fbshipit-source-id: cab0f1f717db160df244c9bb2769e345d6e19917 --- .../com/facebook/react/utils/PathUtils.kt | 19 +++++++++++++------ .../com/facebook/react/utils/PathUtilsTest.kt | 3 +-- .../facebook/react/utils/ProjectUtilsTest.kt | 8 ++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt index 74fe49ca17a316..a4239dd898e13a 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt @@ -188,12 +188,19 @@ internal fun projectPathToLibraryName(projectPath: String): String = * Gradle module (generally the case for library projects) or we fallback to looking into the `root` * folder of a React Native project (generally the case for app projects). */ -internal fun findPackageJsonFile(project: Project, extension: ReactExtension): File? = - if (project.file("../package.json").exists()) { - project.file("../package.json") - } else { - extension.root.file("package.json").orNull?.asFile - } +internal fun findPackageJsonFile(project: Project, extension: ReactExtension): File? { + val inParent = project.file("../package.json") + if (inParent.exists()) { + return inParent + } + + val fromExtension = extension.root.file("package.json").orNull?.asFile + if (fromExtension?.exists() == true) { + return fromExtension + } + + return null +} /** * Function to look for the `package.json` and parse it. It returns a [ModelPackageJson] if found or diff --git a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt index fb230f49551df6..7871122498c9ae 100644 --- a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt +++ b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt @@ -13,7 +13,6 @@ import com.facebook.react.tests.OS import com.facebook.react.tests.OsRule import com.facebook.react.tests.WithOs import java.io.File -import java.io.FileNotFoundException import org.gradle.testfixtures.ProjectBuilder import org.junit.Assert.* import org.junit.Assume.assumeTrue @@ -249,7 +248,7 @@ class PathUtilsTest { assertEquals(localFile, findPackageJsonFile(project, extension)) } - @Test(expected = FileNotFoundException::class) + @Test fun readPackageJsonFile_withMissingFile_returnsNull() { val moduleFolder = tempFolder.newFolder("awesome-module") val project = ProjectBuilder.builder().withProjectDir(moduleFolder).build() diff --git a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt index d918c285d1cf39..4851fb2b0d9638 100644 --- a/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt +++ b/packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/ProjectUtilsTest.kt @@ -161,4 +161,12 @@ class ProjectUtilsTest { assertFalse(project.needsCodegenFromPackageJson(model)) } + + @Test + fun needsCodegenFromPackageJson_withMissingPackageJson_returnsFalse() { + val project = createProject() + val extension = TestReactExtension(project) + + assertFalse(project.needsCodegenFromPackageJson(extension)) + } }