From 2860540d5f317f05257d0695e4a24019c5d99f54 Mon Sep 17 00:00:00 2001
From: Gregory Szorc <gps@mozilla.com>
Date: Fri, 28 Jul 2017 16:11:22 -0700
Subject: [PATCH] Bug 1385380 - Detect watchman more resiliently; r=glandium

Before, a non-runnable watchman would result in configure
error.

A trivial refactor to ignore `watchman version` errors would still
result in setting WATCHMAN and exposing its presence to downstream
consumers.

Since we want WATCHMAN tied to a working watchman install, we
refactor the code so that binary location and its version test
are in the same function. They are either both defined or none
of them are.

MozReview-Commit-ID: 7wvBvYuOlmJ

--HG--
extra : rebase_source : d9cc648cdb8253bf8e413ec0fa5e969aa68f75b9
---
 moz.configure | 40 +++++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git a/moz.configure b/moz.configure
index ec531cedf78ee..68bdc011c496c 100755
--- a/moz.configure
+++ b/moz.configure
@@ -368,15 +368,41 @@ tup = check_prog('TUP', tup_progs)
 # watchman detection
 # ==============================================================
 
-watchman = check_prog('WATCHMAN', ('watchman',), allow_missing=True)
+option(env='WATCHMAN', nargs=1, help='Path to the watchman program')
 
-@depends_if(watchman)
-@checking('for watchman version')
+@depends('WATCHMAN')
 @imports('json')
-def watchman_version(watchman):
-    out = check_cmd_output(watchman, 'version')
-    res = json.loads(out)
-    return Version(res['version'])
+def watchman_info(prog):
+    if not prog:
+        prog = find_program('watchman')
+
+    if not prog:
+        return
+
+    out = check_cmd_output(prog, 'version', onerror=lambda: None)
+    if out is None:
+        return
+
+    # Assume a process not emitting JSON is not watchman or is a
+    # broken watchman.
+    try:
+        res = json.loads(out)
+    except ValueError:
+        return
+
+    return namespace(path=prog, version=Version(res['version']))
+
+@depends_if(watchman_info)
+@checking('for watchman')
+def watchman(w):
+    return w.path
+
+@depends_if(watchman_info)
+@checking('for watchman version')
+def watchman_version(w):
+    return w.version
+
+set_config('WATCHMAN', watchman)
 
 @depends_all(hg_version, hg_config, watchman)
 @checking('for watchman Mercurial integration')
-- 
GitLab