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

Cache is not being written or used #48

Open
ewhauser opened this issue Mar 18, 2022 · 4 comments
Open

Cache is not being written or used #48

ewhauser opened this issue Mar 18, 2022 · 4 comments

Comments

@ewhauser
Copy link

The integration attempts to write out cache files using mypy's undocumented Bazel integration (--bazel, --package-root, and --cache-map) but these files are never written (due to sandboxing?). This can be reproduced by via the tests in this repo:

$ ./test.sh
$ find $(bazel info output_base) -name '*.data.json'
# returns nothing

If you temporarily disable sandboxing via --spawn-strategy=standalone, i.e.:

 diff --git a/test/shell/test_mypy.sh b/test/shell/test_mypy.sh
 index db1dcc0..a3bbb6c 100644
 --- a/test/shell/test_mypy.sh
 +++ b/test/shell/test_mypy.sh
 @@ -5,7 +5,7 @@ dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  runner=$(get_test_runner "${1:-local}")

  test_ok_on_valid_imported_mypy_typings() {
 -  action_should_succeed build --verbose_failures --aspects //:mypy.bzl%mypy_aspect --output_groups=mypy //test:correct_imported_mypy_typings
 +  action_should_succeed build --spawn_strategy=standalone --verbose_failures --aspects //:mypy.bzl%mypy_aspect --output_groups=mypy //test:correct_imported_mypy_typings
  }

  test_ok_on_valid_imported_mypy_test() {

and then run the tests again:

$ bazel clean; ./test.sh
$ find $(bazel info output_base) -name '*.data.json'
/home/vscode/.cache/bazel/_bazel_vscode/8dbf04a8279d05eb224863e2e901551c/execroot/bazel_mypy_integration/bazel-out/k8-fastbuild/bin/test/correct_generated.py.data.json
$ find $(bazel info output_base) -name '*.meta.json'
/home/vscode/.cache/bazel/_bazel_vscode/8dbf04a8279d05eb224863e2e901551c/execroot/bazel_mypy_integration/bazel-out/k8-fastbuild/bin/test/correct_generated.py.meta.json

then our the cache files for our transitive dependencies are written to the output directory. However, it also writes our the cache files for the root dependency directory to the workspace:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test/__init__.data.json
        test/__init__.meta.json
        test/correct_imported_typings.py.data.json
        test/correct_imported_typings.py.meta.json

If you enable verbose mypy logging (via setting the DEBUG variable in mypy.bzl to True and changing the tests so output to stdout and stderr), then you can see mypy's logs:

OG:  Cached module os.path has changed interface
LOG:  Writing sys /home/vscode/.cache/bazel/_bazel_vscode/8dbf04a8279d05eb224863e2e901551c/sandbox/processwrapper-sandbox/66/execroot/bazel_mypy_integration/bazel-out/host/bin/mypy/mypy.r
unfiles/mypy_integration_pip_deps/pypi__mypy/mypy/typeshed/stdlib/3/sys.pyi sys.meta.json sys.data.json
LOG:  Cached module sys has changed interface
LOG:  Writing builtins /home/vscode/.cache/bazel/_bazel_vscode/8dbf04a8279d05eb224863e2e901551c/sandbox/processwrapper-sandbox/66/execroot/bazel_mypy_integration/bazel-out/host/bin/mypy/m
ypy.runfiles/mypy_integration_pip_deps/pypi__mypy/mypy/typeshed/stdlib/2and3/builtins.pyi builtins.meta.json builtins.data.json
LOG:  Cached module builtins has changed interface
LOG:  Processing SCC singleton (importlib) as inherently stale with stale deps (builtins importlib.abc types typing)
LOG:  Writing importlib /home/vscode/.cache/bazel/_bazel_vscode/8dbf04a8279d05eb224863e2e901551c/sandbox/processwrapper-sandbox/66/execroot/bazel_mypy_integration/bazel-out/host/bin/mypy/
mypy.runfiles/mypy_integration_pip_deps/pypi__mypy/mypy/typeshed/stdlib/3/importlib/__init__.pyi importlib/__init__.meta.json importlib/__init__.data.json
LOG:  Cached module importlib has changed interface
LOG:  Processing SCC singleton (test) as inherently stale with stale deps (builtins)
LOG:  Writing test ./test/__init__.py test/__init__.meta.json test/__init__.data.json
LOG:  Cached module test has changed interface
LOG:  Processing SCC singleton (test.correct_generated) as inherently stale with stale deps (builtins typing)
LOG:  Writing test.correct_generated bazel-out/k8-fastbuild/bin/test/correct_generated.py bazel-out/k8-fastbuild/bin/test/correct_generated.py.meta.json bazel-out/k8-fastbuild/bin/test/co
rrect_generated.py.data.json
LOG:  Cached module test.correct_generated has changed interface
LOG:  Processing SCC singleton (test.correct_imported_typings) as inherently stale with stale deps (builtins test.correct_generated)
LOG:  Writing test.correct_imported_typings test/correct_imported_typings.py test/correct_imported_typings.py.meta.json test/correct_imported_typings.py.data.json
LOG:  Cached module test.correct_imported_typings has changed interface
LOG:  No fresh SCCs left in queue

(You'll note in the logs above that cache files appear to be written to 3 separate locations (sandbox/processwrapper-sandbox/66/execroot, bazel-out/k8-fastbuild/bin/test/, and the workspace).

@jmantherobot
Copy link

jmantherobot commented Jun 29, 2024

Are there any updates on this? I'm seeing a similar issue.

@jmantherobot
Copy link

We're seeing some pretty heavy slow downs from a development perspective because of this issue. Is there anyone that has found a workaround?

@jmantherobot
Copy link

Hey @adzenith long time no see! Didn't realize you were maintaining this.

@ph03
Copy link

ph03 commented Jul 11, 2024

We are struggling with the same issue in a larger codebase that would definitely benefit from re-using cached mypy analysis results.

A simple hack to side-step all of bazel's internal artifact caching / sandboxing would be to overrule the cache output target with

--- a/mypy/build.py
+++ b/mypy/build.py
@@ -1178,7 +1178,7 @@ def _load_json_file(
 
 def _cache_dir_prefix(options: Options) -> str:
     """Get current cache directory (or file if id is given)."""
-    if options.bazel:
+    if options.bazel and not os.environ.get('MYPY_CACHE_DIR'):
         # This is needed so the cache map works.
         return os.curdir
     cache_dir = options.cache_dir

to make use of an explicitly set cache folder given by the MYPY_CACHE_DIR env. However, I'd prefer to have the cache output properly be handled by bazel actions instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants