Repository: python/mypy

Issue:
JSONDecodeError when running MyPy
When I run the following command:

```
mypy ./src/main ./src/tests --junit-xml mypy.xml
```

I get the following error. 

```
Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/usr/local/lib/python3.7/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/usr/local/lib/python3.7/site-packages/mypy/main.py", line 92, in main
    res = build.build(sources, options, None, flush_errors, fscache)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 156, in build
    result = _build(sources, options, alt_lib_path, flush_errors, fscache)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 206, in _build
    graph = dispatch(sources, manager)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 2178, in dispatch
    graph = load_graph(sources, manager)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 2336, in load_graph
    root_source=True)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 1476, in __init__
    self.meta = find_cache_meta(self.id, path, manager)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 862, in find_cache_meta
    log_error='Could not load cache for {}: '.format(id))
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 790, in _load_json_file
    result = json.loads(data)  # TODO: Errors
  File "/usr/local/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 105 column 5 (char 5143)
```


Discussion:
I looks like you have broken cache. Have you tried `rm -rf .mypy_cache`?

Anyway, mypy shouldn't crash in this case, but show/log a reasonable error message. (There is even a TODO about this in the traceback.)
Is the .mypy_cache file always at the root of the project?
IIRC by default mypy stores it in current working directory from where mypy is run (but can be overridden by `--cache-dir`).

Resolving patch:
diff --git a/mypy/build.py b/mypy/build.py
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -825,8 +825,21 @@ def _load_json_file(file: str, manager: BuildManager,
         manager.log(log_error + file)
         return None
     manager.trace(log_sucess + data.rstrip())
-    result = json.loads(data)  # TODO: Errors
-    return result
+    try:
+        result = json.loads(data)
+    except ValueError:  # TODO: JSONDecodeError in 3.5
+        manager.errors.set_file(file, None)
+        manager.errors.report(-1, -1,
+                              "Error reading JSON file;"
+                              " you likely have a bad cache.\n"
+                              "Try removing the {cache_dir} directory"
+                              " and run mypy again.".format(
+                                  cache_dir=manager.options.cache_dir
+                              ),
+                              blocker=True)
+        return None
+    else:
+        return result
 
 
 def _cache_dir_prefix(manager: BuildManager) -> str:


Test patch: