Write mb analyze output errors to json output.

Bug: 985959
Change-Id: Ib683447a9dd35f0f4982d6bae7e642693bb9335b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1711023
Commit-Queue: Debrian Figueroa <[email protected]>
Reviewed-by: Dirk Pranke <[email protected]>
Cr-Commit-Position: refs/heads/master@{#679619}
diff --git a/tools/mb/mb.py b/tools/mb/mb.py
index db31cb4..083adba 100755
--- a/tools/mb/mb.py
+++ b/tools/mb/mb.py
@@ -123,7 +123,7 @@
     subp.add_argument('output_path',
                       help='path to a file containing the output arguments '
                            'as a JSON object.')
-    subp.add_argument('--json_output',
+    subp.add_argument('--json-output',
                       help='Write errors to json.output')
     subp.set_defaults(func=self.CmdAnalyze)
 
@@ -143,7 +143,7 @@
     subp.add_argument('--swarming-targets-file',
                       help='save runtime dependencies for targets listed '
                            'in file.')
-    subp.add_argument('--json_output',
+    subp.add_argument('--json-output',
                       help='Write errors to json.output')
     subp.add_argument('path',
                       help='path to generate build into')
@@ -834,7 +834,7 @@
 
     ret, output, _ = self.Run(cmd)
     if ret:
-      if getattr(self.args, 'json_output', None) and output:
+      if self.args.json_output:
         # write errors to json.output
         self.WriteJSON({'output': output}, self.args.json_output)
       # If `gn gen` failed, we should exit early rather than trying to
@@ -1380,8 +1380,11 @@
     try:
       self.WriteJSON(gn_inp, gn_input_path)
       cmd = self.GNCmd('analyze', build_path, gn_input_path, gn_output_path)
-      ret, _, _ = self.Run(cmd, force_verbose=True)
+      ret, output, _ = self.Run(cmd, force_verbose=True)
       if ret:
+        if self.args.json_output:
+          # write errors to json.output
+          self.WriteJSON({'output': output}, self.args.json_output)
         return ret
 
       gn_outp_str = self.ReadFile(gn_output_path)