Skip to content
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

feat: run npx from JS app dir #824

Merged
merged 6 commits into from
Nov 14, 2019
Merged

feat: run npx from JS app dir #824

merged 6 commits into from
Nov 14, 2019

Conversation

thymikee
Copy link
Member

@thymikee thymikee commented Oct 24, 2019

Summary:

Gradle may be run from a driectory that's outside of JS project, with a passed project dir (through -p flag) like path/to/app/android/gradlew bundleRelease -p ../app/android/app.

project/
  app/ -> js, android, ios files
  scripts/ -> running gradlew from here, e.g. through Fastlane

Gradle CWD is usually (maybe always, but they don't give guarantees) set to the directory where it's ran from. This tricks npx, which doesn't know where to find react-native binary. To account for it, we can spawn npx with a CWD passed to it. We don't need an exact path to android project directory, path to the project which holds @react-native-community/cli in node_modules is just fine with current config search mechanism.

I'm not 100% sure about this fix, open for discussion.

Fixes #793
Fixes #804

Test Plan:

I don't have an automated test for this, you'll need to verify on repro provided in #793.

def npx = Os.isFamily(Os.FAMILY_WINDOWS) ? "npx.cmd" : "npx"
def command = "${npx} --quiet react-native config"
def command = "${npx} --quiet --no-install react-native config"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding --no-install so we don't accidentally install react-native into local npm cache. Verified that it's already present in npx shipped with Node 8.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it related to #823?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really sure, but it's preferable to run with --no-install anyway. Feel free to send a PR, as I don't want to mix iOS and Android in this one :)

@AndreiCalazans
Copy link

I applied these changes inside node_modules and I"m still seeing an issue, it's no longer the same one pointed out in #793.

Here is a gist of the error outputted by Gradle
https://gist.github.com/AndreiCalazans/808217902369be04e0ab5c591afcccc7

@thymikee
Copy link
Member Author

@AndreiCalazans it's a normal project structure, in terms of running from Android Studio. I'll need you to debug this a little deeper, as Gradle sync and build works on my machine. This is likely an environmental issue I cannot reproduce.

@grabbou
Copy link
Member

grabbou commented Oct 29, 2019

How about simple heuristics here: if -p flag is present, spawn (cd $pValue && react-native config), otherwise just react-native config?

@thymikee
Copy link
Member Author

I'm not sure how to access the -p value. Cc @Salakar

@Salakar
Copy link
Member

Salakar commented Oct 29, 2019

I'm not sure how to access the -p value. Cc @Salakar

Afaik it's not possible to read this. Only properties passed via -P (e.g. -PcustomProp=foo) or --args can be read in the build script.

@codestersunion
Copy link

In my testing, a cd <path> && npx... does not solve the issue.

@grabbou
Copy link
Member

grabbou commented Nov 4, 2019

@thymikee @Salakar that sounds to me like a serious issue not being able to read args. Being able to do that or at least have the equivalent of __dirname would seriously help with reducing the amount of code needed to resolve this.

Let's discuss this tomorrow and figure how to move forward!

@ifyapishore
Copy link

ifyapishore commented Nov 5, 2019 via email

@lucasbento
Copy link
Member

Bump
What's left to be done for this PR?

Looking forward to v3

@thymikee
Copy link
Member Author

It's done, waiting for @grabbou and @Esemesek to review. And maybe @Salakar :)

@grabbou
Copy link
Member

grabbou commented Nov 14, 2019

Do we know why the Windows CI is failing? It's blocking few important PRs that are good to be merged otherwise. I'd ship them all if that's not the case.

@@ -2,6 +2,7 @@ import groovy.json.JsonSlurper
import org.gradle.initialization.DefaultSettings
import org.apache.tools.ant.taskdefs.condition.Os

def jsAppDir = buildscript.sourceFile.toString().split("node_modules/@react-native-community/cli-platform-android/native_modules.gradle")[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think we can just split by node_modules/@react-native-community/cli-platform-android, which is a path to the module (the gradle file at the end is not needed and may break if we refactor files).

I was thinking to go with node_modules, but that would break in case dependencies were not hoisted.

ArrayList<HashMap<String, String>> reactNativeModules = new ArrayList<HashMap<String, String>>()
def npx = Os.isFamily(Os.FAMILY_WINDOWS) ? "npx.cmd" : "npx"
def command = "${npx} --quiet --no-install react-native config"
// Running npx from the directory of the JS app which holds this script in its node_modules.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: blank space before comment and prefer /* */?
nit: I'd rephrase the comment a bit to explain when such a case happens, in other words, when it's run with a -p flag. We can also link to this PR for further ref.

Copy link
Member

@grabbou grabbou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks for keeping this clean. Left few commits and let's ship it.

@thymikee thymikee force-pushed the feat/gradle-from-js-root branch from 829a685 to a978a53 Compare November 14, 2019 15:42
@grabbou grabbou merged commit a3a70a1 into master Nov 14, 2019
@thymikee thymikee deleted the feat/gradle-from-js-root branch November 15, 2019 14:25
@ghost
Copy link

ghost commented Nov 15, 2019

I believe this has broken projects running in a monorepo as the react-native config needs to be run in the package directory, however is now running in the root directory as the node_module dependencies are hoisted.

@thymikee
Copy link
Member Author

@mbios could you provide a repro we could investigate?

@ghost
Copy link

ghost commented Nov 15, 2019

git clone git@github.com:brunolemos/react-native-web-monorepo.git
cd react-native-web-monorepo
rm yarn.lock
yarn
yarn android

yields this:

FAILURE: Build failed with an exception.

* Where:
Script '~/react-native-web-monorepo/node_modules/@react-native-community/cli-platform-android/native_modules.gradle' line: 206

* What went wrong:
A problem occurred evaluating script.
> React Native CLI failed to determine Android project configuration. This is likely due to misconfiguration. Config output:

but works without removing the yarn.lock as that points to 3.0.0 alpha

@thymikee
Copy link
Member Author

Yep, it's a regression, thanks for noticing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants