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

Replace applescript with browser-launcher2 #2406

Closed
wants to merge 1 commit into from

Conversation

elliottsj
Copy link
Contributor

This allows...

  1. launching Chrome on platforms other than OS X
  2. users to launch their own instance of Chrome (e.g. via command line) rather than being forced to use the default instance (i.e. tell application "Chrome" always used the default instance)

isDebuggerConnected() addresses the problem in #510 where the dev tools would only open once per server session.

Also passing the --disable-web-security flag when launching Chrome so that users can inspect network requests in Chrome by commenting the xhr polyfill in InitializeJavaScriptAppEngine.js: #934 (comment)

Also added a --dangerouslyDisableChromeDebuggerWebSecurity flag to
packager.js to enable Chrome --disable-web-security flag so that users can inspect network requests in Chrome by commenting the xhr polyfill in InitializeJavaScriptAppEngine.js: #934 (comment)


The only issue I've found in my testing is the console output WARN: not opened whenever you reload the iOS app:

[21:42:58] <END>   request:/index.ios.map (45ms)
[21:42:58] <START> request:/index.ios.bundle
[21:42:58] <END>   request:/index.ios.bundle (6ms)
WARN: not opened
WARN: not opened
[21:43:07] <START> request:/index.ios.bundle
[21:43:07] <END>   request:/index.ios.bundle (12ms)
[21:43:07] <START> request:/index.ios.bundle

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Aug 22, 2015
console.warn(stderr);
launch(debuggerURL, {
browser: 'chrome',
options: ['--disable-web-security'],
Copy link
Contributor

Choose a reason for hiding this comment

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

SOP should be kept on by default. It will be bad if someone launches Chrome with the RN tools and then starts using it as their browser.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair. As an alternative, what do you think about adding a command line option to packager.js to disable SOP? Something like

node packager.js --disable-web-security

@ide
Copy link
Contributor

ide commented Aug 22, 2015

Can you also squash your commits?

@elliottsj
Copy link
Contributor Author

Is it alright if I keep the commits separate for now, until we have a solution for if/how to use --disable-web-security? Will squash after that.

@ide
Copy link
Contributor

ide commented Aug 22, 2015

Sure. Re: solution I think you could expose a CLI flag from the packager that sets the disable-web-security option. Something like --dangerously-enable-chrome-debugger-fetch so that the user is very aware of what's going on.

@elliottsj
Copy link
Contributor Author

Ok, I've added a command line argument --dangerouslyDisableChromeDebuggerWebSecurity and squashed the commits.

}, function(err, instance) {
if (err) {
console.error('Failed to launch chrome', err);
next(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

missing return here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@ChristianHersevoort
Copy link
Contributor

After rebaseing it on latest master it also works for React Native Android on Linux 👍

@elliottsj
Copy link
Contributor Author

Awesome that it works! On that note, I've just rebased this PR onto master.

@BerndWessels
Copy link
Contributor

Hi guys
Has that been merged into master?

@ide
Copy link
Contributor

ide commented Sep 20, 2015

@BerndWessels no, this PR is still open. The plan is for the packager to be moved into another repo some time next week or maybe the week after, so PRs that touch the packager are on hiatus.

@BerndWessels
Copy link
Contributor

Hi @ide.

Thanks for the information. There are several small fixes that all together will enable React-Native development on Windows. You can find them by following this issue.

Please let us know when all these made it into master - I believe there are masses of developers desperately waiting to be able to start developing with React-Native on Windows.

Thank you
Bernd

@darkyen
Copy link

darkyen commented Sep 21, 2015

@BerndWessels you can pull the changes to your local and test on windows (that is what I am doing)

@elliottsj
Copy link
Contributor Author

@frantic I've just amended my commit with the following two changes:

  1. Keep a reference to the opened Chrome instance. Now when launching the dev tools, the existing Chrome instance (if there is one) is closed first. This fixes the issue where an empty tab pops up instead of opening the debugger UI.
  2. Return the isDebuggerConnected function from attachToServer instead of exporting it from the module. This means we can keep the connected clients' state (var clients = [];) scoped to attachToServer instead of to the module itself.

@jrouleau
Copy link

@davidgilbertson

Slight update to the quick fix if your don't want to get spammed by new tabs:

@@ -184,22 +184,24 @@ function getDevToolsLauncher(options) {
+  var launched = false;
   return function(req, res, next) {
     if (req.url === '/debugger-ui') {
       var debuggerPath = path.join(__dirname, 'debugger.html');
       res.writeHead(200, {'Content-Type': 'text/html'});
       fs.createReadStream(debuggerPath).pipe(res);
     } else if (req.url === '/launch-chrome-devtools') {
       var debuggerURL = 'http://localhost:' + options.port + '/debugger-ui';
-      var script = 'launchChromeDevTools.applescript';
       console.log('Launching Dev Tools...');
-      execFile(path.join(__dirname, script), [debuggerURL], function(err, stdout, stderr) {
-        if (err) {
-          console.log('Failed to run ' + script, err);
-        }
-        console.log(stdout);
-        console.warn(stderr);
-      });
+      if (!launched) {
+        // NOTE: The exec command varies depending on your OS and chrome installation.
+        require('child_process').exec('google-chrome "' + debuggerURL + '"', function (err) {
+          if (err) {
+            console.log("error", err);
+          }
+        });
+        launched = true;
+      }
       res.end('OK');
     } else {
       next();
     }
   };
 }

@mkonicek
Copy link
Contributor

mkonicek commented Oct 8, 2015

Sorry for the delay @elliottsj, pinging @frantic to take another look.

@mkonicek
Copy link
Contributor

mkonicek commented Oct 8, 2015

@elliottsj Is it possible to keep the exact original developer experience?

@martinbigio
Copy link
Contributor

I'm landing a couple of diffs that clean up this code separating each middleware packager.js has on multiple files. You'll need to rebase..

@elliottsj
Copy link
Contributor Author

@mkonicek I'm not sure whether it's possible to keep the original applescript behaviour (i.e. using the user's normal Chrome instance) using browser-launcher2. How important is it to use the default Chrome instance? With the --dangerouslyDisableChromeDebuggerWebSecurity flag, I think the only choice is to launch a new Chrome instance, since we can't (and shouldn't) modify the web security setting on the user's already-running Chrome instance.

@martinbigio Let me know when your diff lands 😄

@frantic
Copy link
Contributor

frantic commented Oct 8, 2015

@elliottsj my main concern is that it opens another Chrome instance, which is pretty confusing. I just tried running Google Chrome without params and it re-uses the same app instance, so it should be possible to maintain the current behavior (maybe file an issue against browser-launcher2). If it's hard to workaround this issue I'd be fine gating this code to Windows/Linux only. @martinbigio's refactoring will make it easier to implement and test your change.

@elliottsj
Copy link
Contributor Author

@frantic Hmm, I agree that it might be a bit confusing at first. Though the node-debug command of node-inspector also opens in a new Chrome instance using browser-launcher2 (via biased-opener), so some developers may be used to it.

If we opt for using the user's default Chrome instance, how do you think we should handle the --dangerouslyDisableChromeDebuggerWebSecurity option?

@frantic
Copy link
Contributor

frantic commented Oct 8, 2015

If we opt for using the user's default Chrome instance, how do you think we should handle the --dangerouslyDisableChromeDebuggerWebSecurity option?

if the option is present do launch new Chrome instance, no doubts about that.

@elliottsj
Copy link
Contributor Author

Alright, sounds good 😁

I'll do some digging to see if browser-launcher2 supports what we want, or perhaps use an alternative like https://github.com/sindresorhus/opn.

@frantic
Copy link
Contributor

frantic commented Oct 8, 2015

thanks!

This allows...

1. launching Chrome on platforms other than OS X
2. users to launch their own instance of Chrome (e.g. via command line)
   rather than being forced to use the default instance (i.e.
   `tell application "Chrome"` always used the default instance)

`isDebuggerConnected()` addresses the problem in facebook#510 where the dev tools
would only open once per server session.

Add a '--dangerouslyDisableChromeDebuggerWebSecurity' flag to
packager.js to enable Chrome '--disable-web-security' flag.

This allows users to inspect network requests in Chrome by commenting
the xhr polyfill in InitializeJavaScriptAppEngine.js:
  facebook#934 (comment)

Usage:

    node packager.js --dangerouslyDisableChromeDebuggerWebSecurity

or:

    packager.sh --dangerouslyDisableChromeDebuggerWebSecurity
@facebook-github-bot
Copy link
Contributor

@elliottsj updated the pull request.

@elliottsj
Copy link
Contributor Author

I opened a new pull request using opn instead of browser-launcher2: #3394

It uses the default Chrome instance on OS X; I have yet to test on Linux/Windows.

@elliottsj elliottsj closed this Oct 14, 2015
ghost pushed a commit that referenced this pull request Oct 23, 2015
Summary: This allows opening the Chrome debugger on OS X, Linux, and Windows, and succeeds the previous PR which used [browser-launcher2](https://github.com/benderjs/browser-launcher2) and included a `--dangerouslyDisableChromeDebuggerWebSecurity` option: #2406

[opn](https://github.com/sindresorhus/opn) is cross-platform and much simpler than browser-launcher2 (since we don't have to manage the opened Chrome instance; the OS will just use the default instance).
Closes #3394

Reviewed By: mkonicek

Differential Revision: D2550996

Pulled By: frantic

fb-gh-sync-id: fa4cbe55542562f30f77e0a6ab4bc53980ee13aa
@syarul
Copy link

syarul commented Oct 26, 2015

v0.13.0 does not have this fix yet, what @davidgilbertson and + @jrouleau suggest seem to be working on windows without opening new tab each reload. Are these already on pull request?

@elliottsj elliottsj deleted the browser-launcher branch October 26, 2015 03:18
@elliottsj
Copy link
Contributor Author

@syarul This PR was closed in favour of #3394, which does not open a new tab each reload.

MattFoley pushed a commit to skillz/react-native that referenced this pull request Nov 9, 2015
Summary: This allows opening the Chrome debugger on OS X, Linux, and Windows, and succeeds the previous PR which used [browser-launcher2](https://github.com/benderjs/browser-launcher2) and included a `--dangerouslyDisableChromeDebuggerWebSecurity` option: facebook#2406

[opn](https://github.com/sindresorhus/opn) is cross-platform and much simpler than browser-launcher2 (since we don't have to manage the opened Chrome instance; the OS will just use the default instance).
Closes facebook#3394

Reviewed By: mkonicek

Differential Revision: D2550996

Pulled By: frantic

fb-gh-sync-id: fa4cbe55542562f30f77e0a6ab4bc53980ee13aa
Crash-- pushed a commit to Crash--/react-native that referenced this pull request Dec 24, 2015
Summary: This allows opening the Chrome debugger on OS X, Linux, and Windows, and succeeds the previous PR which used [browser-launcher2](https://github.com/benderjs/browser-launcher2) and included a `--dangerouslyDisableChromeDebuggerWebSecurity` option: facebook#2406

[opn](https://github.com/sindresorhus/opn) is cross-platform and much simpler than browser-launcher2 (since we don't have to manage the opened Chrome instance; the OS will just use the default instance).
Closes facebook#3394

Reviewed By: mkonicek

Differential Revision: D2550996

Pulled By: frantic

fb-gh-sync-id: fa4cbe55542562f30f77e0a6ab4bc53980ee13aa
cpojer pushed a commit to facebook/metro that referenced this pull request Jan 26, 2017
Summary: This allows opening the Chrome debugger on OS X, Linux, and Windows, and succeeds the previous PR which used [browser-launcher2](https://github.com/benderjs/browser-launcher2) and included a `--dangerouslyDisableChromeDebuggerWebSecurity` option: facebook/react-native#2406

[opn](https://github.com/sindresorhus/opn) is cross-platform and much simpler than browser-launcher2 (since we don't have to manage the opened Chrome instance; the OS will just use the default instance).
Closes facebook/react-native#3394

Reviewed By: mkonicek

Differential Revision: D2550996

Pulled By: frantic

fb-gh-sync-id: fa4cbe55542562f30f77e0a6ab4bc53980ee13aa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.