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)) + } }