Skip to content

Commit

Permalink
RNGP - findPackageJsonFile should return null if package.json d…
Browse files Browse the repository at this point in the history
…oes not exist (#35566)

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: #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
  • Loading branch information
tido64 authored and facebook-github-bot committed Dec 6, 2022
1 parent 96d6680 commit 913ebd2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

0 comments on commit 913ebd2

Please sign in to comment.