react-native-unimodules icon indicating copy to clipboard operation
react-native-unimodules copied to clipboard

Build presumes directory structure that does not work for mono repos

Open vladp opened this issue 5 years ago • 11 comments

Hello trying to use "react-native-unimodules": "0.7.0-rc.4", RN 0.60.6 Android

Getting an error from

if (!(new File(project.rootProject.projectDir.parentFile, 'package.json').exists())) {
   // There's no package.json
   throw new GradleException(
       "'addUnimodulesDependencies()' is being used in a project that doesn't seem to be a React Native project."
   )
 }

I believe, the code above, incorrectly assumes the location of package.json . Instead, it should find it in modulesPaths, as package.json would normally reside on the same level as first node_modules, and node_modules are configured via modulesPaths directive

addUnimodulesDependencies([modulesPaths: ['../../rn.common/node_modules'],
                               exclude: ['expo-face-detector']])


Or, potentially, from a new settings,eg like root settings in RN itself

      // the root of your RN project, i.e. where "package.json" lives
       // this solved my previous issue that I had to create symlink above u1b
       //and it also allowed to finally enable automatic release bundling...
       root: "${myRnAppRootDir_abs}"

This way, several node modules directories can be used at same time (which seems to be the intention behind modulesPaths directive)

For a more complete description of my directory structure, see https://github.com/react-native-community/cli/issues/804#issuecomment-542797814

-- At the moment I am not sure if a workaround exists, if somebody knows it, would very much appreciate a pointer.

vladp avatar Nov 26 '19 19:11 vladp

I'm afraid I have to agree with you on that one. 😕

Fortunately, that's what I saw too and why I refactored the script! 🎉

Would you mind trying to install react-native-unimodules from https://github.com/unimodules/react-native-unimodules/pull/84

yarn add react-native-unimodules@expo/react-native-unimodules#@sjchmiela/configurable_groovy

This should allow you to pass in a skipValidation: true flag to addUnimodulesDependencies, which will stop trying to verify whether package.json` is where we would expect it to be.

https://github.com/unimodules/react-native-unimodules/pull/84/files#diff-3c3161ef96eeb66ea8060f56589c8437R251-R258

Let me know if/how it works for you, I'd be happy to make react-native-unimodules work as expected in monorepos.

sjchmiela avatar Nov 27 '19 08:11 sjchmiela

When trying to pull I am getting permission error

yarn add react-native-unimodules@expo/react-native-unimodules#@sjchmiela/configurable_groovy

Exit code: 128
Command: git
Arguments: ls-remote --tags --heads ssh://[email protected]/expo/react-native-unimodules.git
Directory: ..
Output:
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

I can try to test just the raw gradle.groovy, would that be sufficient ?

Also, I saw that every build.gradle in Expo modules expects package.json to be in directory above settings.gradle (see my comment on https://github.com/unimodules/react-native-unimodules/issues/88#issuecomment-559264873)

That would mean that skipping the package.json validation in addUnimodulesDependencies() would not be enough (or may be I am mistaken).

In the mean time, I am using same process as you outlined in https://github.com/unimodules/react-native-unimodules/issues/88#issuecomment-558987221 to completely skip addUnimodulesDependencies() and includeUnimodulesProjects()

vladp avatar Nov 28 '19 17:11 vladp

is Anything changes? I also have problem custom path of node_module (package.json) the package.json is located outside of project

/root -/@common -/node_modules -/package.json -/App1 (use @common) --/android --/ios --index.js -/App2 (use @common) --/(same)

I can't avoid below code if (new File(rootProject.projectDir.parentFile, 'package.json').exists())

If I set force true like if (ture /* new File(rootProject.projectDir.parentFile, 'package.json').exists() */) after set modulePaths in addUnimodulesDependencies([modulePaths:['./custom/path']]) and includeUnimodulesProjects([modulePaths:['./custom/path']])

It worked

eyu0415 avatar Dec 18 '19 02:12 eyu0415

Sorry, we haven't had time to review and ship the changes, in the meantime you can try to use patch-package …package to save and automatically the patch of changes from #84.

sjchmiela avatar Dec 18 '19 07:12 sjchmiela

Got unimodules to work in our monorepo. Was pretty straightforward https://github.com/react-navigation/navigation-ex/commit/a23dfd419dceaf070948903ae70ab2170dd18769

satya164 avatar Jan 10 '20 03:01 satya164

Any news on this? Please merge this PR as we need it in our mono repo.

thongxuan avatar Feb 17 '21 23:02 thongxuan

Got unimodules to work in our monorepo. Was pretty straightforward react-navigation/navigation-ex@a23dfd4

Finally got it working in my monorepo based on this. Thank you!

ecklf avatar Jun 09 '21 08:06 ecklf

Not sure how others are getting this to work - I set addUnimodulesDependencies([modulesPaths:['../../../node_modules']]) and removed the package.json check in node_modules/react-native-unimodules/gradle.groovy but then started getting error:

A problem occurred evaluating project ':expo-application'.
> 'unimodules-core.gradle' was not found in the usual React Native dependency location. This package can only be used in such projects. Are you sure you've installed the dependencies properly?

Tied to code in node_modules/expo-application/android/build.gradle:

if (new File(rootProject.projectDir.parentFile, 'package.json').exists()) {
  apply from: project(":unimodules-core").file("../unimodules-core.gradle")
} else {
  throw new GradleException(
      '\'unimodules-core.gradle\' was not found in the usual React Native dependency location. ' +
          'This package can only be used in such projects. Are you sure you\'ve installed the dependencies properly?')
}

At this level, I don't have any reference of the overridden modulesPaths - so I don't think I could hack this together if I tried.

Unsure how deep this rabbit hole goes, but this kinda sucks. Trying to get unimodules working with autolinking in any RN project not at root package.json level seems impossible.

Jpoliachik avatar Sep 03 '21 22:09 Jpoliachik

@Jpoliachik can you provide more info about the file changes? The only files that should differ from regular install instructions are the following on Android:

packages/app/android/app/build.gradle packages/app/android/settings.gradle

ecklf avatar Sep 04 '21 03:09 ecklf

Sure thing @ecklf, these are the lines I changed in those two files. app/build.gradle

addUnimodulesDependencies([modulesPaths:['../../../node_modules']])

settings.gradle

apply from: file("../../node_modules/react-native-unimodules/gradle.groovy"); includeUnimodulesProjects([ modulesPaths : ['../../../node_modules'] ])

These two changes get me further, but I get downstream errors in expo-applications:

FAILURE: Build failed with an exception.

* Where:
Build file '***/node_modules/expo-application/android/build.gradle' line: 55

* What went wrong:
A problem occurred evaluating project ':expo-application'.
> 'unimodules-core.gradle' was not found in the usual React Native dependency location. This package can only be used in such projects. Are you sure you've installed the dependencies properly?

It looks like expo-application also hardcodes the reference to node_modules, which is not possible to override by setting modulesPaths.

Jpoliachik avatar Sep 07 '21 13:09 Jpoliachik

I have the following (bare react native, sorry I don't use expo). Maybe this helps.

app/build.gradle

apply from: '../../../../node_modules/react-native-unimodules/gradle.groovy'

addUnimodulesDependencies([ modulesPaths : ['../../../../node_modules'] ])

settings.gradle

apply from: "../../../node_modules/react-native-unimodules/gradle.groovy"; includeUnimodulesProjects([ modulesPaths : ['../../../../node_modules'] ])

ecklf avatar Sep 07 '21 17:09 ecklf