From 2958b97712388f3e9285139375d1e651ea6017c4 Mon Sep 17 00:00:00 2001 From: Andrey Lunyov Date: Thu, 28 Feb 2019 09:24:56 -0800 Subject: [PATCH] What if Watchman CLI is missing... (#2643) Summary: Here is an updated version of the `GraphQLWatchmanClient.isAvailable`. (as a fix for https://github.com/facebook/relay/issues/2617) Now, it's also checking if the `watchman` CLI is available. Test plan (Tested with `relay-examples:relay-publish-test` (https://github.com/relayjs/relay-examples/pull/89) First of all, I've removed watchman from my machine. Then, - Build the compiler - Install updated dependencies relay-examples (yarn) - Run the compiler (yarn build) Compiled without errors. Pull Request resolved: https://github.com/facebook/relay/pull/2643 Reviewed By: kassens Differential Revision: D14240611 Pulled By: alunyov fbshipit-source-id: 551a9338158debe7db8748676a6dccf0dfd479b0 --- .../relay-compiler/bin/RelayCompilerMain.js | 3 +-- .../core/GraphQLWatchmanClient.js | 24 +++++++++---------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/relay-compiler/bin/RelayCompilerMain.js b/packages/relay-compiler/bin/RelayCompilerMain.js index 16d55282e83ce..85775c210319b 100644 --- a/packages/relay-compiler/bin/RelayCompilerMain.js +++ b/packages/relay-compiler/bin/RelayCompilerMain.js @@ -185,7 +185,6 @@ Ensure that one such file exists in ${srcDir} or its parents. verbose: options.verbose, quiet: options.quiet, }); - const useWatchman = options.watchman && (await WatchmanClient.isAvailable()); const schema = getSchema(schemaPath); @@ -270,7 +269,7 @@ Ensure that one such file exists in ${srcDir} or its parents. // TODO: allow passing in a flag or detect? sourceControl: null, }); - if (!options.validate && !options.watch && options.watchman) { + if (!options.validate && !options.watch && useWatchman) { // eslint-disable-next-line no-console console.log('HINT: pass --watch to keep watching for changes.'); } diff --git a/packages/relay-compiler/core/GraphQLWatchmanClient.js b/packages/relay-compiler/core/GraphQLWatchmanClient.js index 58d0f640ba208..81a6a16533bf2 100644 --- a/packages/relay-compiler/core/GraphQLWatchmanClient.js +++ b/packages/relay-compiler/core/GraphQLWatchmanClient.js @@ -10,6 +10,7 @@ */ 'use strict'; +const childProcess = require('child_process'); const watchman = require('fb-watchman'); const MAX_ATTEMPT_LIMIT = 5; @@ -24,21 +25,18 @@ class GraphQLWatchmanClient { static isAvailable(): Promise { return new Promise(resolve => { - const client = new GraphQLWatchmanClient(MAX_ATTEMPT_LIMIT); - client.on('error', () => { + // This command not only will verify that watchman CLI is available + // More than that `watchman version` is a command that runs on the server. + // And it can tell us that watchman is up and running + // Also `watchman version` check ``relative_root`` capability + // under the covers + const proc = childProcess.spawn('watchman', ['version']); + proc.on('error', () => { resolve(false); - client.end(); }); - client.hasCapability('relative_root').then( - hasRelativeRoot => { - resolve(hasRelativeRoot); - client.end(); - }, - () => { - resolve(false); - client.end(); - }, - ); + proc.on('close', code => { + resolve(code === 0); + }); }); }